简体   繁体   中英

Javascript alert in C# not working

I have few textboxes to view and also update records. Here is my code to check for any duplicate record before save or update & alert the user if there is any. The javascript alert is popping up for 'Update' but not for 'Save'. The debugger even reads the line in the else block. Where am I going wrong?

protected void Save_Click(object sender, EventArgs e)
 {
    int returnId;
    returnId = chkDuplicates(value,1);//function to check for any duplicate value
    if(returnId==0)
      //save the record
    else
     ScriptManager.RegisterStartupScript(this,typeof(Page), "MsgSave", 
                                         "alert('value exists');", true);
 }

  protected void Update_Click(object sender, EventArgs e)
 {
    int returnId;
    returnId = chkDuplicates(value,2);//function to check for any duplicate value
    if(returnId==0)
      //update the record
    else
     ScriptManager.RegisterStartupScript(this,typeof(Page), "MsgUpdate", 
                                         "alert('value exists');", true);
 }

Please try this, It may helps

   ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
     "Msg", "alert('value exists');", true);

Have you made sure that the function chkDuplicates actually returns a non zero value for the Save_Click use case?

Any ways this is not the appropriate way to implement such use cases. The best way would be to create these methods as Webmethods and call them via ajax calls on button click events using javascript. Throw a custom exception in case the duplicate exists. This will result in the faliure callback on your ajax call. On this call back show watever alert you need to show.

This is how you do it elegantly and not register scripts in server side code. This will also save your application from post backs and unnecessary page reloads.

In both of your Startup Scripts, your Key is

"Msg"

and If you look for the definition of 'string key' parameter in RegisterStartupScript Method, it says ' A Unique Identifier for the Script Block '. So it must be Unique from other Keys in the specific Page.

On the button, add an onClientClick to call the JavaScript. If it then passes you're, server side code will be called.

显示javascript警报弹出窗口的另一种方法可能是这样

Response.Write("<script>alert(\"Your text here\");</script>");

try this...it workd for me....

    ClientScript.RegisterStartupScript(typeof(Page), "alertMessage",
"<script type='text/javascript'>alert('value exist');window.location.replace('yourpage.aspx');</script>");

Here we are showing alert message directly

ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true);

Here we are showing alert message from javascript

ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);

These are the two-ways to display alert messages in c#

Please make sure you are not using Save button under any Update Panel . this problem occurs when you do asynchronous postback. I tested your code in new web page and it is working fine.

Code at aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Save" runat="server" Text="Save" onclick="Save_Click1" />
        <asp:Button ID="Update" runat="server" Text="Update" onclick="Update_Click1" /></div>
    </form>
</body>
</html>

Code at Code-Behind file

 protected void Save_Click1(object sender, EventArgs e)
    {
        int returnId;
        returnId = 1;
        if (returnId == 0)
        { }
        else
            ScriptManager.RegisterStartupScript(this, typeof(Page), "MsgSave",
                                                "alert('save value exists');", true);
    }
    protected void Update_Click1(object sender, EventArgs e)
    {
        int returnId;
        returnId = 2;
        if (returnId == 0)
        { }
        else
            ScriptManager.RegisterStartupScript(this, typeof(Page), "MsgUpdate",
                                                "alert('update value exists');", true);
    }

May be this will help you.

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