Repeater控件的使用
今天学习了Repeater控件的使用,Repeater控件没有定义样式,要自己编写HTML来定义样式,这样工作量可能会大一些,但它所带来的好处是可以随心所欲地定义自己数据的样式,数据源用了AccessDataSource控件,通过id传值到Default2.aspx页面来显示在Default.aspx点击记录的详细信息,详细信息显示在一个gridview控件上
Default.aspx
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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 id="Head1" runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource1">
13 <%--自定义头模板--%>
14 <HeaderTemplate>
15 <table>
16 <tr>
17 <td>
18 name
19 </td>
20 <td>
21 title
22 </td>
23 </tr>
24 </HeaderTemplate>
25 <%--自定义项模板--%>
26 <ItemTemplate>
27 <tr bgcolor="#33ff66">
28 <td>
29 <%--绑定name字段--%>
30 <a href='Default2.aspx?id=<%#Eval("id") %>'><%#Eval("name") %></a>
31 </td>
32 <td>
33 <%--绑定title字段--%>
34 <%#Eval("title") %>
35 </td>
36 </tr>
37 </ItemTemplate>
38 <%--自定义交替项模板--%>
39 <AlternatingItemTemplate>
40 <tr bgcolor="#ff9900">
41 <td >
42 <font color="red">
43 <a href='Default1.aspx?id=<%#Eval("id") %>'><%#Eval("name") %></a>
44 </font>
45 </td>
46 <td>
47 <font color="red">
48 <%#Eval("title") %>
49 </font>
50 </td>
51 </tr>
52 </AlternatingItemTemplate>
53 <%--自定义分隔符模板--%>
54 <SeparatorTemplate>
55 <tr>
56 <td colspan="2">
57 <hr size="1pt" />
58 </td>
59 </tr>
60 </SeparatorTemplate>
61 <%--自定义脚模板--%>
62 <FooterTemplate>
63 <tr>
64 <td colspan="2">
65 footerTemplate
66 </td>
67 </tr>
68 </table>
69 </FooterTemplate>
70 </asp:Repeater>
71 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/student.mdb"
72 SelectCommand="SELECT * FROM [message]"></asp:AccessDataSource>
73
74 </div>
75 </form>
76</body>
77</html>
78
79
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 id="Head1" runat="server">
7 <title>Untitled Page</title>
8</head>
9<body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource1">
13 <%--自定义头模板--%>
14 <HeaderTemplate>
15 <table>
16 <tr>
17 <td>
18 name
19 </td>
20 <td>
21 title
22 </td>
23 </tr>
24 </HeaderTemplate>
25 <%--自定义项模板--%>
26 <ItemTemplate>
27 <tr bgcolor="#33ff66">
28 <td>
29 <%--绑定name字段--%>
30 <a href='Default2.aspx?id=<%#Eval("id") %>'><%#Eval("name") %></a>
31 </td>
32 <td>
33 <%--绑定title字段--%>
34 <%#Eval("title") %>
35 </td>
36 </tr>
37 </ItemTemplate>
38 <%--自定义交替项模板--%>
39 <AlternatingItemTemplate>
40 <tr bgcolor="#ff9900">
41 <td >
42 <font color="red">
43 <a href='Default1.aspx?id=<%#Eval("id") %>'><%#Eval("name") %></a>
44 </font>
45 </td>
46 <td>
47 <font color="red">
48 <%#Eval("title") %>
49 </font>
50 </td>
51 </tr>
52 </AlternatingItemTemplate>
53 <%--自定义分隔符模板--%>
54 <SeparatorTemplate>
55 <tr>
56 <td colspan="2">
57 <hr size="1pt" />
58 </td>
59 </tr>
60 </SeparatorTemplate>
61 <%--自定义脚模板--%>
62 <FooterTemplate>
63 <tr>
64 <td colspan="2">
65 footerTemplate
66 </td>
67 </tr>
68 </table>
69 </FooterTemplate>
70 </asp:Repeater>
71 <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/student.mdb"
72 SelectCommand="SELECT * FROM [message]"></asp:AccessDataSource>
73
74 </div>
75 </form>
76</body>
77</html>
78
79
Default2.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.OleDb;
12
13public partial class Default2 : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 string connectionString = "provider=microsoft.jet.oledb.4.0;data source=D:/WebSites/Repeater1/App_Data/student.mdb;";
18 OleDbConnection myConnection = new OleDbConnection(connectionString);
19 int id = Int32.Parse(Request.QueryString["id"].ToString());//这里改成int id = Int32.Parse(Request.QueryString["id"]);就会出错,想不明白???
20 string queryString = "select * from message where id=" + id + "";
21 OleDbCommand myCommand = new OleDbCommand(queryString,myConnection);
22 myConnection.Open();
23 OleDbDataReader dr = myCommand.ExecuteReader();
24 GridView1.DataSource = dr;
25 GridView1.DataBind();
26 }
27}
28
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.OleDb;
12
13public partial class Default2 : System.Web.UI.Page
14{
15 protected void Page_Load(object sender, EventArgs e)
16 {
17 string connectionString = "provider=microsoft.jet.oledb.4.0;data source=D:/WebSites/Repeater1/App_Data/student.mdb;";
18 OleDbConnection myConnection = new OleDbConnection(connectionString);
19 int id = Int32.Parse(Request.QueryString["id"].ToString());//这里改成int id = Int32.Parse(Request.QueryString["id"]);就会出错,想不明白???
20 string queryString = "select * from message where id=" + id + "";
21 OleDbCommand myCommand = new OleDbCommand(queryString,myConnection);
22 myConnection.Open();
23 OleDbDataReader dr = myCommand.ExecuteReader();
24 GridView1.DataSource = dr;
25 GridView1.DataBind();
26 }
27}
28