最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ASP.Net - Calling javascript in User Control server side method - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 8

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)

  1. 暂无评论