简体   繁体   中英

call a Javascript function from ascx page code behind

I am using the below client script to pass a value to a javascript function. It works fine in an aspx page but in an ascx page it, is not working. Please help me to solve this.

ScriptManager.RegisterStartupScript(this, this.GetType(), "tabmvng", "<script language='javascript'>SetActiveTab(3); </script>", false);

试试这个,这都是因为UserControl,您不再需要处理Page。

 ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UpdatePanel), UniqueID, "myFunction('" + parameter + "');", true);

try the following

<script type='text/javascript'>
function SetActiveTab(a){
alert(a);
}
</script>

ScriptManager.RegisterStartupScript(this, this.GetType(), "tabmvng", "SetActiveTab(3);", true);
Control Caller = this; //user control
string MyScript= "SetActiveTab(3);";
ScriptManager.RegisterStartupScript(Caller, typeof(Caller), "Script Name", MyScript), true);

Get through Script manager having trouble adding scripts to the Page object from that user control use a reference to the calling user control. In addition, it will wrap the script for you so no need to add the script tag.

EDIT NOTE: I assume this function exists in your script somewhere: SetActiveTab(3);

ScriptManager.RegisterStartupScript(this.Page, typeof(System.Web.UI.Page), "javascript", "YourScript", true);

I had the same issue of the Javascript not being in the html output. Turns out if you are doing this from ascx (control) you have to pass reference to the control, in example Me .

ScriptManager.RegisterStartupScript(Me, GetType(Page), Guid.NewGuid().ToString(), jscript, True)

After that, all started to work! thanks Mark Schultheiss!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM