A simple BBS demo including(CRUD) - 3
6. Create a web form named "List" in the website.
List.aspx
==========
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="List" %>
<!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>List</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblBbs" Text="BBS DEMO" runat="server" BackColor="LightBlue" Width="506px"></asp:Label>
<asp:GridView ID="gridList" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="2"
ondatabound="gridList_DataBound" DataSourceID="SqlDataSource1"
onpageindexchanging="gridList_PageIndexChanging">
<AlternatingRowStyle BackColor="AliceBlue" />
<Columns>
<asp:TemplateField HeaderText="Number">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"><%#Eval("ID") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="lblTitle" Width="200px" runat="server"><%#Eval("Title").ToString().Length > 25 ?Eval("Titel").ToString().Substring(0,25) + "...":Eval("Title") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Writer">
<ItemTemplate>
<asp:Label ID="lblWriter" Width="100px" runat="server"><%#Eval("Writer") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="WirteDate">
<ItemTemplate>
<asp:Label ID="lblWriteDate" Width="100px" runat="server"><%#Eval("WriteDate","{00:yyyy-MM-dd}") %></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Read">
<ItemTemplate>
<asp:LinkButton ID="linkRead" Text="Read" runat="server" CommandName='<%#Eval("ID") %>'
oncommand="linkRead_Command"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings PageButtonCount="4" />
<PagerTemplate>
<table>
<tr>
<td>
<asp:Button ID="btnPrevList" runat="server" CommandName="Page" CausesValidation="false" Text="..." />
<asp:Button ID="btnFirst" runat="server" CommandName="Page" CommandArgument="First" CausesValidation="false" Text="1" />
<asp:Literal ID="lFirst" runat="server" Text="..."></asp:Literal>
<asp:Button ID="btnNum1" runat="server" CommandName="Page" CausesValidation="false" />
<asp:Button ID="btnNum2" runat="server" CommandName="Page" CausesValidation="false" />
<asp:Button ID="btnNum3" runat="server" CommandName="Page" CausesValidation="false" />
<asp:Button ID="btnNum4" runat="server" CommandName="Page" CausesValidation="false" />
<asp:Button ID="btnNum5" runat="server" CommandName="Page" CausesValidation="false" />
<asp:Literal ID="lLast" runat="server" Text="..."></asp:Literal>
<asp:Button ID="btnLast" runat="server" CommandName="Page" CommandArgument="Last" CausesValidation="false" />
<asp:Button ID="btnNextList" runat="server" CommandName="Page" CausesValidation="false" Text="..." />
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>' Font-Size="10" />
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="SEARCH" />
</td>
</tr>
<tr>
<td>
총<asp:Label ID="TotalPagesLabel" runat="server" Font-Bold="true"></asp:Label>페이지중
<asp:Label ID="PageNumberLabel" runat="server" Font-Bold="true"></asp:Label>페이지
</td>
</tr>
</table>
</PagerTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:BBSConnectionString %>"
SelectCommand="SELECT [id], [title], [writer], [writeDate] FROM [bbs]">
</asp:SqlDataSource>
<p><asp:Button ID="btnInsert" Text="Write" runat="server"
onclick="btnInsert_Click" /></p>
</div>
</form>
</body>
</html>
============================================================================================
List.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnInsert_Click(object sender, EventArgs e)
{
Response.Redirect("Insert.aspx");
}
protected void linkRead_Command(object sender, CommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandName);
Response.Redirect("Read.aspx?ID=" + id);
}
protected void gridList_DataBound(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
GridViewRow pagerRow = gv.BottomPagerRow;
Label pageNum = (Label)pagerRow.Cells[0].FindControl("PageNumberLabel");
Label totalNum = (Label)pagerRow.Cells[0].FindControl("TotalPagesLabel");
if ((pageNum != null) && (totalNum != null))
{
int page = gv.PageIndex + 1;
int count = gv.PageCount;
pageNum.Text = page.ToString();
totalNum.Text = count.ToString();
}
new DAL().CreateNumericPager(gv, pagerRow);
}
protected void gridList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = (GridView)sender; // refer to the GridView
int newPageIndex = 0;
if (-2 == e.NewPageIndex)
{
TextBox txtNewPageIndex = null;
GridViewRow pagerRow = theGrid.BottomPagerRow;
if (null != pagerRow)
{
txtNewPageIndex = (TextBox)pagerRow.FindControl("txtNewPageIndex");
}
if (null != txtNewPageIndex)
{
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
}
}
else
{
newPageIndex = e.NewPageIndex;
}
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
theGrid.PageIndex = newPageIndex;
}
}