页面使用Ajax控件,后台不能调用前…

来源:互联网 发布:天国王朝 演员 知乎 编辑:程序博客网 时间:2024/05/01 20:29

如果使用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页:

 

  1.    protected void Button1_Click(object sender, EventArgs e)
  2.    {
  3.       Response.Write("<script>alert('你好');</script>");
  4.    }
  5.    protected void Button2_Click(object sender, EventArgs e)
  6.    {
  7.       ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "Button2""<script>alert('你好');</script>"false);
  8.    }
  9. }

 

 

aspx页:

 

  1. <body>
  2.    <form id="form1" runat="server">
  3.        &nbsp;<div>
  4.           <asp:ScriptManager ID="ScriptManager1" runat="server">
  5.           </asp:ScriptManager>
  6.           &nbsp;&nbsp;
  7.           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  8.              <ContentTemplate>
  9.                  &nbsp;<asp:Button ID="Button2" runat="server" OnClick="Button2_Click"Text="Button1" />
  10.           <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
  11.                </ContentTemplate>
  12.           </asp:UpdatePanel>
  13.        </div>
  14.    </form>
  15. </body>
0 0
原创粉丝点击