?
Solved

Call Client Side Javascript function from Codebehind ASP.NET

Posted on 2013-01-10
5
Medium Priority
?
909 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 1500 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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