Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  79] [ 3]  / answers: 1 / hits: 11726  / 10 Years ago, wed, april 30, 2014, 12:00:00

I have created a hidden field in my JavaScript and this hidden filed is used to capture the value that users select from the message box. When selection is made from the drop-down box, I am making a call to check if the ID that is selected from the drop down is already in the table, if only the ID is found in the table then I am calling the JavaScript to show the message box. If user Selects Yes show some alert and if user selects No show another alert. Somehow, I am having hard time to make it to work, nothing happens when I make selection from the drop-down. Please help as I have spent days on researching this. thanks
here my code:



 //javascript code
<script type=text/javascript>
function Confirm() {
var confirm_value = document.createElement(INPUT);
confirm_value.type = hidden;
confirm_value.name = confirm_value;
if (confirm(Do you want to delete data?)) {
confirm_value.value = Yes;
} else {
confirm_value.value = No;
}
document.forms[0].appendChild(confirm_value);
}
</script>


//drop down in aspx file







//code behind
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{

string ID = ddlName.SelectedValue;

using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = SELECT count(*) from MyTable WHERE ID =@ID;
cmd.Parameters.AddWithValue(@ID, ID);
con.Open();
int result = (int)cmd.ExecuteScalar();

if (result >= 1)
{
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), text, Confirm(), true);
string confirmValue = Request.Form[confirm_value];
if (confirmValue == Yes)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), alert, alert('You clicked YES!'), true);
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), alert, alert('You clicked NO!'), true);
}

}
con.Close();
}
}

}

More From » c#

 Answers
5

This isn't doing what you think it's doing:



ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), text, Confirm(), true);
string confirmValue = Request.Form[confirm_value];


Server-side code runs in its entirety before rendering the page to the client. There's no implicit back-and-forth between the two. So the above lines basically say:



When the page renders, create a form element in JavaScript.
Get the value from the form element.


Well, the page hasn't rendered yet. There is no form element. So there's no value to get at this point.



As suggested in the comments, what you're looking to do is make an AJAX request from your client-side (JavaScript) code to a server-side resource (page, handler, etc.). There are lots of tutorials available on how to use AJAX in ASP.NET. It looks like you're using WebForms, so I'd recommend either using an ASHX handler (best practice) or a standalone ASPX page (sometimes easier) to facilitate the AJAX requests.



Essentially you would want to build a JavaScript function to call in your client-side code which will initiate the AJAX call and include the form value as a parameter (either a GET or POST parameter would be easy). The server-side handler (ASHX or ASPX) would receive that parameter, check the database, and respond with a result. The result is the content of the response (not like returning from a method), there shouldn't be any page output. The JavaScript AJAX call receives the response, checks the returned value, and responds to the user accordingly.



The main thing to remember is that there is a hard physical separation between client-side code and server-side code. They can't call each other, they don't run simultaneously, etc. Clients make HTTP requests to servers, servers receive those requests, run some code, and respond with HTTP responses. Clients receive those responses, parse the results, and run their code accordingly. Everything happens as HTTP requests and responses.


[#45644] Tuesday, April 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorafrancisl

Total Points: 80
Total Questions: 96
Total Answers: 102

Location: Ukraine
Member since Sun, Dec 13, 2020
4 Years ago
;