블로그 이미지
Magic_kit
study 관련자료를 한곳으로 자기 개발 목적으로 재태크 재무 관리 목적으로 일상생활의 팁을 공유 하기 위하여 블로그를 개설 하였습니다.

calendar

1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Category

Recent Post

Recent Comment

Archive

2009. 10. 8. 01:04 .Net Project/ASP.NET 3.5 Sp1
반응형
 Basic Modify.aspx 디자인 모드                                                                         



- 번호, 이메일, 홈페이지, 제목, 내용 의 내용을 비밀번호 입력 후 일치하면 다음과 같이 
   삭제 완료 됩니다.(비밀번호 일치 하지 않으면 "암호가 틀립니다" 라는 메시지 출력)

- 수정 버튼을 클릭하면 수정을 완료 할 수 있습니다. 

- 리스트 버튼 클릭시 (수정완료) 화면 List.aspx로 이동 하게 되어 있습니다. 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Modify.aspx.cs" Inherits="Basic_Modify" ValidateRequest="false" %>

<!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>수정 : Modify</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h3>글 수정</h3>
   
    번호
    <asp:Label ID="lblNum" runat="server" Text=""></asp:Label>
    
    이름:
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
  
    이메일:
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br />
  
    홈페이지 :
    <asp:TextBox ID="txtHomepage" runat="server"></asp:TextBox><br />
  
    제목 :
    <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox><br />
  
    내용 :
    <asp:TextBox ID="txtContent" runat="server"
     TextMode="MultiLine" Columns="20" Rows="5"></asp:TextBox><br />
   
     인코딩 : <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="btnWrite_Click" /> &nbsp
   
    <asp:Button ID="btnList" runat="server" Text="리스트"
            onclick="btnList_Click" />&nbsp;
  
    <asp:Label ID="lblError" runat="server" Text="Label" 
            ForeColor="Red"></asp:Label> <br />    
    </div>
    </form>
</body>
</html>




 Basic Modify.aspx.Cs 컨트롤 이벤트 모드  

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");
    }
}    





반응형
posted by Magic_kit