?
Solved

Call Client Side Javascript function from Codebehind ASP.NET

Posted on 2013-01-10
5
Medium Priority
?
914 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
  • 2
  • 2
5 Comments
 
LVL 42

Expert Comment

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

Expert Comment

by:Meir Rivkin
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 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