asp.net manipulates javascript: There are two ways for confirm to return values

  • 2021-01-06 00:31:59
  • OfStack

The use of confirm in asp.net can be divided into two types:

1. ajax is not used,confirm will cause a face refresh

2. ajax is used and will not refresh

A. Not using ajax, can be done with StringBuilder.

(1)asp.net uses StringBuilder to control background operation javascript:confirm return value, this method is more cumbersome

1. Background start events


StringBuilder sb = new StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("var val=window.confirm('Are you sure!');");
sb.Append("window.document.getElementById('TextBox1').value=val;");
sb.Append("__doPostBack('TextBox1','');");
sb.Append("</script>");
this.RegisterStartupScript(System.Guid.NewGuid().ToString(), sb.ToString());

2. Front desk code:


<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

3. Background event code for text box OnTextChanged="TextBox1_TextChanged"


protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (((TextBox)(sender)).Text != "")
{
if (((TextBox)(sender)).Text.ToUpper() == "TRUE")
{
// Determine after the execution of another 1 An event / methods  
}
if (((TextBox)(sender)).Text.ToUpper() == "FALSE")
{
// Cancel after execution another 1 An event / methods  

}

} 
}

The event can also be written in other method-driven ways. It just determines the value of TexBox1.
Note: This event is caused by AutoPostBack="true" causing the page to refresh and the global variable to be lost. It is better to use the session variable to hold the value that needs to be stored.

(2) through the foreground Javascript to determine the method of background execution, there is more OnClientClick event this method is the simplest

Foreground button event


<asp:Button ID="bt_DeleteButton" runat="server" OnClick="bt_DeleteButton_Click" OnClientClick="if(confirm(' Call the background bt_DeleteButton_Click Event, are you sure to proceed? ')){return true;}else{return false;}" Text=" delete " Visible="False" />

Just add it on the page

a.OnClick background events,

b.OnClientClick event,javascript of confirm, select OK to execute OnClick background is the event bt_DeleteButton_Click

ES69en: A page that uses the ES70en control


ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert(' prompt ');if(confirm(' Whether to continue or not yes)){alert(' This page continues to be added ')}else{window.open('productManage.aspx','_blank');}", true);


Related articles: