转自:
http://www.cnblogs.com/Terrylee/archive/2006/11/01/ASPNET_AJAX_UpdatePanle_Part2.html
UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编写任何客户端脚本,只要在一个页面上添加几个UpdatePanel控件和一个ScriptManager控件就可以自动实现局部更新。通过本文来学习一下UpdatePanel其他的一些使用方法(第二篇)。
主要内容
1.用编程的方法控制UpdatePanel的更新
2.UpdatePanel的嵌套使用
3.同一页面上使用多个UpdatePanel
一.用编程的方法控制UpdatePanel的更新
对于UpdatePanel,我们也可以使用编程的方法来控制它的更新,可以通过ScriptManager的RegisterAsyncPostBackControl()方法注册一个异步提交的控件,并且调用UpdatePanel的Update()方法来让它更新。再次用我在前面的文章中用到的一个无聊的时间更新例子来看一下,有时候我觉得例子过于复杂更加不好说明白所要讲的内容,如下代码所示,注意Button1并不包含在UpdatePanel中:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script runat="server">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Label2.Text = DateTime.Now.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<head runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<title>Refreshing an UpdatePanel Programmatically</title>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</head>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Label ID="Label1" runat="server" Text="更新时间:"></asp:Label>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Label ID="Label2" runat="server" Text="Label" ForeColor="Red"></asp:Label><br/><br/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:UpdatePanel>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClick = "Button1_Click"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</html>
这时候不用多说,肯定是整页提交了,运行如下图所示:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ASPNETAJAX_009.gif)
再次修改上面的例子,使用ScriptManager的RegisterAsyncPostBackControl()注册Button1为一个异步提交控件,并且调用UpdatePanel的Update()方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script runat="server">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ScriptManager1.RegisterAsyncPostBackControl(Button1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Label2.Text = DateTime.Now.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.UpdatePanel1.Update();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<head runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<title>Refreshing an UpdatePanel Programmatically</title>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</head>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Label ID="Label1" runat="server" Text="更新时间:"></asp:Label>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Label ID="Label2" runat="server" Text="Label" ForeColor="Red"></asp:Label><br/><br/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:UpdatePanel>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClick = "Button1_Click"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</html>
这时候可以看到,已经是异步提交了:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ASPNETAJAX_010.gif)
二. UpdatePanel的嵌套使用
UpdatePanel还可以嵌套使用,即在一个UpdatePanel的ContentTemplate中还可以放入另一个UpdatePanel。当最外面的UpdatePanel被触发更新时,它里面的子UpdatePanel也随着更新,里面的UpdatePanel触发更新时,只更新它自己,而不会更新外层的UpdatePanel。看下面的例子:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script runat="server">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<head id="Head1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<title>UpdatePanelUpdateMode Example</title>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<style type="text/css">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
div.NestedPanel
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
position: relative;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
margin: 2% 5% 2% 5%;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
</style>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</head>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:ScriptManager ID="ScriptManager"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
runat="server" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:UpdatePanel ID="OuterPanel"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UpdateMode="Conditional"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fieldset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<legend>Outer Panel </legend>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Button ID="OPButton1"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Text="Outer Panel Button"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
runat="server" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Last updated on
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<%= DateTime.Now.ToString() %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:UpdatePanel ID="NestedPanel1"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UpdateMode="Conditional"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div class="NestedPanel">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<fieldset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<legend>Nested Panel 1</legend>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Last updated on
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<%= DateTime.Now.ToString() %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<br />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Button ID="NPButton1"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Text="Nested Panel 1 Button"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
runat="server" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</fieldset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:UpdatePanel>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</fieldset>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:UpdatePanel>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</html>
运行后如下:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ASPNETAJAX_012.gif)
三.同一页面上使用多个UpdatePanel
使用UpdatePanel的时候并没有限制在一个页面上用多少个UpdatePanel,所以我们可以为不同的需要局部更新的页面区域加上不同的UpdatePanel。由于UpdatePanel默认的UpdateMode是Always,如果页面上有一个局部更新被触发,则所有的UpdatePanel都将更新,这是我们不愿看到的,我们只需要UpdatePanel在它自己的触发器触发的时候更新就可以了,所以需要把UpdateMode设置为Conditional。
来看一下官方网站上提供的一个例子:包括两个UpdatePanel,其中一个用来用户输入而另一个则用来显示数据,每一个UpdatePanel的UpdateMode属性都设置为Conditional。当我们单击Cancel按钮时,只有用来用户输入的那个UpdatePanel刷新,当单击Insert按钮时,两个UpdatePanel都刷新。代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Import Namespace="System.Collections.Generic" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<head id="Head1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<title>Enter New Employees</title>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<script runat="server">![](https://www.cnblogs.com/Images/dot.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private List<Employee> EmployeeList;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load()
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeeList = new List<Employee>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeeList.Add(new Employee(1, "Jump", "Dan"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeeList.Add(new Employee(2, "Kirwan", "Yvette"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ViewState["EmployeeList"] = EmployeeList;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeeList = (List<Employee>)ViewState["EmployeeList"];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeesGridView.DataSource = EmployeeList;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeesGridView.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void InsertButton_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (String.IsNullOrEmpty(FirstNameTextBox.Text) ||
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String.IsNullOrEmpty(LastNameTextBox.Text))
{ return; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int employeeID = EmployeeList[EmployeeList.Count-1].EmployeeID + 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string lastName = Server.HtmlEncode(FirstNameTextBox.Text);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string firstName = Server.HtmlEncode(LastNameTextBox.Text);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FirstNameTextBox.Text = String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LastNameTextBox.Text = String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeeList.Add(new Employee(employeeID, lastName, firstName));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ViewState["EmployeeList"] = EmployeeList;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeesGridView.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EmployeesGridView.PageIndex = EmployeesGridView.PageCount;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected void CancelButton_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
FirstNameTextBox.Text = String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LastNameTextBox.Text = String.Empty;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Serializable]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public class Employee
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private int _employeeID;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _lastName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private string _firstName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int EmployeeID
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _employeeID; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string LastName
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _lastName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string FirstName
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _firstName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Employee(int employeeID, string lastName, string firstName)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_employeeID = employeeID;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_lastName = lastName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_firstName = firstName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
</script>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</head>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td style="height: 206px" valign="top">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:UpdatePanel ID="InsertEmployeeUpdatePanel" runat="server" UpdateMode="Conditional">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<table cellpadding="2" border="0" style="background-color:#7C6F57">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td><asp:Label ID="FirstNameLabel" runat="server" AssociatedControlID="FirstNameTextBox"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Text="First Name" ForeColor="White" /></td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td><asp:TextBox runat="server" ID="FirstNameTextBox" /></td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td><asp:Label ID="LastNameLabel" runat="server" AssociatedControlID="LastNameTextBox"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Text="Last Name" ForeColor="White" /></td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td><asp:TextBox runat="server" ID="LastNameTextBox" /></td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td></td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:LinkButton ID="InsertButton" runat="server" Text="Insert" OnClick="InsertButton_Click" ForeColor="White" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:LinkButton ID="Cancelbutton" runat="server" Text="Cancel" OnClick="CancelButton_Click" ForeColor="White" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Label runat="server" ID="InputTimeLabel"><%=DateTime.Now %></asp:Label>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:UpdatePanel>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<td style="height: 206px" valign="top">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:UpdatePanel ID="EmployeesUpdatePanel" runat="server" UpdateMode="Conditional">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:GridView ID="EmployeesGridView" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" AutoGenerateColumns="False">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<FooterStyle BackColor="Tan" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<HeaderStyle BackColor="Tan" Font-Bold="True" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<AlternatingRowStyle BackColor="PaleGoldenrod" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Columns>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Columns>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<PagerSettings PageButtonCount="5" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:GridView>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Label runat="server" ID="ListTimeLabel"><%=DateTime.Now %></asp:Label>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</ContentTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Triggers>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:AsyncPostBackTrigger ControlID="InsertButton" EventName="Click" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Triggers>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:UpdatePanel>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</td>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</tr>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</table>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</html>
运行后效果如下:
示例代码下载:https://files.cnblogs.com/Terrylee/ASPNETAJAXUpdatePanelDemo2.rar