2014년 1월 16일 목요일

C#, ASP.NET 방명록 소스예제(C#,ASP.NET,닷넷 교육,구로디지털단지)

C#, ASP.NET 방명록 소스예제(C#,ASP.NET,닷넷 교육,구로디지털단지)






C#, ASP.NET 방명록 소스예제(C#,ASP.NET,닷넷 교육,구로디지털단지)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
namespace Visit
{
    public partial class _Default : System.Web.UI.Page
    {
        OleDbConnection conn;
        string ConStr = "Provider=MSDAORA;data source=ONJ;User ID=scott;Password=tiger";
        protected void Page_Load(object sender, EventArgs e)
        {
            // 페이지 맨 처음 실행 되었을 때 작용
            if (!Page.IsPostBack)
            {
                VisitorGrid_Bind();
            }
        }
        //저장
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsSave())
                {
                    conn = new OleDbConnection(ConStr);
                    conn.Open();

                    string sql = "insert into VisitorTable (VisitorNo,VisitorName,VisitorEmail,VisitorContent,VisitorDate) values (";
                    sql += "seq_visitor.nextval" + ",";
                    sql += "'" + txtName.Text + "'" + ",";
                    sql += "'" + txtEmail.Text + "'" + ",";
                    sql += "'" + txtContent.Text + "'" + ",";
                    sql += "sysdate)";
                    OleDbCommand myCommand = new OleDbCommand(sql);
                    myCommand.Connection = conn;
                    myCommand.ExecuteNonQuery();
                    conn.Close();
                    txtName.Text = "";
                    txtEmail.Text = "";
                    txtContent.Text = "";
                    VisitorGrid.PageIndex = 0;
                    VisitorGrid_Bind();
                }
                else
                {
                    txtName.Text = "";
                    txtEmail.Text = "";
                    txtContent.Text = "";
                    VisitorGrid.PageIndex = 0;
                    VisitorGrid_Bind();
                }
            }
            catch (Exception ex)
            {
                txtContent.Text = ex.ToString();
            }
        }
        // 이미 데이타베이스 들어간  값을 가져와서  그리드에 바인딩
        protected void VisitorGrid_Bind()
        {
            OleDbDataAdapter adapter = null;
            OleDbConnection conn;
           
            conn = new OleDbConnection(ConStr);
            conn.Open();
            string sqlc = "select * from VisitorTable ORDER BY VisitorNo DESC";
            adapter = new OleDbDataAdapter(sqlc, conn);
            DataSet ds = new DataSet();

            adapter.Fill(ds);

            VisitorGrid.DataSource = ds;
            VisitorGrid.DataBind();
            conn.Close();
        }
        //저장여부를 체크
        protected bool IsSave()
        {
            if (Request.Cookies["VisitorName"] == null)
            {
                Response.Cookies["VisitorName"].Value = txtName.Text;
                Response.Cookies["VisitorEmail"].Value = txtEmail.Text;
                Response.Cookies["VisitorContent"].Value = txtContent.Text;
                return true;
            }
            else if ((Request.Cookies["VisitorName"].Value == txtName.Text) && (Request.Cookies["VisitorEmail"].Value == txtEmail.Text) && (Request.Cookies["VisitorContent"].Value == txtContent.Text))
            {
                return false;
            }
            else
            {
                Response.Cookies["VisitorName"].Value = txtName.Text;
                Response.Cookies["VisitorEmail"].Value = txtEmail.Text;
                Response.Cookies["VisitorContent"].Value = txtContent.Text;
                return true;
            }
        }

        //페이징 처리
        protected void VisitorGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            VisitorGrid.PageIndex = e.NewPageIndex;
            VisitorGrid_Bind();
        }
    }
}





<%@ Page Title="홈 페이지" Language="C#" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="Visit._Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 66px;
        }
        .style2
        {
        }
        .style3
        {
            width: 54px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        방명록
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;
        <table border="1" cellpadding="0" cellspacing="0"
            style="width: 100%; height: 52px;">
            <tr>
                <td bgcolor="#669999" class="style1">
                    &nbsp;&nbsp; 이름 &nbsp;</td>
                <td class="style2">
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                        ControlToValidate="txtName" Display="None" ErrorMessage="이름"></asp:RequiredFieldValidator>
                </td>
                <td bgcolor="#669999" class="style3">
                   이메일&nbsp;</td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
                   
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
                        ControlToValidate="txtEmail" Display="None" ErrorMessage="이메일"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td bgcolor="#669999" class="style1">
&nbsp;&nbsp;&nbsp; 내용
                </td>
                <td class="style2" colspan="3">
                    <asp:TextBox ID="txtContent" runat="server" Height="138px" TextMode="MultiLine"
                        Width="512px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
                        ControlToValidate="txtContent" Display="None" ErrorMessage="내용"></asp:RequiredFieldValidator>
                </td>
            </tr>
        </table>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnSave" runat="server" Text="저장" onclick="btnSave_Click" />
        <br />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server"
            ShowMessageBox="True" ShowSummary="False" />
        <br />
        <asp:GridView ID="VisitorGrid" runat="server" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333" GridLines="None" Width="656px"
            onpageindexchanging="VisitorGrid_PageIndexChanging">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
            <Columns>
            <asp:boundField headerText="글번호" dataField="VisitorNo"/>
            <asp:boundField headerText="방문자 이름" dataField="VisitorName"/>
            <asp:boundField headerText="이메일" dataField="VisitorEmail"/>
            <asp:boundField headerText="글내용" dataField="VisitorContent"/>
            <asp:boundField headerText="작성일" dataField="VisitorDate"/>
            </Columns>
        </asp:GridView>
        <br />
        <br />
        <br />
   
    </div>
    </form>
</body>
</html>

댓글 없음:

댓글 쓰기