Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Referring to http://www.aspsnippets.com/Articles/Server-Side-Code-Behind-Yes-No-Confirmation-Message-Box-in-ASPNet.aspx[^], I revised the code and added an argument in the JS function in order to catch the "Yes" or "No" value from the Confirmation dialog. The related code are shown below:
C#
// Code-behind
protected void Button1_Click(object sender, EventArgs e)  {
    int d = 10;
    string confirmValue = Request.Form["confirm_value('" + d + "'"];   
    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);
}

// JavaScript
function Confirm(days) {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "Your Password is to be expired in " + days + " days.  Do you want to reset your Password?"";
    if (confirm("Do you want to save data?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);
}

// HTML
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"  OnClientClick = "Confirm(d)" />

However, I got Microsoft JScript runtime error: 'd' is undefined. How can this be debugged? Thanks a lot.
Posted

When Button1 is clicked you told the browser to call a function named Confirm and you are telling it to pass the variable d. But you never define what d is.

A couple of things:

1. Don't use C# to create JavaScript. Just write your JavaScript in a JS file and include it in your page. It keeps things much easier to deal with.
2. Don't name a function the same as a built-in function. Lowercase confirm is a built-in JS function and will confuse other developers who have to later maintain your code.
3. You basically have 2 events on your Button1. 1 that will fire first in JavaScript and then another that will happen afterwards in C#. Again, don't use C# for this purpose. Just do it in JavaScript. C# should be used for things you need to do on the server, such as access a database, send email, etc.
 
Share this answer
 
Comments
s yu 23-Mar-15 13:10pm    
In the Btn_Click event, I need to do something reacted with server side. Having a popup by calling a JS is to avoid server-side reaction. Your recommendation using lower-case for JS is appreciated. Thanks for your response.
C#
// Code-behind
protected void Button1_Click(object sender, EventArgs e)  {
    int d = 10;
    // **** Changed Request.Form code ****
    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);
}


JavaScript
// JavaScript
function Confirm(days) {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    // **** Changed name ****
    confirm_value.name = "confirm_value";
    if (confirm("Do you want to save data?")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);
}



HTML
// HTML
<!-- **** Hard coded param **** -->
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"  OnClientClick = "Confirm(10)" />
 
Share this answer
 
v2
Comments
s yu 23-Mar-15 10:03am    
The value of '10' in my code is for testing only. Actually, variable days defined here is dynamic. Thanks for your response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900