简体   繁体   English

从ascx页面代码后面调用Javascript函数

[英]call a Javascript function from ascx page code behind

I am using the below client script to pass a value to a javascript function. 我正在使用以下客户端脚本将值传递给javascript函数。 It works fine in an aspx page but in an ascx page it, is not working. 它在aspx页中工作正常,但在ascx页中却无法工作。 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. 此外,它将为您包装脚本,因此无需添加script标签。

EDIT NOTE: I assume this function exists in your script somewhere: SetActiveTab(3); 编辑说明:我假设此函数存在于您的脚本中的某个位置: 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. 我有同样的问题,即Javascript不在html输出中。 Turns out if you are doing this from ascx (control) you have to pass reference to the control, in example Me . 事实证明,如果要从ascx(控件)执行此操作,则必须将引用传递给控件,​​例如Me

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

After that, all started to work! 在那之后,所有工作开始了! thanks Mark Schultheiss! 谢谢马克舒尔泰斯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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