1

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!

2 Answers 2

8

Actually changing your code as following should work.

ScriptManager.RegisterStartupScript(this.Page, typeof(System.Web.UI.Page), "ShowEmailPreview", "alert('hi');", true);
Sign up to request clarification or add additional context in comments.

1 Comment

This solution changed my week.
0
ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ScriptName", "alert('hi');", true);

this is a shorter version

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.