ASP.NET Tutorial/ASP.net Controls/UpdatePanel — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:30, 26 мая 2010

applying partial rendering to a sample ASP.NET form

<%@ 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" /> 
        <div id="pageContent">       
            <asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
                <ContentTemplate>
                        <tr>
                            <td>
                                <h2>Pick up an important date</h2>
            
                                        <asp:Calendar runat="server" ID="Calendar1" style="width:100%" 
                                            OnSelectionChanged="Calendar1_SelectionChanged" VisibleDate="5/1/2007" />
                                        <asp:TextBox runat="server" ID="TextBox1" />
                                    <br />
                                    <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%">
                                <h2>
                                    So [
                                    <asp:Label ID="Label3" runat="server" BackColor="cyan" />
                                    ] is important. Tell us why 
                                </h2>
                                <asp:TextBox runat="server" ID="TextBox2" 
                                        TextMode="MultiLine" Columns="50" Rows="10" />
                                <br />
                                <asp:Button runat="server" ID="Button2" Text="Save" OnClick="Button2_Click" />
                                <asp:Label ID="Label2" runat="server" />
                            </td>
                        </tr>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </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();
    }
}


Clock

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Control.ascx.cs" 
    Inherits="Default" %>
<div>
    <div style="background-color:#EFF3FB;margin:20px;border:solid 4px cyan;">
<%--     <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" />
    </div>
</div>
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(); 
    }
}


RegisterAsyncPostBackControl method on the script manager class

<%@ 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" />
        <div id="pageContent">
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <h2>
                    <asp:Label runat="server" ID="Label1" Text="[write here]" />
                    </h2>
                </ContentTemplate>
            </asp:UpdatePanel>
            <h2>
            <asp:Label runat="server" ID="Label2" Text="[write here::no update panel]" />
            </h2>
            <asp:Button runat="server" ID="Button1" Text="Refresh" OnClick="Button1_Click" />
        </div>
    </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();
    }
}


UpdatePanel control is defined dynamically but constructing the tree programmatically

<%@ 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">
        <div runat="server" id="pageContent">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
        </div>
    </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("<br>");
        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(); 
    }
}