1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="test_DataBind_DataList" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml" >
6<head runat="server">
7 <title>无标题页</title>
8</head>
9<body style="text-align: center">
10 <form id="form1" runat="server">
11 <div>
12 </div>
13 <asp:Panel ID="Panel1" runat="server" Height="382px" Width="578px">
14 DataList 例子<br />
15 <br />
16 <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" OnEditCommand="DataList1_EditCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand">
17 <ItemTemplate>
18 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="select">查看信息</asp:LinkButton><asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit">编辑</asp:LinkButton><%# Eval("personname")%><%# Eval("personsex")%>
19 </ItemTemplate>
20 <AlternatingItemTemplate>
21 <font color=red>
22 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="select">查看信息</asp:LinkButton><asp:LinkButton ID="LinkButton2" runat="server" CommandName="edit">编辑</asp:LinkButton><%# Eval("personname")%><%# Eval("personsex")%></font>
23 </AlternatingItemTemplate>
24 <HeaderTemplate>
25 <span style="color: #3300cc"><strong>页眉
26 <hr />
27 </strong></span>
28 </HeaderTemplate>
29 <SelectedItemTemplate>
30 姓名:<%# Eval("personname")%>
31 <br />
32 姓别:<%# Eval("personsex")%>
33 </SelectedItemTemplate>
34 <FooterTemplate>
35 <strong><span style="color: #0000cc">页脚
36 <hr />
37 <span style="color: #3300cc"></span></span></strong>
38 </FooterTemplate>
39 <SeparatorTemplate>
40 <hr />
41
42 </SeparatorTemplate>
43 <EditItemTemplate>
44 <asp:LinkButton ID="Update" runat="server">确定</asp:LinkButton>
45 <asp:LinkButton ID="Cancel" runat="server">取消</asp:LinkButton>
46 姓名:<%# Eval("personname")%>
47 <asp:TextBox ID="personName" runat="server" Text='<%# Eval("personname") %>'></asp:TextBox>
48 </EditItemTemplate>
49 </asp:DataList></asp:Panel>
50 </form>
51</body>
52</html>
53
后台代码:
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Data.SqlClient;
12public partial class test_DataBind_DataList : System.Web.UI.Page
13{
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if(!this.IsPostBack)
17 {
18 this.BindToDataList();
19 }
20
21 }
22 public void BindToDataList()
23 {
24 SqlConnection con = DBtest.createCon();
25 SqlDataAdapter sdr = new SqlDataAdapter();
26 sdr.SelectCommand = new SqlCommand("select * from person", con);
27 DataSet ds = new DataSet();
28 sdr.Fill(ds, "emp"); //用数据适配器填充数据集.
29 this.DataList1.DataKeyField = "pID"; //更新时需要的主键
30 this.DataList1.DataSource = ds.Tables["emp"];
31 this.DataList1.DataBind();
32 }
33
34 protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
35 {
36 if (e.CommandName == "select")
37 {
38 this.DataList1.SelectedIndex = e.Item.ItemIndex; //指定选中项的索引
39 this.BindToDataList();
40 }
41 }
42 protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
43 {
44 this.DataList1.EditItemIndex = e.Item.ItemIndex; //指定选中项的索引
45 this.BindToDataList(); //注意这里没有判断 e.CommandName
46
47 }
48 protected void LinkButton3_Click(object sender, EventArgs e)
49 {
50
51 }
52 protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
53 {
54 this.DataList1.EditItemIndex = -1; //设置其索引号为-1,没选中.
55 // this.DataList1.SelectedIndex = -1;
56 this.BindToDataList();
57 }
58 protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
59 {
60 //获取主键
61 string personID = this.DataList1.DataKeys[e.Item.ItemIndex].ToString();
62 string personName = ((TextBox)e.Item.FindControl("personname")).Text;
63 SqlConnection con = DBtest.createCon();
64 con.Open();
65 SqlCommand cmd = new SqlCommand("update person set personname='"+personName+"'",con);
66 cmd.ExecuteNonQuery();
67 this.DataList1.EditItemIndex = -1; //更新好后取消选中
68 this.BindToDataList(); //再重新绑定
69
70 }
71}
72
为什么点击"取消"按钮无反应,哪里写错了????