【问题标题】:How to fire JS from code behind of a UserControl by an event如何通过事件从 UserControl 后面的代码中触发 JS
【发布时间】:2014-09-30 08:08:42
【问题描述】:

在 ASP.NET C# 中,我想从 UserControl 的代码隐藏中显示 ALERT('HI') 但不起作用:

用户控制:

<asp:UpdatePanel ID="updatepanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click();"/>
    </ContentTemplate>
</asp:UpdatePanel>

ASP.NET 页面

protected void btnSubmit_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, this.GetType(), "s1", "javascript:alert('hi!')", true);
}

更新:

忘了说ASP.NET页面本身就是“prettyPhoto”的弹窗。

【问题讨论】:

    标签: c# javascript asp.net updatepanel code-behind


    【解决方案1】:

    可能有以下可能的原因尝试更改这些并检查它是否有效

     1). ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('hi!')", true);//no need to use javascript with alert when bool set to true
    
     2). ScriptManager.RegisterStartupScript(page, page.GetType(), "msg", "alert('hi!')", true);
    
     3). ClientScript.RegisterClientScriptBlock(GetType(), "sas", "<script> alert('hi!');</script>", true);
    

    【讨论】:

    • 但是不明白这个和page有什么区别!?
    • @Kardo this 和 page here 的区别在于,有些时候脚本无法确定页面需要给 page 而不是 this
    猜你喜欢
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2012-09-19
    • 2015-07-28
    • 2013-12-14
    • 1970-01-01
    相关资源
    最近更新 更多