Solved

Call Client Side Javascript function from Codebehind ASP.NET

Posted on 2013-01-10
5
905 Views
Last Modified: 2013-01-10
Hello Experts,

I need to call a Client Side JS function on a button click, where the button click event will update the data in database and shows a message of success.liek this:

 ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('Record Updated Successfully.');", true);

Open in new window


After the alert message, I need to call a client side function.I tried this:
ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "return chk();", true);

Open in new window


It doesn't call the function.
0
Comment
Question by:ASPDEV
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38763505
so why from codebehind?
add onclick event, update your DB and alert from you client side.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38763516
another thing,  you first wanna update and only after success alert with the message not the other way around, cause update might fail.
0
 

Author Comment

by:ASPDEV
ID: 38763542
sedgwick,

The reason for codebehind is it does lot of validations and then insert into database. For this task, codebehind is better, only thing is I need to disable some controls if the database insert is success, and I have a button under updatepanel which triggers asynchronously.

Thanks.
0
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 500 total points
ID: 38763702
try this,
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function displayMessage(msg) {
            alert(msg);
            return chk();
        }
        function chk() {
            alert("Raised from Chk function");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="asm" runat="server"></asp:ScriptManager>
            <asp:Button ID="Button1" runat="server" Text="Button" 
            onclick="Button1_Click" />
            
    
    </div>
    </form>
</body>
</html>

Open in new window

    protected void Button1_Click(object sender, EventArgs e)
    {
        string custommessage = "Success";
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), Guid.NewGuid().ToString(), "displayMessage('" + custommessage + "');", true);
    }

Open in new window

0
 

Author Closing Comment

by:ASPDEV
ID: 38764963
Perfect!

Thanks,
ASPDEV
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question