게시물 리스트에서 특정 게시물을 클릭하면 그 게시물의 상세 내용을 보여주는 컨트롤을 제작한다. 게시물을 보여주는 컨트롤인 ViewPost.ascx의 소스를 먼저 보자.
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="ViewPost.ascx.cs" Inherits="DotNetBBS.ViewPost" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<
asp:Panel
ID
="pnlViewPost"
Visible
=False
Runat
=server>
<
table
>
<
tr
class
="ViewPostSubject">
<
td
nowrap
>
제목:</td><tdwidth="100%"><asp:LabelID="lblSubject"Runat=server></asp:Label></td>
</
tr
>
<
tr
>
<
td
nowrap
>
글쓴이:</td><tdwidth="100%"><asp:LabelID="lblCreator"Runat=server></asp:Label></td>
</
tr
>
<
tr
>
<
td
nowrap
>
날짜:</td><tdwidth="100%"><asp:LabelID="lblCreateDate"Runat=server></asp:Label></td>
</
tr
>
<
tr
class
="ViewPostBodyText">
<
td
colspan
="2"><
asp:Label
ID
="lblBodyText"
Runat
=server></
asp:Label
></
td
>
</
tr
>
</
table
>
</
asp:Panel
>
게시물의 각 정보들을 Label 웹 컨트롤을 이용하여 표시한다. 코드를 보자.
private
void Page_Load(object sender, System.EventArgs e)
{
PostController pc = new PostController();
if(Session["CPostSystemID"] == null)
return;
pnlViewPost.Visible = true;
PostInfo pi = pc.Get((int)Session["CPostSystemID"]);
lblSubject.Text = pi.Subject;
lblCreator.Text = pi.CreatorName;
lblCreateDate.Text = pi.CreateDate.ToString();
lblBodyText.Text = pi.BodyText;
}
매우 간단 한데 세션부분을 자세히 보자. 게시물의 선택은 ListPost컨트롤에서 선택되므로 ViewPost컨트롤에서는 선택된 게시물을 알 방법이 없다. 선택된 게시물의 ID를 전달하는 방법으로는 QueryString으로 만들어서 전달하거나 쿠키를 사용하거나 세션을 사용하거나 하는 방법이 있다. 여기서는 세션을 이용하고 있다. ASP.NET에서 세션 사용법은 매우 간단해서 일반 변수를 사용하듯이 어떤 개체던지 넣고 빼고가 자유롭다. ViewPost에서 사용하는 세션변수는 아래의 ListPost코드에서 설정된 것이다.
ListPost.ascx
<
itemtemplate
>
<
asp:linkbutton
commandname
="ViewPost"
runat
="server">
<%# DataBinder.Eval(Container, "DataItem.Subject") %>
</
asp:LinkButton
>
</
itemtemplate
>
ListPost.ascx.cs
private
void PostList_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if (((LinkButton)e.CommandSource).CommandName == "ViewPost")
{
int PostSystemID = int.Parse(((System.Web.UI.DataBoundLiteralControl)(e.Item.Cells[0].Controls[0])).Text);
Session["CPostSystemID"] = PostSystemID;
Response.Redirect(Request.RawUrl);
}
}
DataController.cs에 게시물 정보를 DB에서 읽어 오는 Get 메서드를 구현한다.
public PostInfo Get(Int32 postSystemID)
{
SqlConnection myConnection = new SqlConnection(myConnString);
myConnection.Open();
string myQuery =
string.Format("select PostNo,Subject,BodyText,CreatorID,CreatorName,CreateDate from DNBBSPost " +
"where PostSystemID={0}", postSystemID);
PostInfo pi = new PostInfo();
SqlCommand myCommand = new SqlCommand(myQuery, myConnection);
SqlDataReader dr = myCommand.ExecuteReader();
while(dr.Read())
{
pi.PostNo = dr.GetInt32(0);
pi.Subject = dr.GetString(1);
pi.BodyText = dr.GetString(2);
pi.CreatorID = dr.GetInt32(3);
pi.CreatorName = dr.GetString(4);
pi.CreateDate = dr.GetDateTime(5);
}
dr.Close();
myConnection.Close();
return pi;
}
실행하면 다음과 같은 화면이 나오면 잘 된 것이다. 이제 게시물을 입력 하고 보고 지우는 게시판의 기본적인 기능이 모두 동작하는지 확인한다

프로젝트 소스: http://www.code99.net/tabid/842/ItemID/23/Default.aspx