Ajax
前台:添加ScriptManager,UpdatePanel,在UpdatePanel中加入2个Label,2个TextBox,2个Button.
<form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" style="height: 21px" Text="Button" /> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> </div> </form>
后台
protected void Page_Load(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToString(); } protected void Button1_Click(object sender, EventArgs e) { TextBox1.Text = DateTime.Now.ToString(); } protected void Button2_Click(object sender, EventArgs e) { TextBox2.Text = DateTime.Now.ToString(); } }
可以观察到Button1单击完成局部更新(Label1没有变化),单击Button2后,Page_Load事件触发。