• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 919
  • Last Modified:

Call Client Side Javascript function from Codebehind ASP.NET

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.
  • 2
  • 2
1 Solution
Meir RivkinFull stack Software EngineerCommented:
so why from codebehind?
add onclick event, update your DB and alert from you client side.
Meir RivkinFull stack Software EngineerCommented:
another thing,  you first wanna update and only after success alert with the message not the other way around, cause update might fail.
ASPDEVAuthor Commented:

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.

Rajar AhmedConsultantCommented:
try this,
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        function displayMessage(msg) {
            return chk();
        function chk() {
            alert("Raised from Chk function");
    <form id="form1" runat="server">
    <asp:ScriptManager ID="asm" runat="server"></asp:ScriptManager>
            <asp:Button ID="Button1" runat="server" Text="Button" 
            onclick="Button1_Click" />

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

ASPDEVAuthor Commented:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now