ASP.NET DataList, DataGrid 사용방법
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex61_DataList.aspx.cs" Inherits="Ex61_DataList" %>
<!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>
<br />
<asp:DataList ID="DataList3" runat="server" RepeatColumns="4" Width="600px">
<HeaderTemplate>
도서목록
</HeaderTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("title_id", "images/Title-{0}.gif") %>' />
<br />
<asp:Label ID="Label2" runat="server" Text='<%# Eval("title") %>'></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text='<%# Eval("price", "${0:N2}") %>'></asp:Label>
<br />
<asp:Label ID="Label4" runat="server" Text='<%# Eval("pubdate", "{0:yyyy-MM-dd}") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
<br />
<br />
<asp:DataList ID="DataList2" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" GridLines="Both" RepeatColumns="4">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("title") %>'></asp:Label>
</ItemTemplate>
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:DataList>
<br />
<br />
<br />
<asp:DataList ID="DataList1" runat="server" BackColor="White"
BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
GridLines="Horizontal">
<FooterStyle BackColor="White" ForeColor="#333333" />
<%--헤더에 대한 cs 적용--%>
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
<h2>도서목록</h2>
</HeaderTemplate>
<%--실제 데이터를 가져오는건 요놈임--%>
<ItemStyle BackColor="White" ForeColor="#333333" />
<ItemTemplate>
<%# Eval("title") %>
</ItemTemplate>
<SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
</asp:DataList>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<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;
public partial class Ex61_DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
pubsDataContext pubs = new pubsDataContext();
DataList1.DataSource = pubs.titles;
DataList1.DataBind();
DataList2.DataSource = pubs.titles;
DataList2.DataBind();
DataList3.DataSource = pubs.titles;
DataList3.DataBind();
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex62_DataList.aspx.cs" Inherits="Ex62_DataList" MaintainScrollPositionOnPostback="true" %>
<!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>
Ex62_DataList.aspx</h2>
<p>
</p>
<asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" GridLines="Both" onitemcommand="DataList1_ItemCommand"
oncancelcommand="DataList1_CancelCommand"
ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand"
onupdatecommand="DataList1_UpdateCommand" Width="500px">
<EditItemTemplate>
[<asp:Label ID="Label7" runat="server" Text='<%# Eval("seq") %>'></asp:Label>
]
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'
Width="80px"></asp:TextBox>
-
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("address") %>'
Width="80px"></asp:TextBox>
<asp:Button ID="Button3" runat="server" Text="수정하기" CommandName="update"
onclientclick="return confirm("수정하시겠습니까?");" />
<asp:Button ID="Button5" runat="server" CommandName="delete" Text="삭제하기"
onclientclick="return confirm("삭제 하시겠습니까?");" />
<asp:Button ID="Button4" runat="server" CommandName="cancel" Text="취소하기" />
</EditItemTemplate>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<HeaderTemplate>
[주소록]
</HeaderTemplate>
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<ItemTemplate>
[<asp:Label ID="Label1" runat="server" Text='<%# Eval("seq") %>'></asp:Label>
]
<asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
-
<asp:Label ID="Label3" runat="server" Text='<%# Eval("address") %>'></asp:Label>
<asp:Button ID="Button1" runat="server" Text="선택하기" CommandName="sel" />
<asp:Button ID="Button2" runat="server" CommandName="edit" Text="편집하기" />
</ItemTemplate>
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SelectedItemTemplate>
[<asp:Label ID="Label1" runat="server" Text='<%# Eval("seq") %>'></asp:Label>
]
<asp:Label ID="Label2" runat="server" Text='<%# Eval("name") %>'></asp:Label>
-
<asp:Label ID="Label3" runat="server" Text='<%# Eval("address") %>'></asp:Label>
<hr />
<asp:Label ID="Label4" runat="server" Text='<%# Eval("age") %>'></asp:Label>
<br />
<asp:Label ID="Label5" runat="server" Text='<%# Eval("email") %>'></asp:Label>
<br />
<asp:Label ID="Label6" runat="server" Text='<%# Eval("tel") %>'></asp:Label>
</SelectedItemTemplate>
</asp:DataList>
<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 Ex62_DataList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataListBind();
}
}
private void DataListBind()
{
ADONETDataContext ado = new ADONETDataContext();
DataList1.DataSource = ado.tblAddress;
DataList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
//15개의 모든 버튼이 이 메소드를 호출
//Response.Write("버튼 클릭");
//누가?
Button btn = sender as Button;
Response.Write(btn.ID);
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
//몇번째 버튼?
//Response.Write("클릭");
//해당 버튼이 있는 행의 인덱스
//Response.Write(e.Item.ItemIndex);
//Response.Write(e.CommandName);
//인덱스를 변경하고나면 반드시 DataBind()호출
if (e.CommandName == "sel")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
DataList1.EditItemIndex = -1;
DataListBind();//***
}
//else if (e.CommandName == "edit")
//{
// DataList1.EditItemIndex = e.Item.ItemIndex;
// DataList1.SelectedIndex = -1;
// DataListBind();
//}
//else if (e.CommandName == "update")
//{
//}
//else if (e.CommandName == "delete")
//{
//}
//else if (e.CommandName == "cancel")
//{
//}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
//버튼중에 e.CommandName = "update" 라고 되어 있는 버튼은 해당 이벤트를 추가로 발생
//e.Item에는 모든 정보가 들어있다.
TextBox txt1 = e.Item.FindControl("TextBox1") as TextBox;//이름
TextBox txt2 = e.Item.FindControl("TextBox2") as TextBox;//주소
Label seq = e.Item.FindControl("label7") as Label;
//업데이트 호출 프로시져
ADONETDataContext ado = new ADONETDataContext();
ado.upUpdateAddress(int.Parse(seq.Text), txt1.Text, txt2.Text);//수정끝
//편집모드 해제
DataList1.EditItemIndex = -1;
DataListBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.SelectedIndex = -1;
DataListBind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
Label seq = e.Item.FindControl("label7") as Label;
//업데이트 호출 프로시져
ADONETDataContext ado = new ADONETDataContext();
ado.upDeleteAddress(int.Parse(seq.Text));//수정끝
//편집모드 해제
DataList1.EditItemIndex = -1;
DataListBind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
//편집중이던 항목을 일반 항목으로 되돌리기
DataList1.EditItemIndex = -1;
DataListBind();
}
}
데이타 그리드 출력형식
첫번째 컬럼에 어떤 컬럼을 출력하시겠습니까?
컬럼단위로 출력 형식을 만든다.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex63_DataGrid.aspx.cs" Inherits="Ex63_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>
<asp:DataGrid ID="DataGrid1" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" AutoGenerateColumns="false">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" Mode="NumericPages" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundColumn DataField="title" HeaderText="도서명" />
<asp:BoundColumn DataField="price" DataFormatString="$ {0:N2}" HeaderText="가격" />
<asp:BoundColumn DataField="pubdate" HeaderText="출간일" DataFormatString="{0:yyyy-MM-dd}" />
</Columns>
</asp:DataGrid>
</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 Ex63_DataGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pubsDataContext pubs = new pubsDataContext();
DataGrid1.DataSource = pubs.titles;
DataGrid1.DataBind();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex64_DataGrid.aspx.cs" Inherits="Ex64_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>
Ex64_DataGrid.aspx</h2>
<br />
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" BackColor="#DEBA84" BorderColor="#DEBA84"
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
<Columns>
<asp:BoundColumn DataField="title_id" HeaderText="아이디"></asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="제목"></asp:BoundColumn>
<asp:BoundColumn DataField="price" HeaderText="가격"></asp:BoundColumn>
<asp:BoundColumn DataField="pubdate" HeaderText="출간일"></asp:BoundColumn>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" Mode="NumericPages" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [title_id], [title], [price], [pubdate] FROM [titles]">
</asp:SqlDataSource>
<br />
<br />
<asp:DataList ID="DataList1" runat="server" DataKeyField="title_id"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("title_id") %>' />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("title") %>'></asp:Label>
<br />
<br />
</ItemTemplate>
</asp:DataList>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex65_DataGrid.aspx.cs" Inherits="Ex65_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>
Ex65_DataGrid.aspx</h2>
<br />
카테고리 :
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="type" DataValueField="type"
AutoPostBack="True">
</asp:DropDownList>
<br />
<br />
<asp:DataGrid ID="DataGrid1" runat="server" BackColor="White"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataSourceID="SqlDataSource2" GridLines="Horizontal" AutoGenerateColumns="False">
<AlternatingItemStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundColumn DataField="title_id" HeaderText="아이디"></asp:BoundColumn>
<asp:BoundColumn DataField="title" HeaderText="제목"></asp:BoundColumn>
<asp:BoundColumn DataField="price" DataFormatString="$ {0:N2}" HeaderText="가격">
</asp:BoundColumn>
<asp:BoundColumn DataField="type" HeaderText="카테고리"></asp:BoundColumn>
<asp:BoundColumn DataField="pubdate" DataFormatString="{0:yyyy-MM-dd}"
HeaderText="출간일"></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="Right"
Mode="NumericPages" />
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [title_id], [title], [price], [pubdate], [type] FROM [titles] WHERE ([type] = @type)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="type"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT DISTINCT [type] FROM [titles] ORDER BY [type]">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex66_DataGrid.aspx.cs" Inherits="Ex66_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>
Ex66_DataGrid.aspx</h2>
<br />
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingItemStyle BackColor="White" />
<Columns>
<asp:TemplateColumn HeaderText="표지">
<ItemTemplate>
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("title_id", "images/Title-{0}.gif") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="title_id" HeaderText="아이디"></asp:BoundColumn>
<asp:HyperLinkColumn DataNavigateUrlField="title_id"
DataNavigateUrlFormatString="View.aspx?id={0}" DataTextField="title"
HeaderText="도서명"></asp:HyperLinkColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Select" HeaderText="확인"
Text="선택"></asp:ButtonColumn>
</Columns>
<EditItemStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#E3EAEB" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:DataGrid>
<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="Ex67_DataGrid.aspx.cs" Inherits="Ex67_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>
Ex67_DataGrid.aspx</h2>
<br />
<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlDataSource1"
onitemdatabound="DataGrid1_ItemDataBound" Width="600px">
<Columns>
<asp:BoundColumn DataField="title_id" HeaderText="아이디">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
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="가격">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
Width="70px" />
</asp:BoundColumn>
<asp:BoundColumn DataField="type" HeaderText="카테고리">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
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="출간일">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" />
<HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
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="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<ItemStyle ForeColor="#000066" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left"
Mode="NumericPages" />
<SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT * FROM [titles]"></asp:SqlDataSource>
<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 Ex67_DataGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
//행 1개가 만들어진 직후에 발생하는 이벤트
// - DataGrid에 행이 10개 -> 이 이벤트 10번 발생
//이벤트 역할
// - 방금 만들어진 행의 각 셀들과 그 안의 내용물을 접근 가능 해주는 역할
//방금 만들어진 2번째 셀의 책 제목에 접근해서.. 텍스트가 너무 길면.. 자르기 작업..
//e.Item : 방금 만들어진 행
//Response.Write(e.Item.ItemIndex + "<br />");
//Response.Write(e.Item.ItemType + "<br />");
//일반행 접근 -> 제목을 가지고 있었기때문..
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//제목 <- 바운드컬럼의 텍스트
//Response.Write(e.Item.Cells[1].Text + "<br />");
if (e.Item.Cells[1].Text.Length > 20)
{
e.Item.Cells[1].Text = e.Item.Cells[1].Text.Substring(0, 20) + "...";
}
//타입별로 구분 -> 배경색(tr)
if (e.Item.Cells[3].Text.Trim() == "business")
{
e.Item.BackColor = System.Drawing.Color.Yellow;
}
else if (e.Item.Cells[3].Text.Trim() == "mod_cook")
{
e.Item.BackColor = System.Drawing.Color.Blue;
}
else if (e.Item.Cells[3].Text.Trim() == "psychology")
{
e.Item.BackColor = System.Drawing.Color.Red;
}
}
}
}