ASP.NET how to call a button web control's OnClientClick event from code-behind (server-side)
Using ASP.NET 4.0 (c#),开发者_Python百科 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?
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);
}
Add a little JQuery to the page...
<head>
<!-- Add the jquery library to your page -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<!-- Document.ready runs when the page is loaded -->
<script type="text/javascript">
$(document).ready(function () {
$('#MyButtonID').click(); // Find the button and click it.
});
</script>
</head>
精彩评论