1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 using System.Data.SqlClient;//
 12 
 13 public partial class Basic_Modify : System.Web.UI.Page
 14 {
 15   protected void Page_Load(object sender, EventArgs e)
 16   {
 17         if (!Page.IsPostBack) // 처음 로드할 때에만 예전 자료 출력
 18         {
 19             DisplayData();    
 20         }
 21   }
 22     private void DisplayData() // 상세 패턴
 23     {
 24         string strSql = "ViewBasic";
 25         SqlConnection objCon = new SqlConnection();
 26         objCon.ConnectionString =
 27             ConfigurationManager.ConnectionStrings[
 28                 "ConnectionString"].ConnectionString;//DB연결문자열지정
 29         objCon.Open();
 30         SqlCommand objCmd = new SqlCommand();
 31         objCmd.Connection = objCon;
 32         objCmd.CommandText = strSql;// 
 33         objCmd.CommandType = CommandType.StoredProcedure;//
 34         objCmd.Parameters.AddWithValue("@Num", Request["Num"]);
 35         SqlDataReader objDr = objCmd.ExecuteReader();//내부적 Open()실행
 36         //[5] 바인딩
 37         if (objDr.Read()) { // 데이터가 있는 동안 반복 출력
 38             this.lblNum.Text = Request["Num"];
 39             this.txtName.Text = objDr[1].ToString();//이름
 40             this.txtEmail.Text = objDr.GetString(2);//이메일
 41             this.txtHomepage.Text = objDr["Homepage"].ToString();
 42             txtTitle.Text = objDr["Title"].ToString();
 43             txtContent.Text = objDr["Content"].ToString();
 44             string strEncoding = objDr["Encoding"].ToString();
 45             if (strEncoding == "Text") {
 46                 lstEncoding.Items[0].Selected = true;    
 47             }
 48             else if (strEncoding == "HTML") {
 49                 lstEncoding.Items[1].Selected = true;    
 50             }
 51             else {
 52                 lstEncoding.Items[2].Selected = true;    
 53             }
 54         }
 55         else {
 56             this.lblError.Text = "해당 자료가 없습니다.";
 57         }
 58         objDr.Close(); objCon.Close();
 59     }
 60     protected void btnModify_Click(object sender, EventArgs e)//입력
 61     {
 62         //[1] 변수 선언부
 63         string strName = txtName.Text;
 64         string strEmail = txtEmail.Text;
 65         string strTitle = txtTitle.Text;
 66         string strModifyIP = Request.UserHostAddress;//IP주소
 67         string strContent = txtContent.Text;
 68         string strEncoding = lstEncoding.SelectedValue;
 69         string strHomepage = txtHomepage.Text;
 70         string strPassword = txtPassword.Text;
 71         string strSql = "ModifyBasic";
 72         #region 커넥션과 커멘드
 73         //[2] 커넥션
 74         SqlConnection objCon = new SqlConnection();
 75         objCon.ConnectionString =
 76             ConfigurationManager.ConnectionStrings[
 77                 "ConnectionString"].ConnectionString;//DB연결문자열지정
 78         objCon.Open();
 79         //[3] 커멘드
 80         SqlCommand objCmd = new SqlCommand();
 81         objCmd.Connection = objCon;
 82         objCmd.CommandText = strSql;//
 83         objCmd.CommandType = CommandType.StoredProcedure;// 
 84         #endregion
 85         //[4] 파라미터 추가
 86         objCmd.Parameters.AddWithValue("@Name", strName);
 87         objCmd.Parameters.AddWithValue("@Email", strEmail);
 88         objCmd.Parameters.AddWithValue("@Title", strTitle);
 89         objCmd.Parameters.AddWithValue("@ModifyIP", strModifyIP);
 90         objCmd.Parameters.AddWithValue("@Content", strContent);
 91         objCmd.Parameters.AddWithValue("@Encoding", strEncoding);
 92         objCmd.Parameters.AddWithValue("@Homepage", strHomepage);
 93         objCmd.Parameters.AddWithValue("@Password", strPassword);
 94         objCmd.Parameters.AddWithValue("@Num", Request["Num"]);
 95         //[5] 실행 : 업데이트된 행의 값을 반환
 96         int result = objCmd.ExecuteNonQuery();
 97         //[6] 마무리
 98         objCon.Close();
 99         if (result > 0)
100         {
101             Response.Redirect("View.aspx?Num=" + Request["Num"]);            
102         }
103         else
104         {
105             this.lblError.Text = "암호가 틀립니다.";
106         }
107     }
108     protected void btnCancel_Click(object sender, EventArgs e)
109     {
110         Response.Redirect("View.aspx?Num=" + Request["Num"]);
111     }
112 }
113