using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class Basic_Modify : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//넘겨져 온 번호 값 검사
if (String.IsNullOrEmpty(Request["Num"]))
{
Response.Write("잘못된 요청입니다");
Response.End();
}
else
{
//반드시 수정페이지는 처음 로드시에만 이전 데이터 읽어오자
if (!Page.IsPostBack)
{
DisplayData(); //호출
}
}
}
private void DisplayData()
{
//VIew.aspx
SqlConnection con = new SqlConnection
(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("ViewBasic", con);
cmd.CommandType = CommandType.StoredProcedure;
//파라미터 추가 : List.aspx에서 넘겨온 쿼리 스트링 값
cmd.Parameters.AddWithValue("@Num", Request["Num"]);
//상세보기 :DataReader
SqlDataReader dr = cmd.ExecuteReader();
//바인딩 : 각각의 컨트롤
while (dr.Read())
{
this.lblNum.Text = dr["Num"].ToString();
this.txtName.Text = dr["Name"].ToString();
this.txtEmail.Text = dr["Email"].ToString();
this.txtHomepage.Text = dr["Homepage"].ToString();
this.txtTitle.Text = dr["Title"].ToString();
this.txtContent.Text = dr["Content"].ToString();
//인코딩에 따른 Content 출력 3가지 방법
string encoding = dr["Encoding"].ToString(); //Text/Html
if (encoding == "HTML")
{
this.lstEncoding.SelectedIndex = 1;
}
else if (encoding == "Mixed") //태그는 실행하되, 엔터는 처리
{
this.lstEncoding.SelectedIndex = 2;
}
}
dr.Close();
con.Close();
}
//수정
protected void btnWrite_Click(object sender, EventArgs e)
{
//Write.aspx 페이지와 동일한 패턴
if (IsPasswordCorrect())
{
SqlConnection objcon = new SqlConnection
(ConfigurationManager.ConnectionStrings
["ConnectionString"].ConnectionString);
SqlCommand objCmd = new SqlCommand("ModifyBasic", objcon);
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.AddWithValue("@Name", txtName.Text);
objCmd.Parameters.AddWithValue("@Email", txtEmail.Text);
objCmd.Parameters.AddWithValue("@Title", txtTitle.Text);
objCmd.Parameters.AddWithValue("@ModifyIP",
Request.UserHostAddress);
objCmd.Parameters.AddWithValue("@Content", txtContent.Text);
objCmd.Parameters.AddWithValue("@Encoding",
lstEncoding.SelectedValue);
objCmd.Parameters.AddWithValue("@Homepage", txtHomepage.Text);
objCmd.Parameters.AddWithValue("@Num", lblNum.Text);
objCmd.Parameters.AddWithValue("@Password", txtPassword.Text);
objcon.Open();
objCmd.ExecuteNonQuery();
objcon.Close();
Response.Redirect
("View.aspx?Num=" + Request["Num"]); //상세보기로 가서 수정 확인
}
else
{
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 btnList_Click(object sender, EventArgs e)
{
Response.Redirect("List.aspx");
}
} |