C# 使用C在用户控件内注册javascript#
我想使用C#从用户控件调用javascript函数。为此,我正试图使用C# 使用C在用户控件内注册javascript#,c#,javascript,asp.net,user-controls,scriptmanager,C#,Javascript,Asp.net,User Controls,Scriptmanager,我想使用C#从用户控件调用javascript函数。为此,我正试图使用 ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", "javascript:ShowPopup('Select a row to rate');", true); 但这对我不起作用。这在页面上运行良好。有人能帮我解决一下如何在运行时使用C#调用javascript函数吗 谢谢,我不确定这是否是最好的方法,但对于使用javascript
ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", "javascript:ShowPopup('Select a row to rate');", true);
但这对我不起作用。这在页面上运行良好。有人能帮我解决一下如何在运行时使用C#调用javascript函数吗
谢谢,我不确定这是否是最好的方法,但对于使用javascript的用户控件,我在用户控件上有一个公共字符串属性,并在页面中注册它 //sudo代码 例如。 用户控制
{
public bool CustomBool
{
get
{
//logic
return value;
}
}
public string Javascript
{
get { return "javascript...."; }
}
}
第页
{
page load()
{
if (Usercontrol.CustomBool)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", UserControl.Javascript, true);
}
}
}
这样做的缺点是您必须记住在页面上注册脚本。它确实可以工作,但是请在脚本字符串中不使用“javascript:”的情况下尝试:
ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", "ShowPopup('Select a row to rate');", true);
尝试此.GetType()而不是typeof(字符串):
以下内容摘自工作代码,显示了在
UpdatePanel
中从异步回发中注册到激发的脚本
ScriptManager.RegisterStartupScript( this.upnl, this.upnl.GetType(), Guid.NewGuid().ToString(), "alert('test');", true );
如果您的代码不是从UpdatePanel
内部执行的,它仍然不应该是typeof(string)
;您应该使用某个容器的类型(通常是控件本身)
类型:客户端脚本块的类型。此参数为
通常使用typeof指定
运算符(C#)或GetType运算符
(Visual Basic)检索的类型
正在注册的控件
剧本
我发现给定的字符串是按字面意思嵌入的,因此有必要将其包含在suitabie
和
中,到底是什么不起作用?剧本不在那里吗?有错误吗?没有,没有错误只是不调用javascriptyes,这确实起到了作用,但我在特定的if-else条件下调用此警报,因此我正在使用public bool属性进行检查,以便在显示警报时有更多的解决方法需要将其设置为false:)我已略作修改,在用户controllast bool参数ScriptManager.RegisterStartupScript()中使用bool以使字符串包含标记
ScriptManager.RegisterStartupScript( this.upnl, this.upnl.GetType(), Guid.NewGuid().ToString(), "alert('test');", true );