DataList控件应用示例
DataListControl.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListControl.aspx.cs"
2 Inherits="DataListControl" %>
3
4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5<html xmlns="http://www.w3.org/1999/xhtml">
6<head runat="server">
7 <title>DataList控件应用示例</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:DataList ID="MyDataList" runat="server" CellPadding="0" ForeColor="#333333" OnDeleteCommand="MyDataList_DeleteCommand" OnItemCommand="MyDataList_ItemCommand">
13 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
14 <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
15 <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
16 <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
17 <HeaderTemplate>
18 <table border="1" cellpadding="1" cellspacing="1" style="font-size: small">
19 <tr>
20 <td style="width: 120px">
21 姓名</td>
22 <td style="width: 80px">
23 电话</td>
24 <td style="width: 180px">
25 地址</td>
26 <td style="width: 120px">
27 作者详情</td>
28 <td style="width: 60px">
29 </td>
30 </tr>
31 </table>
32 </HeaderTemplate>
33 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
34 <ItemTemplate>
35 <table border="1" cellpadding="1" cellspacing="1" style="font-size: small">
36 <tr>
37 <td style="width: 120px">
38 <%#DataBinder.Eval(Container.DataItem,"au_lname") %>
39 </td>
40 <td style="width: 80px">
41 <%#DataBinder.Eval(Container.DataItem,"phone") %>
42 </td>
43 <td style="width: 180px">
44 <%#DataBinder.Eval(Container.DataItem,"address") %>
45 </td>
46 <td style="width: 120px">
47 <asp:LinkButton ID="lbDetail" runat="server" Text="参看详情" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"au_id") %>' CommandName="BookDetail"></asp:LinkButton>
48 </td>
49 <td style="width: 60px">
50 <asp:LinkButton ID="lbDelete" runat="server" Text="删除" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"au_id") %>' CommandName="Delete"></asp:LinkButton>
51 </td>
52 </tr>
53 </table>
54 </ItemTemplate>
55 </asp:DataList></div>
56 </form>
57</body>
58</html>
59
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListControl.aspx.cs"
2 Inherits="DataListControl" %>
3
4<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5<html xmlns="http://www.w3.org/1999/xhtml">
6<head runat="server">
7 <title>DataList控件应用示例</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:DataList ID="MyDataList" runat="server" CellPadding="0" ForeColor="#333333" OnDeleteCommand="MyDataList_DeleteCommand" OnItemCommand="MyDataList_ItemCommand">
13 <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
14 <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
15 <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
16 <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
17 <HeaderTemplate>
18 <table border="1" cellpadding="1" cellspacing="1" style="font-size: small">
19 <tr>
20 <td style="width: 120px">
21 姓名</td>
22 <td style="width: 80px">
23 电话</td>
24 <td style="width: 180px">
25 地址</td>
26 <td style="width: 120px">
27 作者详情</td>
28 <td style="width: 60px">
29 </td>
30 </tr>
31 </table>
32 </HeaderTemplate>
33 <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
34 <ItemTemplate>
35 <table border="1" cellpadding="1" cellspacing="1" style="font-size: small">
36 <tr>
37 <td style="width: 120px">
38 <%#DataBinder.Eval(Container.DataItem,"au_lname") %>
39 </td>
40 <td style="width: 80px">
41 <%#DataBinder.Eval(Container.DataItem,"phone") %>
42 </td>
43 <td style="width: 180px">
44 <%#DataBinder.Eval(Container.DataItem,"address") %>
45 </td>
46 <td style="width: 120px">
47 <asp:LinkButton ID="lbDetail" runat="server" Text="参看详情" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"au_id") %>' CommandName="BookDetail"></asp:LinkButton>
48 </td>
49 <td style="width: 60px">
50 <asp:LinkButton ID="lbDelete" runat="server" Text="删除" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"au_id") %>' CommandName="Delete"></asp:LinkButton>
51 </td>
52 </tr>
53 </table>
54 </ItemTemplate>
55 </asp:DataList></div>
56 </form>
57</body>
58</html>
59
DataListControl.aspx.cs
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;
12
13public partial class DataListControl : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 SqlConnection myCon = new SqlConnection("server=sony;database=Pubs;User Id=sa;pwd=");
18 try
19 {
20 myCon.Open();
21 SqlCommand myCmd;
22 myCmd = new SqlCommand("select * from Authors", myCon);
23 SqlDataReader reader = myCmd.ExecuteReader();
24 MyDataList.DataSource = reader;
25 MyDataList.DataBind();
26 }
27 finally
28 {
29 myCon.Close();
30 }
31 }
32 protected void MyDataList_ItemCommand(object source, DataListCommandEventArgs e)
33 {
34 if (e.CommandName == "BookDetail")
35 {
36 string url;
37 url = "MyOrderDetail.aspx?OrderID="+e.CommandArgument.ToString();
38 Response.Redirect(url);
39 }
40 }
41 protected void MyDataList_DeleteCommand(object source, DataListCommandEventArgs e)
42 {
43 int intOrderID = int.Parse(e.CommandArgument.ToString());
44 //删除相应的行DeleteData(intOrderID);
45 //重新显示列表OrderDataBind();
46 }
47}
48
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;
12
13public partial class DataListControl : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 SqlConnection myCon = new SqlConnection("server=sony;database=Pubs;User Id=sa;pwd=");
18 try
19 {
20 myCon.Open();
21 SqlCommand myCmd;
22 myCmd = new SqlCommand("select * from Authors", myCon);
23 SqlDataReader reader = myCmd.ExecuteReader();
24 MyDataList.DataSource = reader;
25 MyDataList.DataBind();
26 }
27 finally
28 {
29 myCon.Close();
30 }
31 }
32 protected void MyDataList_ItemCommand(object source, DataListCommandEventArgs e)
33 {
34 if (e.CommandName == "BookDetail")
35 {
36 string url;
37 url = "MyOrderDetail.aspx?OrderID="+e.CommandArgument.ToString();
38 Response.Redirect(url);
39 }
40 }
41 protected void MyDataList_DeleteCommand(object source, DataListCommandEventArgs e)
42 {
43 int intOrderID = int.Parse(e.CommandArgument.ToString());
44 //删除相应的行DeleteData(intOrderID);
45 //重新显示列表OrderDataBind();
46 }
47}
48