ASP.NET how to call a button web control’s OnClientClick event from code-behind (server-side)

asp.net

Using ASP.NET 4.0 (c#), I want to call a button's OnClientClick event from code-behind (server-side). How do I code that in the code-behind file so that when the page is rendered again, the OnClientClick event is fired like the user clicked the button?

Best Answer

Here are two ways to "call" the javascript alert function from code behind. You may be able to replace the alert function with your function:

public static void ShowAlert(Page page, String message)
{
    String Output;
    Output = String.Format("alert('{0}');",message);
    page.ClientScript.RegisterStartupScript(page.GetType(), "Key", Output, true);
}

public static void ShowAlert(HttpResponse response, String message)
{
    String Output;
    Output = String.Format("<Script Language='javascript'> alert('{0}');</script>", message);
    response.Write(Output);
}