C# ScriptManager.RegisterClientScriptBlock 来自 MasterPage 的 UserControl
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19454865/
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
ScriptManager.RegisterClientScriptBlock from UserControl Of MasterPage
提问by Hiren
I have master page. In master page I have one UserControl. In this UserControl I have one button . When I complete everything with this button event I want to call Javascript function from page. Below is my code :
我有母版页。在母版页中,我有一个 UserControl。在这个 UserControl 中,我有一个按钮。当我用这个按钮事件完成所有事情时,我想从页面调用 Javascript 函数。下面是我的代码:
Button Event
按钮事件
protected void btnAddCompany_Click(object sender, EventArgs e)
{
// Do something Here
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), Guid.NewGuid().ToString(), "alertmessage();", true);
}
Javascript Function On same UserControl
同一用户控件上的 Javascript 函数
<script type="text/javascript">
function alertmessage() {
alert("Call Successfull");
}
</script>
But this javascript function never been called. When same UserControl is inside page it works. but if it is inside masterpage it doesn't.
但是这个 javascript 函数从来没有被调用过。当相同的 UserControl 位于页面内时,它可以工作。但如果它在母版页内,则不会。
采纳答案by Pradip Prajapati
The above code works. Make sure if you have user ScriptManager.RegisterStartupScrip anywhere other part of the page then you have terminate Javascript code with ;. If inline javascript fails, no other function is called.
上面的代码有效。确保您在页面的任何其他部分拥有用户 ScriptManager.RegisterStartupScrip,然后您已使用;终止 Javascript 代码。. 如果内联 javascript 失败,则不会调用其他函数。
Refer: ScriptManager.RegisterStartupScript code not working - why?