ASP.Net - 在用户控制服务器端方法中调用javascript [英] ASP.Net - Calling javascript in User Control server side method
问题描述
我有一个带有公共方法的用户控件:
I have a user control with a public method:
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。
When hosting page for this user control calls ShowDetails(), I need to call some javascript.
我尝试使用ScriptManager.RegisterStartupScript和Page.ClientScript.RegisterStartupScript但它不起作用...但是如果我添加一个UpdatePanel在我的控件上并为UpdatePanel添加脚本,如上所示,它运行良好。
I tried with ScriptManager.RegisterStartupScript and Page.ClientScript.RegisterStartupScript but it doesn't work... However if I add an UpdatePanel on my control and add script for UpdatePanel as shown above, it works well.
我不想为了调用javascript而将UpdatePanel添加到我的控件中。
I don't want to add UpdatePanel onto my control just for the sake of calling javascript.
我错过了什么吗?
谢谢!
推荐答案
实际上如下所示更改代码应该有效。
Actually changing your code as following should work.
ScriptManager.RegisterStartupScript(this.Page, typeof(System.Web.UI.Page), "ShowEmailPreview", "alert('hi');", true);
这篇关于ASP.Net - 在用户控制服务器端方法中调用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!