DataList来实现详细信息查询,以及内容的修改.
界面如图所示:
模板页眉
显示详细信息 编辑 Davolio Nancy

保存 取消 Fuller Andrew

显示详细信息 编辑 Leverling Janet

员工姓名: Peacock Margaret
生日:1937年9月19日
1937年9月19日 0:00:00
1937-9-19 0:00:00
9月19日
1937年9月19日
Sun, 19 Sep 1937 00:00:00 GMT
0:00:00
1937年9月18日 16:00:00
1937年9月

地址:4110 Old Redmond Rd.

显示详细信息 编辑 Buchanan Steven
模板页角

根据此图,以下为ASP代码:实现的是Datalist的绑定:(注意生日表达式格式的对应)
        <asp:DataList ID="DataList1" runat="server"
            Style
="z-index: 16; left: 47px; position: absolute; top: 59px" Width="306px" OnItemCommand="DataList1_ItemCommand" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand1" OnUpdateCommand="DataList1_UpdateCommand1">
            
<ItemTemplate>
            
<asp:LinkButton ID="LinkButtonShowDetails" runat="server" CommandName="select" 
                    
>显示详细信息</asp:LinkButton>
                
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="Edit">编辑</asp:LinkButton>
            
<%# DataBinder.Eval(Container.DataItem,"LastName"%>
                
            
<%# DataBinder.Eval(Container.DataItem,"FirstName"%>
            
</ItemTemplate>
            
<SelectedItemTemplate>
                员工姓名:      
<%# DataBinder.Eval(Container.DataItem,"LastName"%>
                       
<%# DataBinder.Eval(Container.DataItem,"FirstName"%>
     
                 
<br />
                生日:
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:D}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:F}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:G}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:M}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:D}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:R}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:T}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:U}"%>
                
<br />
                
<%# DataBinder.Eval(Container.DataItem,"BirthDate","{0:Y}"%>
                
<br />
                
                
            
<br />
            地址:
<%# DataBinder.Eval(Container.DataItem,"Address"%>
 
                
            
</SelectedItemTemplate>
            
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Size="18pt"
                Font-Strikeout
="False" Font-Underline="False" />
            
<AlternatingItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
                Font-Strikeout
="False" Font-Underline="False" ForeColor="#FF6666" />
            
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                Font-Underline
="False" ForeColor="#33CCFF" />
            
<HeaderTemplate>
                模板页眉
            
</HeaderTemplate>
            
<HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Size="18pt"
                Font-Strikeout
="False" Font-Underline="False" />
            
<SeparatorTemplate>
                
<hr id="HR1" />
            
</SeparatorTemplate>
            
<FooterTemplate>
                模板页角
            
</FooterTemplate>
            
<EditItemTemplate>
                
<asp:LinkButton ID="LinkButtonSave" runat="server" CommandName="update">保存</asp:LinkButton>
                
<asp:LinkButton ID="LinkButtonCancel" runat="server" CommandName="cancel" >取消</asp:LinkButton>
                
<%# DataBinder.Eval(Container.DataItem,"LastName"%>
                
<%# DataBinder.Eval(Container.DataItem,"FirstName"%>
                
<asp:TextBox ID="TextBoxCity" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"City") %>' ></asp:TextBox>
            
</EditItemTemplate>
        
</asp:DataList>
后台代码:
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;
using System.Data.SqlClient;

public partial class DataListTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            DataBindToList();
        }


    }

    
private void DataBindToList()
    
{
      
        
//得到数据库数据
        SqlConnection conn = DB.getConnection();
        
//得到数据适配器
        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand 
= new SqlCommand("select * from employees", conn);
        
//新建数据集合
        DataSet ds = new DataSet();
        
//将数据集合进行绑定
        sda.Fill(ds, "emp");
        
//用于保存主健,这样的话才能做到已主健更新
        DataList1.DataKeyField = "EmployeeID";
        DataList1.DataSource 
= ds.Tables["emp"];
        DataList1.DataBind();

    }

    
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    
{
        
//之所以要对LINKBUTTON设置COMMANDNAME的目的就是要进行判断
        if (e.CommandName == "select")
        
{
            
//DataList1.SelectedIndex设置为选中的项目
            DataList1.SelectedIndex = e.Item.ItemIndex;
            
//再重新进行数据绑定,绑定之后用到selectItemTmplee
            DataBindToList();
        }

    }

    
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    
{
        DataList1.EditItemIndex 
= e.Item.ItemIndex;
        DataBindToList();
    }




    
protected void DataList1_UpdateCommand1(object source, DataListCommandEventArgs e)
    
{
        
//得到这一行的主键的值
        String empID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
        
//找到ID 为TextBoxCity的控件,并把他的值找出来
        String city = ((TextBox)e.Item.FindControl("TextBoxCity")).Text;
        SqlConnection conn 
= DB.getConnection();
        conn.Open();
        SqlCommand cmd 
= new SqlCommand("update employees set city = '" + city + "' where employeeID = '" + empID + "'",conn);
        cmd.ExecuteNonQuery();
        DataList1.EditItemIndex 
= -1;
        DataBindToList();
    }

    
protected void DataList1_CancelCommand1(object source, DataListCommandEventArgs e)
    
{
        
//表示一个都没有选中
        DataList1.EditItemIndex = -1;
        DataBindToList();
    }

}