javascript 用户控件回发事件中的 RegisterStartupScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8706578/
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
RegisterStartupScript in Usercontrol Postback Events
提问by Tails
I have created a static method to display a popup on my website from both pages and usercontrols. The method does not care if there is a script manager or update panel on the page.
我创建了一个静态方法来从页面和用户控件在我的网站上显示一个弹出窗口。该方法不关心页面上是否有脚本管理器或更新面板。
At the moment the method works correctly in:
目前该方法在以下情况下正常工作:
- Page Load
- Page PreRender
- Page Event Handlers (e.g. OnClick, OnSelectedIndexChanged, etc.)
- UserControl Load
- UserControl PreRender
- 页面加载
- 页面预渲染
- 页面事件处理程序(例如 OnClick、OnSelectedIndexChanged 等)
- 用户控制负载
- 用户控件预渲染
The method fails in:
该方法失败:
- UserControl Event Handlers (e.g. OnClick, OnSelectedIndexChanged, etc.)
- UserControl 事件处理程序(例如 OnClick、OnSelectedIndexChanged 等)
Neither of these lists are exhausted.
这些列表都没有穷尽。
The strangest part is that the method will work in the load and pre render events of the user control and fail in an OnClick event.
最奇怪的部分是该方法将在用户控件的加载和预渲染事件中工作,而在 OnClick 事件中失败。
Here is the static code:
这是静态代码:
private static Page CurrentPage
{
get
{
try
{
return (Page)HttpContext.Current.Handler;
}
catch
{
return null;
}
}
}
public static void ShowMessage(String Heading, String Message, String RedirectURL, Boolean AllowHTML)
{
if (CurrentPage != null)
{
try
{
String Script = "ShowCtMessagePopup('" + Heading + "', '" + Message + "', " + AllowHTML.ToString().ToLower() + ", " + (String.IsNullOrWhiteSpace(URL) ? "null" : "'" + URL + "'") + ");";
try
{
ScriptManager sm = ScriptManager.GetCurrent(CurrentPage);
if (sm == null || sm.IsInAsyncPostBack)
CurrentPage.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ErrorMessage", Script.ToString(), true);
else
CurrentPage.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ErrorMessage", "Sys.Application.add_load(function (){" + Script.ToString() + "});", true);
}
catch
{
//trouble finding script manager
CurrentPage.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "ErrorMessage", Script.ToString(), true);
}
}
catch
{
//ignore failed displays for now
}
}
}
Here is the Calling Code:
这是调用代码:
<asp:Button ID="btValidate" runat="server" OnClick="btValidate_Click" Text="Validate" Width="125px" CssClass="formButton" />
protected void btValidate_Click(object sender, EventArgs e)
{
CtMessagePopup.ShowMessage("Hello", "There", null, false);
}
回答by Tails
The solution to this issue is to use the following code:
这个问题的解决方案是使用以下代码:
Control Caller = this; //the user control that you are calling from
ScriptManager.RegisterStartupScript(Caller, typeof(Caller), "Script Name", Script.ToString(), true);
The Script manager has trouble adding scripts to the Page object from a user control and must have a reference to the calling user control.
脚本管理器无法从用户控件向 Page 对象添加脚本,并且必须具有对调用用户控件的引用。