Blog Content

    티스토리 뷰

    05.ASP.NET - 기본형 게시판(6) : 수정(Modify) 페이지

    반응형
    Modify(수정) :글쓴 내용 수정하는 페이지

    Modify.aspx

    <div>

    <h3> 수정</h3>

       번호 :

       <asp:Label ID="lblNum" runat="server" Text=""/>

      

       이름 :

       <asp:TextBox ID="txtName" runat="server" /><br />

      

       이메일 :

       <asp:TextBox ID="txtEmail" runat="server" /><br />

      

       홈페이지 :

       <asp:TextBox ID="txtHomepage" runat="server" /><br />

      

       제목 :

       <asp:TextBox ID="txtTitle" runat="server" /><br />

      

       내용 :

       <asp:TextBox ID="txtContent" runat="server"

           TextMode="MultiLine" Columns="20" Rows="5"/><br />

          

       인코딩 :

       <asp:RadioButtonList ID="lstEncoding" runat="server"

           RepeatDirection="Horizontal" RepeatLayout="Flow">

          

           <asp:ListItem Selected="True">Text</asp:ListItem>

           <asp:ListItem>HTML</asp:ListItem>

           <asp:ListItem>Mixed</asp:ListItem>

          

       </asp:RadioButtonList><br />

      

       비밀번호 :

       <asp:TextBox ID="txtPassword" runat="server"

           TextMode="Password"></asp:TextBox><br />

          

       <asp:Button ID="btnModify" runat="server" Text="수정"

           onclick="btnModify_Click" />

          

       <asp:Button ID="btnList" runat="server" Text="리스트"

           onclick="btnList_Click" /><br />

          

       <asp:Label ID="lblError" runat="server" ForeColor="Red"/>

    </div>


    Modify.aspx.cs

    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()

    {

        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();

           string encoding = dr["Encoding"].ToString(); // Text/HTML/Mixed

          

           if (encoding == "HTML") // 입력한 모양 그래도 출력

           {

               this.lstEncoding.SelectedIndex = 1;

           }

     

           else // 태그는 실행하되, 엔터는 처리

           {

               this.lstEncoding.SelectedIndex = 2;

           }

       }

     

       dr.Close();

       con.Close();

    }

     

    protected void btnModify_Click(object sender, EventArgs e)

    {

       if (IsPasswordCorrect())

       {

           // Write.aspx 동일한 패턴

           // 커넥션

           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("@Content",

               txtContent.Text);

           objcmd.Parameters.AddWithValue("@ModifyIP",

               Request.UserHostAddress); //IP주소

           //objcmd.Parameters.AddWithValue("@ModifyDate", DateTime.Now);

           objcmd.Parameters.AddWithValue("@Password",

               txtPassword.Text);

           objcmd.Parameters.AddWithValue("@Encoding",

               lstEncoding.SelectedValue);

           objcmd.Parameters.AddWithValue("@Homepage",

               txtHomepage.Text);

           objcmd.Parameters.AddWithValue("@Num",

               lblNum.Text);

     

           objcon.Open();

           objcmd.ExecuteNonQuery();

           objcon.Close();

          

           // 상세보기로 가서 수정 확인

           Response.Redirect("View.aspx?Num=" + Request["Num"]);

       }

     

       else

       {

           lblError.Text = "암호가 틀립니다.";

       }

    }

     

    protected void btnList_Click(object sender, EventArgs e)

    {

       // 리스트 페이지로 이동

       Response.Redirect("List.aspx");

    }

     

    // 번호과 암호가 맞으면 , 그렇지 않으면 거짓

    private bool IsPasswordCorrect()

    {

       bool result = false;

       SqlConnection conn = new SqlConnection(

           ConfigurationManager.ConnectionStrings[

           "ConnectionString"].ConnectionString);

     

       conn.Open();

     

       SqlCommand cmd = new SqlCommand(

           "Select * From Basic Where Num = @Num And Password = @Password"

           , conn);

     

       // 파라미터 추가

       cmd.Parameters.AddWithValue("@Num", Request["Num"]);

       cmd.Parameters.AddWithValue("@Password", txtPassword.Text);

     

       SqlDataReader dr = cmd.ExecuteReader();

     

       while (dr.Read()) // 데이터가 읽혀진다면, 번호와 암호가 맞음

       {

           result = true;

       }

       return result;

    }


    결과확인

    [그림5-1]



    반응형

    Comments