简体   繁体   中英

Javascript Alert in ASP.NET

I want to use Javascript Alert function in my ASP.NET page.

For example like this;

Response.Write("<script language=javascript>alert('ERROR');</script>);

But, this doesn't work.

I ask in here what am i doing wrong and everyone suggest me using RegisterScriptBlock

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('ERROR')",true);

But i don't want use it because it's working with PostBack

How can i do that without PostBack ?

EDIT: For example for using;

try
{
    string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

    SqlConnection myConnection = new SqlConnection(strConnectionString);
    myConnection.Open();

    string hesap = Label1.Text;
    string musteriadi = DropDownList1.SelectedItem.Value;
    string avukat = DropDownList2.SelectedItem.Value;

    SqlCommand cmd = new SqlCommand("INSERT INTO AVUKAT VALUES (@MUSTERI, @AVUKAT, @HESAP)", myConnection);

    cmd.Parameters.AddWithValue("@HESAP", hesap);
    cmd.Parameters.AddWithValue("@MUSTERI", musteriadi);
    cmd.Parameters.AddWithValue("@AVUKAT", avukat);
    cmd.Connection = myConnection;

    SqlDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    Response.Redirect(Request.Url.ToString());
    myConnection.Close();
}
catch (Exception)
{
    Response.Write("<h2>ERROR</h2>");
}

See a note from MSDN :

If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class. You can get a reference to the ClientScriptManager object from the ClientScript property of the page.

So, I think ClientScriptManager.RegisterStartupScript method is what you need:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(
    this.GetType(), 
    " ", 
    @"<script language=javascript>alert('ERROR');</script>", 
    true
);

In your code you forgot the quotation mark. I just tried it in a sample page like this:

Response.Write("<script language=javascript>alert('ERROR');</script>");

and it worked. Where did you place the Response.Write in your code? Could you give some more details? What do you want to do?

For displaying an alert message to the User, in a webpage i have a code have a look at this

public void UserMsgBox(string sMsg)
{
StringBuilder sb = new StringBuilder();
System.Web.UI.Control oFormObject = null;
sMsg = sMsg.Replace("'", "\\'");
sMsg = sMsg.Replace(Strings.Chr(34), "\\" + Strings.Chr(34));
sMsg = sMsg.Replace(Constants.vbCrLf, "\\n");
sMsg = "<script language='javascript'>alert(\"" + sMsg + "\")</script>";
sb = new StringBuilder();
sb.Append(sMsg);
foreach (System.Web.UI.Control oFormObject_loopVariable in this.Controls) {
    oFormObject = oFormObject_loopVariable;
    if (oFormObject is HtmlForm) {
        break; // TODO: might not be correct. Was : Exit For
    }
}
oFormObject.Controls.AddAt(oFormObject.Controls.Count, new LiteralControl(sb.ToString()));
}

try using RegisterStartupscript for registering the script. Refer : http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerstartupscript.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM