How to pop up an alert on a button. Click on the code in Asp.net 2.0?

I tried this code but did not work.

protected void btnAbc_Click(object sender, EventArgs e)
{
    string script = "<script language='javascript'>alert('abc');</script>";"
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script,true);
}

Recommendation Pls.

+3
source share
2 answers

You have double script tags. Add script tags:

protected void btnAbct_Click(object sender, EventArgs e) {
   string script = "<script type=\"text/javascript\">alert('abc');</script>";
   ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}

Or let the method add it:

protected void btnAbct_Click(object sender, EventArgs e) {
   string script = "alert('abc');";
   ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
}

Not both.

Also consider whether the method is suitable RegisterStartupScriptfor what you want to do.

+14
source
ScriptManager.RegisterStartupScript(this, this.GetType(), "alerts", "javascript:alert('hai')", true); 

or

string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
+1
source

Source: https://habr.com/ru/post/1725935/


All Articles