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
}
When hosting page for this user control calls ShowDetails(), I need to call some javascript.
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.
I don't want to add UpdatePanel onto my control just for the sake of calling javascript.
Am I missing something?
Thank you!
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
}
When hosting page for this user control calls ShowDetails(), I need to call some javascript.
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.
I don't want to add UpdatePanel onto my control just for the sake of calling javascript.
Am I missing something?
Thank you!
Share Improve this question asked Apr 14, 2011 at 10:29 inutaninutan 10.9k29 gold badges89 silver badges129 bronze badges2 Answers
Reset to default 8Actually changing your code as following should work.
ScriptManager.RegisterStartupScript(this.Page, typeof(System.Web.UI.Page), "ShowEmailPreview", "alert('hi');", true);
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ScriptName", "alert('hi');", true);
this is a shorter version