ASP.NET Tutorial/ASP.net Controls/UpdatePanel
Содержание
- 1 applying partial rendering to a sample ASP.NET form
- 2 Pick up an important date
- 3 So [ <asp:Label ID="Label3" runat="server" BackColor="cyan" /> ] is important. Tell us why
- 4 Clock
- 5 RegisterAsyncPostBackControl method on the script manager class
- 6 <asp:Label runat="server" ID="Label1" Text="[write here]" />
- 7 <asp:Label runat="server" ID="Label2" Text="[write here::no update panel]" />
- 8 UpdatePanel control is defined dynamically but constructing the tree programmatically
applying partial rendering to a sample ASP.NET form
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script type="text/javascript"> </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>UpdatePanel in action</title>
</head> <body>
<form id="form1" runat="server"> <asp:ScriptManager runat="server" ID="ScriptManager1" />
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional"> <ContentTemplate> <tr> <td>
Pick up an important date
<asp:Calendar runat="server" ID="Calendar1" style="width:100%" OnSelectionChanged="Calendar1_SelectionChanged" VisibleDate="5/1/2007" /> <asp:TextBox runat="server" ID="TextBox1" />
<asp:Button runat="server" ID="Button1" Text="Save" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" /> </td> <td valign="top" style="border:solid 3px cyan;width:60%">
So [ <asp:Label ID="Label3" runat="server" BackColor="cyan" /> ] is important. Tell us why
<asp:TextBox runat="server" ID="TextBox2" TextMode="MultiLine" Columns="50" Rows="10" />
<asp:Button runat="server" ID="Button2" Text="Save" OnClick="Button2_Click" /> <asp:Label ID="Label2" runat="server" /> </td> </tr> </ContentTemplate> </asp:UpdatePanel>
</form>
</body> </html> File: Default.aspx.cs using System; using System.Web.Services; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) TextBox1.Text = Calendar1.VisibleDate.ToShortDateString(); } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "Picked up at " + DateTime.Now.ToShortTimeString(); Label3.Text = TextBox1.Text; } protected void Calendar1_SelectionChanged(object sender, EventArgs e) { TextBox1.Text = Calendar1.SelectedDate.ToShortDateString(); } protected void Button2_Click(object sender, EventArgs e) { Label2.Text = "Saved at " + DateTime.Now.ToShortTimeString(); }
}</source>
Clock
<source lang="csharp">
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs"
Inherits="Default" %>
<%-- <asp:ScriptManager runat="server" ID="ScriptManager1" /> --%>
<asp:UpdatePanel runat="server" ID="UpdatePanel1"> <ContentTemplate> <asp:Label runat="server" ID="Label1" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" /> </Triggers> </asp:UpdatePanel> <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000" />
File: Control.ascx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default : System.Web.UI.UserControl {
protected override void OnInit(EventArgs e) { base.OnInit(e); } protected void Timer1_Tick(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToLongTimeString(); }
}</source>
RegisterAsyncPostBackControl method on the script manager class
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Register Async Controls</title>
</head> <body>
<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate>
<asp:Label runat="server" ID="Label1" Text="[write here]" />
</ContentTemplate> </asp:UpdatePanel>
<asp:Label runat="server" ID="Label2" Text="[write here::no update panel]" />
<asp:Button runat="server" ID="Button1" Text="Refresh" OnClick="Button1_Click" />
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { ScriptManager1.RegisterAsyncPostBackControl(Button1); } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "Last update at: " + DateTime.Now.ToLongTimeString(); Label2.Text = "Last update at: " + DateTime.Now.ToLongTimeString(); }
}</source>
UpdatePanel control is defined dynamically but constructing the tree programmatically
<source lang="csharp">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Building Regions Dynamically</title>
</head> <body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
</form>
</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Drawing; using System.Web.UI; using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page {
private Label Label1; protected void Page_Load(object sender, EventArgs e) { UpdatePanel up1 = new UpdatePanel(); up1.ID = "UpdatePanel1"; Button button1 = new Button(); button1.ID = "Button1"; button1.Text = "What time is it?"; button1.Click += new EventHandler(Button1_Click); LiteralControl lit = new LiteralControl("
"); Label1 = new Label(); Label1.ID = "Label1"; Label1.Text = "[time]"; up1.ContentTemplateContainer.Controls.Add(button1); up1.ContentTemplateContainer.Controls.Add(lit); up1.ContentTemplateContainer.Controls.Add(Label1); this.Form.FindControl("pageContent").Controls.Add(up1); } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DateTime.Now.ToLongTimeString(); }
}</source>