Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want a javascript confirm message box from aspx.cs page. In some button click function i want to write the code not in the Page_Load.
Want the confirmation box during execution so that if user clicks on the OK it should procced further and for Cancle clicks it should come out the server side with out executing the rest of the code.

please find the code where i want this functionality

protected void cmdSolve_ServerClick(object sender, System.EventArgs e)
{
string name = dtAttachedFiles.Rows[i]["FileName"].ToString().ToUpper();
if (name.Contains("BACKOUT"))
{
string ext = Path.GetExtension(name);
if (ext != ".PDF")
{
Response.Write(<script language=Javascript>confirm((\"There is no Backout Plan attached.\");</script>);
}

//Rest of the code
}



in this point of execution i want that pop up. On OK click it will execute the rest of the code and Cancel click it will not execute the rest of the code...
Posted

You can use something like the following:

XML
/// <summary>
/// Add a javascript confirm to a <see cref="WebControl"/>.
/// </summary>
/// <param name="control">Would normally expect this to be a <see cref="Button"/>.</param>
/// <param name="Message">The message to display.</param>
public static void ConfirmButton(WebControl Control, string Message)
{
    if (Control != null)
    {
        Control.Attributes.Add("onclick", "javascript:if(confirm('" + Message + "') == false) return false;");
    }
}


Call this in the OnLoad event of the page passing the name of the control (usually a button) and a message to display. If the user chooses 'Cancel' nothing will happen. If they choose 'Ok' the process will continue.

You should be able to adapt this to your specific needs: if you're not sure how you'll need to do some research to figure it out.
 
Share this answer
 
Comments
sajaldutta 20-Jun-11 6:31am    
i dont want to call confirm alert box in the pageload....i want during execution itself it ll give the alert and based on input(ok/cancel) it should proceed further.
R. Giskard Reventlov 20-Jun-11 6:37am    
Doh! Think: what this does is attach the confirm box to a control. It is not called until you, for instance, press the button.
Good (and simple) example to create an alert box here[^].
 
Share this answer
 

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