页面使用Ajax控件,后台不能调用前台Javascript脚本弹出消息框的解决办法

如果使用Ajax控件实现无刷新弹出提示框,在后台写下面方法不能正常显示提示的内容

Response.Write("<script>alert('你好');</script>");

 

 

 

解决方法:

ScriptManager.RegisterStartupScript(UpdatePanel1,typeof(UpdatePanel),"Button2",

"<script>alert('你好');</script>", false);

其中第一个参数为要注册脚本的控件ID,试了一下,只要是本页面的就行。
第二个参数为注册脚本控件类型,是控件还是thisGetType()都可以,typeOf(string)也没问题.
第三个脚本函数的名字,随便起。
第四个是脚本内容。
第五个是标明是否再添加脚本标签,如果第四个参数里包含了<script></script>标签,此处则为false,否则为true

 

代码如下: (主要代码)

aspx.cs

    protected void Button1_Click(object sender, EventArgs e)

    {

        Response.Write("<script>alert('你好');</script>");

    }

    protected void Button2_Click(object sender, EventArgs e)

    {

        ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "Button2", "<script>alert('你好');</script>", false);

    }

}

 

 

aspx

<body>

    <form id="form1" runat="server">

        &nbsp;<div>

            <asp:ScriptManager ID="ScriptManager1" runat="server">

            </asp:ScriptManager>

            &nbsp;&nbsp;

            <asp:UpdatePanel ID="UpdatePanel1" runat="server">

                <ContentTemplate>

                    &nbsp;<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button1" />

            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

                </ContentTemplate>

            </asp:UpdatePanel>

        </div>

    </form>

</body>

posted @ 2010-10-13 11:37  王海龙(Heaven)  阅读(312)  评论(0编辑  收藏  举报