ASP.NET 데이타베이스 컨트롤 Reapeater
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex58_Repeater.aspx.cs" Inherits="Ex58_Repeater" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 415px">
Ex58_Repeater.aspx<br />
Repeater<br />
<br />
<asp:Repeater ID="Repeater1" runat="server">
<%--
아이템이 어떻게 보일지 정의 하는 구문
리피터가 가지는 모든 모습을 홍길동으로 보여 줄것이다.
--%>
<ItemTemplate>
<input type="checkbox" />
<%--데이터바인딩 표현식--%>
<%# DataBinder.Eval(Container.DataItem, "title") %><br />
</ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<a href="View.aspx">
<%# DataBinder.Eval(Container.DataItem, "title_id") %><br />
<%# DataBinder.Eval(Container.DataItem, "title") %><br />
</a>
</ItemTemplate>
</asp:Repeater>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;//**
public partial class Ex58_Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//데이터바인딩 -> 컨트롤 초기화 -> 한번만
if (!IsPostBack)
{
string conStr = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
//Response.Write(conStr);
SqlDataAdapter adapter = new SqlDataAdapter("select top 5 * from titles", conStr);
DataSet ds = new DataSet();
adapter.Fill(ds, "titles");//데이터소스 확보!!
//바인딩
CheckBoxList1.DataSource = ds.Tables["titles"];
CheckBoxList1.DataTextField = "title";
CheckBoxList1.DataValueField = "title_id";
CheckBoxList1.DataBind();
Repeater1.DataSource = ds.Tables["titles"];
Repeater1.DataBind();
Repeater2.DataSource = ds.Tables["titles"];
Repeater2.DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex59_Repeater.aspx.cs" Inherits="Ex59_Repeater" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Ex59_Repeater.aspx</h2>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<h2 style="text-decoration:underline;">도서목록</h2>
</HeaderTemplate>
<ItemTemplate>
<%--책 1권--%>
<table border="1" width="500" style="margin-bottom:5px;">
<tr>
<td rowspan="4" width="150" align="center">
<img src="images/Title-<%# Eval("title_id") %>.gif" />
</td>
<td width="350"><%# Eval("title_id") %></td>
</tr>
<tr>
<td>제목 : <%# Eval("title") %></td>
</tr>
<tr>
<td>가격 : <%# Eval("price") %></td>
</tr>
<tr>
<td>출간일 : <%# Eval("pubdate", "{0:yyyy년 M월 d일}") %></td>
</tr>
</table>
</table>
</ItemTemplate>
<FooterTemplate>
<hr />
© Copyright Test.com.
</FooterTemplate>
<SeparatorTemplate>
<hr style="border-top:1px dotted gray;" />
</SeparatorTemplate>
<AlternatingItemTemplate>
<%--책 1권--%>
<table border="1" width="500" style="margin-bottom:5px;" bgcolor="yellow">
<tr>
<td rowspan="4" width="150" align="center"><img src="images/Title-<%# Eval("title_id") %>.gif" /></td>
<td width="350"><%# Eval("title_id") %></td>
</tr>
<tr>
<td>제목 : <%# Eval("title") %></td>
</tr>
<tr>
<td>가격 : <%# Eval("price", "${0:N2}") %></td>
</tr>
<tr>
<td>출간일 : <%# Eval("pubdate","{0:yyyy년 M월 d일}") %></td>
</tr>
</table>
</AlternatingItemTemplate>
</asp:Repeater>
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;//**
public partial class Ex59_Repeater : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conStr = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter("select * from titles", conStr);
DataSet ds = new DataSet();
adapter.Fill(ds, "titles");
Repeater1.DataSource = ds.Tables["titles"];
Repeater1.DataBind();
}
}
}
Repeater 데이터 소스 선택으로 컨트롤로 연결 함