This is a migrated thread and some comments may be shown as answers.

Calling a Javascript function after button onclick event

1 Answer 782 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Naho
Top achievements
Rank 1
Naho asked on 02 Sep 2010, 05:04 PM

Hi
I have a RadGrid and one of the columns is a button:

<telerik:GridTemplateColumn UniqueName="AgreeColumn">
         <ItemTemplate>
                 <asp:Button ID="AgreeButton" runat="server" onClick="AgreeButton_Click"  Text="Agree" />                            
          </ItemTemplate>
</telerik>

When the button is clicked AgreeButton_Click is fired and in the method, I do some databse updates. Only if the dabase updates are successfull, I want to call a Javascript function:

protected void AgreeButton_Click(object sender, EventArgs e)
    {
        // do some database work
        .....
        if (success)
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myJS", "myJSFunction();", true);
    }

For some reason, the Javascript function never gets called.

It works, however, if I create a button outside of grid:

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  
protected void Button1_Click(object sender, EventArgs e)
  
{
      // do some database work
        .....
    if (success)
  
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myJS", "myJSFunction();", true);
  
}

What do I have to do to make this work for button in the grid?

Thanks

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Sep 2010, 06:19 AM
Hello Naho,

One suggestion would be use ScriptManager.RegisterStartupScript instead of Page.ClientScript .

C#:
protected void AgreeButton_Click(object sender, EventArgs e)
  {
      ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "myJS", "myJSFunction();", true);
  }

Hope this helps,
Princy.
Tags
Grid
Asked by
Naho
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or