.Net Project/ASP.NET 3.5 Sp1
13-4장 ASP.NET 이용하여 게시판 만들기(WebDelete)
Magic_kit
2009. 10. 8. 01:06
- 리스트 목록 List.aspx 에서 목록 선택하여 다음과 같이 이동 가능 하며,
- 이동 후에 수정 페이지 Modify.aspx 이동 하고, 삭제 Delete.aspx 로 페이지 이동 하면
- 다음과 같은 화면이 출력 "1번 글을 삭제 하시겠습니까??"
- 암호 텍스트 박스 입력후 암호 일치하면 삭제 완료 되며,
- 암호가 일치(불일치)할 경우에는 Lavel에 "패스워드가 일치 않습니다"라는 메시지 출력 |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Delete.aspx.cs"
Inherits="Basic_Delete" %>
<!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>글 삭제: Delete</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblNum" runat="server" ForeColor="Red"></asp:Label>
번 글을 삭제 하겠습니까 <br />
암호 :
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:Button ID="btnDelete" runat="server" Text="삭제"
OnClientClick="return confirm('정말로삭제??');"
onclick="btnDelete_Click" />
<asp:Button ID="btnCancel" runat="server" Text="취소"
onclick="btnCancel_Click" />
<asp:Label ID="lblError" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html> |
Basic.Delete.aspx.Cs 이벤트 컨트롤 관련 모드 |
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class Basic_Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//넘겨져 온 번호 값 검사
if (String.IsNullOrEmpty(Request["Num"]))
{
Response.Write("잘못된 요청입니다");
Response.End();
}
else
{
//레이블에 넘겨져 온 번호 값 출력
this.lblNum.Text = Request["Num"];
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (IsPasswordCorrect())
{
SqlConnection con = new SqlConnection
(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("DeleteBasic", con);
cmd.CommandType = CommandType.StoredProcedure;
//파라미터:List.aspx에서 넘겨운 쿼리 스트링값
cmd.Parameters.AddWithValue("@Num", Request["Num"]);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
cmd.ExecuteNonQuery(); //삭제
//삭제 후 리스트로 이동
Response.Redirect("List.aspx");
}
else
{
this.lblError.Text = "암호가 틀립니다";
}
}
//번호와 암호가 맞으면 참을 반환, 그렇지 않으면 거짓을 반환
private bool IsPasswordCorrect()
{
bool result = false;
SqlConnection con = new SqlConnection
(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(
"Select *From Basic Where Num =
@Num And Password = @Password", con);
cmd.Parameters.AddWithValue("@Num", Request["Num"]);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) //데이터가 읽혀진다면, 번호와 암호가 맞음
{
result = true;
}
return result;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
//취소버튼 클릭시 뒤로(상세 보기 페이지로 이동)
Response.Redirect("View.aspx?Num=" + Request["Num"]);
}
} |