ASP控件第二篇-----UpdatePanel
首先是页面代码
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>//使用Ajax控件前一定要加上ScriptManager
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>//定义UpdatePanel的内容;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="局部刷新" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="整页刷新"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />//该按钮刷新UpdatePanel
<asp:PostBackTrigger ControlID="Button2" />//该按钮刷新整个页面
</Triggers>
</asp:UpdatePanel>
</form>
然后是C#的
<pre name="code" class="csharp"> protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString();
TextBox2.Text = DateTime.Now.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = DateTime.Now.ToString();
//UpdatePanel1.Update();//使用Update()方法一定要将UpdateMode改为Conditional,否则报错
}
实现效果
点击Button1时,只有UpdatePanel中TextBox1的值改变了
点击Button2时,两个Textbox的值都改变