본문 바로가기
[ Web ]/ASP.NET

ASP.NET 2.0 게시판 만들기 2 (Write.aspx - 글쓰기 페이지)

by 관이119 2012. 9. 12.
사랑+이별=추억 | 소심비형
http://blog.naver.com/ngmaster/120045778247

이제 글쓰기 페이지를 만들어 봅시다.

우선 Visual Studio 2005 를 실행 시킨 후 솔루션 탐색기에 있는(자동 생성되는...) Default.aspx는 삭제하도록 합니다. 그런 후 아래 그림처럼 웹사이트에서 우클릭 후 새 항목 추가를 선택합니다.



"웹 구성 파일"(Web.Config)을 선택합니다.

아래처럼 connectionStrings를 추가합니다.

<configuration>
<appSettings/>
<connectionStrings>
<add name="conStr" connectionString="SERVER=localhost;DATABASE=BoardAN2;UID=sa"/>
</connectionStrings>

<system.web>

다시 웹 사이트에서 우클릭 > 새 항목 추가를 선택합니다.

Web Form 을 선택 후 이름을 Write.aspx를 입력한 후 확인을 눌러줍니다.

아래 그림처럼 페이지 디자인을 해줍니다.



html 부분 코드

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Write.aspx.cs" Inherits="Write" %>

<!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>
<table width="500">
<tr>
<td colspan="2">
<asp:Label ID="Label1" runat="server" Text="ASP.NET"></asp:Label></td>
<td colspan="2">
<asp:Label ID="Label2" runat="server" Text="자유게시판"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
아이디</td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
<td style="width: 100px">
별명</td>
<td style="width: 100px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="1">
제목</td>
<td colspan="3">
<asp:TextBox ID="TextBox3" runat="server" Width="350px"></asp:TextBox></td>
</tr>
<tr>
<td colspan="1">
내용</td>
<td colspan="3">
<asp:TextBox ID="TextBox4" runat="server" Rows="5" TextMode="MultiLine" Width="350px"></asp:TextBox></td>
</tr>
<tr>
<td align="center">파일
</td>
<td colspan="3">
<asp:FileUpload ID="FileUpload1" runat="server" Width="350px" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="목록보기" /></td>
<td align="center" colspan="2">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="저장하기" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>

대략 위와 같은 코드가 생성됩니다. 디자인이랄것도 없지만, 초보자용 설명인지라 위의 코드를 참고하고 디자인에서 속성을 이것 저것 건드려 보시면 될듯하군요..ㅎㅎ

이제 프로시저를 작성해 봅니다.

우선은 쿼리분석기를 띄우신 후 아래의 sql문을 적어줍니다. 그리고 F5를 해야겠죠..ㅎㅎ?

CREATE PROC dbo.UP_INSERT_BOARD
@mainCategory Varchar(100),
@subCategory Varchar(100),
@id Varchar(100),
@nickName Varchar(100),
@force Int,
@title Varchar(2000),
@upload Varchar(1000),
@content Text,
@ip Varchar(16)
AS
SET NOCOUNT ON
DECLARE @NewThread int
SELECT @NewThread = ISNULL(MAX(Thread),0) + 1000 FROM Board

INSERT INTO Board
([thread],[depth],[mainCategory],[subCategory],[id],[nickName],[force],[title],[upload],[content],[ip])
Values
(@NewThread,0,@mainCategory,@subCategory,@id,@nickName,@force,@title,@upload,@content,@ip)
GO

이제 MS-SQL Server EM을 실행 시킨 후 제대로 프로시저가 생성되었는지 확인해 보도록 합시다.

아래 그림처럼 제대로 생성이 되었나요?



그럼 다시 Visual Studio 2005로 돌아와서 이벤트 처리기를 생성합니다.

쓰기 버튼을 클릭했을 경우에 해당 디비에 저장이 되어야겠지요..-_-;;

디자인에서 "저장하기" 버튼을 더블클릭합니다. 그러면 이벤트 처리기가 자동으로 생성되면서, 코드비하인드(또는 코드사이드라고도... 누구말이 맞는지는..-_-)로 이동합니다.

아래의 코드를 입력하세요. 중요한 부분은 빨간 부분입니다..-_-;;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public partial class Write : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

}

protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ToString());
SqlCommand cmd = new SqlCommand("UP_INSERT_BOARD", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@mainCategory", SqlDbType.VarChar).Value = Label1.Text;
cmd.Parameters.Add("@subCategory", SqlDbType.VarChar).Value = Label2.Text;
cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@nickName", SqlDbType.VarChar).Value = TextBox2.Text;
cmd.Parameters.Add("@force", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = TextBox3.Text;
cmd.Parameters.Add("@upload", SqlDbType.VarChar).Value = FileUpload1.FileName;
cmd.Parameters.Add("@content", SqlDbType.Text).Value = TextBox4.Text;
cmd.Parameters.Add("@ip", SqlDbType.VarChar).Value = Request.UserHostAddress;

con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

}

이제 ctrl + F5를 눌러 실행시킨 후 실제로 글을 작성한 후 저장하기 버튼을 눌러봅시다.

별다른 행동은 없지만;; 아직 완성이 아니라서..-_-;;

SQL 서버를 실행시킨 후 해당 테이블을 보시면 데이터가 잘 들어가 있는것을 볼수 있을겁니다^^;


댓글