2012年3月10日土曜日

run javascript from server side by on button click that in updatepanel

You would do just what you have been doing. Since you are (I'm assuming) executing other server-side code with the button click, injecting the code this way is not harming anything.

You could also use ScriptManager.RegisterClientScriptBlock(), but it is still server-side code.


If you need to generate the code based on the server side event, you can inject it using the ScriptManager's methods. Like this:

protected void Save_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page,typeof(Page),"somecode", "alert('fdfd');",true);
}

If it's a generic message that doesn't require any information from the server side, you might consider placing it in an EndRequest handler instead. Something like:

<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);

function EndRequest(sender, args)
{
if (sender._postBackSettings.sourceElement.id == 'Save')
alert('fdfd');
}
</script>

0 件のコメント:

コメントを投稿