Ruby
Ruby

Reputation: 969

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);
 }

Upvotes: 0

Views: 4988

Answers (8)

Vikash Singh
Vikash Singh

Reputation: 814

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.

Upvotes: 0

user2561316
user2561316

Reputation: 404

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#

Upvotes: 0

sohaiby
sohaiby

Reputation: 1198

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.

Upvotes: 1

Abhishek Punj
Abhishek Punj

Reputation: 289

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.

Upvotes: 1

Bhagavan
Bhagavan

Reputation: 301

Please try this, It may helps

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

Upvotes: 1

Karthik_SD
Karthik_SD

Reputation: 633

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

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

Upvotes: 0

Sid M
Sid M

Reputation: 4354

An alternative way for showing javascript alert popup could be this

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

Upvotes: 0

Christian Phillips
Christian Phillips

Reputation: 18749

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

Upvotes: 0

Related Questions