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
Solved

Call Client Side Javascript function from Codebehind ASP.NET

Posted on 2013-01-10
5
904 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: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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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