使用 C# 在用户控件中注册 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4760176/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Register javascript inside User Control using C#
提问by mehul9595
I want to call javascript function from User Control using C#. For that i am trying to use
我想使用 C# 从用户控件调用 javascript 函数。为此,我正在尝试使用
ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", "javascript:ShowPopup('Select a row to rate');", true); 
but it is not working for me. This works fine on the page. Can some one help me out how can i call javascript function at runtime using C#.
但这对我不起作用。这在页面上工作正常。有人可以帮助我如何在运行时使用 C# 调用 javascript 函数。
Thanks,
谢谢,
回答by zavaz
Try this.GetType() instead of typeof(string):
试试 this.GetType() 而不是 typeof(string):
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertbox", "ShowPopup('Select a row to rate');", true); 
回答by Tim Medora
The following is taken from working code, showing script being registered to fire from an asynchronous postback in an UpdatePanel.
以下内容取自工作代码,显示了注册脚本以从UpdatePanel.
ScriptManager.RegisterStartupScript( this.upnl, this.upnl.GetType(), Guid.NewGuid().ToString(), "alert('test');", true );
If your code is not executed from inside an UpdatePanel, it still should not be typeof(string); you should use the type of some container (typically the control itself).
如果您的代码不是从 an 内部执行UpdatePanel,它仍然不应该是typeof(string);您应该使用某个容器的类型(通常是控件本身)。
Type:The type of the client script block. This parameter is usually specified by using the typeof operator (C#) or the GetType operator (Visual Basic) to retrieve the type of the controlthat is registering the script.
类型:客户端脚本块的类型。此参数通常通过使用 typeof 运算符 (C#) 或 GetType 运算符 (Visual Basic) 来指定,以检索正在注册脚本的控件的类型。
回答by WraithNath
Im not sure if this is the best way to do it but for my user controls that use javascript i have a public string property on the user control and register it in the page.
我不确定这是否是最好的方法,但对于我使用 javascript 的用户控件,我在用户控件上有一个公共字符串属性并将其注册到页面中。
// sudo code
// 须藤代码
eg. UserControl
例如。用户控件
    {
   public bool CustomBool
   { 
     get
     {
       //logic 
       return value;
     }
    }
    public string Javascript
    {
        get { return "javascript...."; }
    }
  }
in page
在页面中
{
    page load()
    {
         if (Usercontrol.CustomBool)
         {
               ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox", UserControl.Javascript, true);
         }
    }
}
The downside for this is you have to remember to register the scripts on the page. it does work though
这样做的缺点是您必须记住在页面上注册脚本。虽然它确实有效
回答by blech
Try it without the "javascript:" in the script string:
尝试在脚本字符串中不包含“javascript:”:
ScriptManager.RegisterStartupScript(this, typeof(string), "alertbox",  "ShowPopup('Select a row to rate');", true); 
回答by Peter Taylor
I find that the string given is embedded literally, so it's necessary to enclose it in a suitabie <script type='text/javascript' language='javascript'>and </script>
我发现给定的字符串是按字面嵌入的,因此有必要将其括在适当的范围内,<script type='text/javascript' language='javascript'>并且</script>

