【问题标题】:ASP.Net - Calling javascript in User Control server side methodASP.Net - 在用户控制服务器端方法中调用 javascript
【发布时间】:2011-08-05 10:25:35
【问题描述】:

我有一个带有公共方法的用户控件:

    public void ShowDetails(Guid requestGuid)
    {            
        Label1.Text = reportGuid.ToString(); //only for testing            

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "ShowEmailPreview", "alert('hi');", true);
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowEmailPreview", "alert('hi');", true); //doesn't work
        //Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowEmailPreview", "alert('hi');", true); //doesn't work
    }

当此用户控件的托管页面调用 ShowDetails() 时,我需要调用一些 javascript。

我尝试使用 ScriptManager.RegisterStartupScript 和 Page.ClientScript.RegisterStartupScript 但它不起作用...但是如果我在我的控件上添加一个 UpdatePanel 并为 UpdatePanel 添加脚本,如上所示,它运行良好。

我不想仅仅为了调用 javascript 而将 UpdatePanel 添加到我的控件中。

我错过了什么吗?

谢谢!

【问题讨论】:

    标签: javascript user-controls


    【解决方案1】:

    实际上更改您的代码如下应该可以工作。

    ScriptManager.RegisterStartupScript(this.Page, typeof(System.Web.UI.Page), "ShowEmailPreview", "alert('hi');", true);
    

    【讨论】:

    • 这个解决方案改变了我的一周。
    【解决方案2】:
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ScriptName", "alert('hi');", true);
    

    这是一个较短的版本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-30
      • 2011-03-21
      • 1970-01-01
      • 2011-05-26
      • 2011-08-23
      • 2013-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多