开发者

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
    }

When hosting page for this user control calls ShowDetails(), I need to call some javascript.

I tri开发者_高级运维ed 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!


Actually 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜