ASP.NET DataList, GridView, DetailsView 페이징 추가 방법
페이징 : 데이타리스트는 페이징이 없으며 데이타리스트 이후에 것들은 페이징이 존재 닷넷에서는 페이져라고 불리운다.
1페이지 2페이지 많은 데이터를 쪼개어 페이지로 보여줌.
GridView : DataGrid 개량 버젼 DataGrid를 MS에서 없애려고 하고 있음..
바운드필드 : 모든컬럼의 표현을 필드로 표현함
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex68_DataGrid.aspx.cs" Inherits="Ex68_DataGrid" %>
<!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>
Ex68_DataGrid.aspx</h2>
<h2>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" Width="86px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</h2>
<p>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</p>
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlDataSource1" Width="600px" AllowPaging="True"
ondatabinding="DataGrid1_DataBinding"
onpageindexchanged="DataGrid1_PageIndexChanged" PageSize="5">
<AlternatingItemStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundColumn DataField="title_id" HeaderText="아이디">
<HeaderStyle Width="60px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="제목">
<HeaderStyle Width="210px" />
</asp:BoundColumn>
<asp:BoundColumn DataField="price" DataFormatString="$ {0:N2}" HeaderText="가격">
<HeaderStyle Width="70px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:BoundColumn>
<asp:BoundColumn DataField="type" HeaderText="카테고리">
<HeaderStyle Width="80px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:BoundColumn>
<asp:BoundColumn DataField="pubdate" DataFormatString="{0:yyyy-MM-dd}"
HeaderText="출간일">
<HeaderStyle Width="80px" />
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
</asp:BoundColumn>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Center"
Mode="NumericPages" Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [titles]" ondatabinding="SqlDataSource1_DataBinding"></asp:SqlDataSource>
<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;
public partial class Ex68_DataGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataGrid1.DataBind();//
//Response.Write(DataGrid1.PageCount.ToString());
//Response.Write(DataGrid1.CurrentPageIndex.ToString());
ShowPage();
//DropDownList <- 페이저 구현
for (int i = 0; i < DataGrid1.PageCount; i++)
{
DropDownList1.Items.Add((i + 1).ToString());
}
}
}
private void ShowPage()
{
int totalPageCount = DataGrid1.PageCount;
int nowPage = DataGrid1.CurrentPageIndex;
Label1.Text = string.Format("{0} page / {1} pages", (nowPage + 1), totalPageCount);
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
//Pager의 페이지번호를 클릭하면 발생
//Response.Write(e.NewPageIndex);
//페이지 이동
//1. DataGrid가 현재 보여줘야할 페이지 인덱스를 대입
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//2. 데이터바인딩 -> 변경 내용이 적용
//this.DataBind();
DataGrid1.DataBind();
ShowPage();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
protected void DataGrid1_DataBinding(object sender, EventArgs e)
{
}
protected void SqlDataSource1_DataBinding(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//페이지 이동
int n = int.Parse(TextBox1.Text);//입력한 페이지
n--;
if (n >= 0 && n < DataGrid1.PageCount)
{
DataGrid1.CurrentPageIndex = n;
DataGrid1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int n = int.Parse(DropDownList1.SelectedItem.Value);
n--;
DataGrid1.CurrentPageIndex = n;
DataGrid1.DataBind();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex69_GridView.aspx.cs" Inherits="Ex69_GridView" %>
<!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>
Ex69_GridView.aspx</h2>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" DataKeyNames="title_id"
DataSourceID="SqlDataSource1" Width="600px" AllowPaging="True" AllowSorting="True"
onrowdatabound="GridView1_RowDataBound" PageSize="5">
<Columns>
<asp:ImageField DataAlternateTextField="title" DataImageUrlField="title_id"
DataImageUrlFormatString="images/Title-{0}.gif" HeaderText="표지"
SortExpression="title">
</asp:ImageField>
<asp:BoundField DataField="title_id" HeaderText="아이디" ReadOnly="True"
SortExpression="title_id" />
<asp:BoundField DataField="title" HeaderText="제목" SortExpression="title" />
<asp:BoundField DataField="price" HeaderText="가격" SortExpression="price"
DataFormatString="${0:N2}" />
<asp:BoundField DataField="type" HeaderText="타입" SortExpression="type" />
<asp:BoundField DataField="pubdate" HeaderText="출간일" SortExpression="pubdate"
DataFormatString="{0:yyyy-MM-dd}" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [titles]"></asp:SqlDataSource>
<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;
public partial class Ex69_GridView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//한행이 만들어질때마다 만들어지는 이벤트
//DataGrid -> ItemDataBound
//GridView -> RowDataBound
//1. 일반 행만 접근(헤더, 풋터 등.. 접근자)
//if(e.Item.ItemType == "")
if (e.Row.RowType == DataControlRowType.DataRow)
{
//일반행일경우
//2. 제목이 들어있는 필드를 찾기
//e.Row.Cells[1].Text get set 다가능함.
if (e.Row.Cells[1].Text.Length > 20)
{
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, 20) + "...";
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex70_DetailsView.aspx.cs" Inherits="Ex70_DetailsView" %>
<!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>
Ex70_DetailsView.aspx</h2>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True"
AutoGenerateRows="False" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
DataKeyNames="title_id" DataSourceID="SqlDataSource1" Height="50px"
Width="647px">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<Fields>
<asp:BoundField DataField="title_id" HeaderText="아이디" ReadOnly="True"
SortExpression="title_id" />
<asp:BoundField DataField="title" HeaderText="제목" SortExpression="title" />
<asp:BoundField DataField="type" HeaderText="타입" SortExpression="type" />
<asp:BoundField DataField="price" HeaderText="가격" SortExpression="price" />
<asp:BoundField DataField="pubdate" HeaderText="출간일" SortExpression="pubdate" />
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [titles]"></asp:SqlDataSource>
<br />
<br />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex71_DetailsView.aspx.cs" Inherits="Ex71_DetailsView" %>
<!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>
Ex71_DetailsView.aspx</h2>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource1" DataTextField="title" DataValueField="title_id">
</asp:DropDownList>
<br />
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataKeyNames="title_id" DataSourceID="SqlDataSource2"
GridLines="Horizontal" Height="50px" Width="648px">
<AlternatingRowStyle BackColor="#F7F7F7" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<Fields>
<asp:BoundField DataField="title_id" HeaderText="title_id" ReadOnly="True"
SortExpression="title_id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
<asp:BoundField DataField="pub_id" HeaderText="pub_id"
SortExpression="pub_id" />
<asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
<asp:BoundField DataField="advance" HeaderText="advance"
SortExpression="advance" />
<asp:BoundField DataField="royalty" HeaderText="royalty"
SortExpression="royalty" />
<asp:BoundField DataField="ytd_sales" HeaderText="ytd_sales"
SortExpression="ytd_sales" />
<asp:BoundField DataField="notes" HeaderText="notes" SortExpression="notes" />
<asp:BoundField DataField="pubdate" HeaderText="pubdate"
SortExpression="pubdate" />
</Fields>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [titles] WHERE ([title_id] = @title_id)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="title_id"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<br />
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [title_id], [title] FROM [titles] ORDER BY [title]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>