Monday, August 20, 2012

Get Return Value From Confirm Box in C# (ASP.Net)

private void Button_click(object sender, EventArgs e)
    {
        var sbConfirm = new StringBuilder();
        string strMessage = string.Format("Are you sure to go ahead.");           
        sbConfirm.AppendFormat("var result = window.confirm('" + strMessage + "');\n");
        sbConfirm.Append("if (result)\n");
        sbConfirm.Append("__doPostBack('ConfirmTarget', result);\n");
        ClientScript.RegisterStartupScript(GetType(), "MyConfirmScript", sbConfirm.ToString(), true);
      
    }


In Page Load you have to write :

protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.GetPostBackEventReference(this, string.Empty);
         string eventTarget = Request["__EVENTTARGET"] ?? string.Empty;
         string eventArgument = Request["__EVENTARGUMENT"] ?? string.Empty;

            switch (eventTarget)
            {
                case "ConfirmTarget":
                    if (Convert.ToBoolean(eventArgument))
                    {
                          //.....your code
                    }
                    break;
            }
 }

No comments:

Post a Comment