아래 방명록 예제를 실습하세요.
프로젝트명 : visitor
웹폼파일명 : visitor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
namespace visitor
{
public partial class visitor : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 페이지 맨 처음 실행 되었을 때 작용
if (!Page.IsPostBack)
{
VisitorGrid_Bind();
}
}
//저장
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
//저장 여부를 체크
if (IsSave())
{
OleDbConnection conn;
string conStr = "Provider=MSDAORA;data source=ONJ;User ID=scott;Password=tiger";
conn = new OleDbConnection(conStr);
conn.Open();
OleDbCommand cmd = new OleDbCommand("insert into VisitorTable(VisitorNo, VisitorName,VisitorEmail,VisitorContent,VisitorDate) values(Visit_seq.nextval,'" + txtName.Text +
"','" + txtEmail.Text + "','" + txtContent.Text + "',sysdate)");
cmd.Connection = conn;
cmd.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;
string conStr = "Provider=MSDAORA;data source=ONJ;User ID=scott;Password=tiger";
conn = new OleDbConnection(conStr);
conn.Open();
string sqlString = "select * from VisitorTable ORDER BY VisitorNo DESC";
adapter = new OleDbDataAdapter(sqlString, 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 Language="C#" AutoEventWireup="true" CodeBehind="visitor.aspx.cs" Inherits="visitor.visitor" %>
<!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 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>
방명록
<br />
<br />
<table border="1" cellpadding="0" cellspacing="0"
style="width: 100%; height: 52px;">
<tr>
<td class="style1">
이름 </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 class="style3">
이메일 </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 class="style1">
내용
</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>
<br />
<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>
오라클자바커뮤니티 오프라인 교육센터, 개발자 전문교육, 개인80%환급
www.oraclejavacommunity.com
평일주간(9:30~18:20) 개강
(5/19)C#4.0,ADO.NET,Network 프로그래밍
(5/19)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(5/21)[교육전취업확정]Spring,MyBatis,XPlatform실무프로젝트과정
(5/26)SQL초보에서실전전문가까지
(5/26)안드로이드개발자과정
(5/26)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(5/27)JAVA,ORACLE 실무개발자과정
(5/30)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
평일야간(19:00~21:50) 개강
(5/16)C#,ASP.NET마스터
(5/19)웹퍼블리싱 마스터
(5/21)Spring3.X, MyBatis, Hibernate실무과정
(5/21)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(5/22)안드로이드개발자과정
(5/23)SQL초보에서실전전문가까지
(5/29)JAVA&WEB프레임워크실무과정
주말(10:00~17:50) 개강
(5/17)Spring3.X, MyBatis, Hibernate실무과정
(5/17)C#,ASP.NET마스터
(5/17)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(5/24)안드로이드개발자과정
(5/24)SQL초보에서실전전문가까지
(5/24)실무예제로 배워보는 jQuery(개발자/디자이너를위한)
주말저녁(18:30~22:20) 개강
(5/24)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
www.oraclejavacommunity.com
평일주간(9:30~18:20) 개강
(5/19)C#4.0,ADO.NET,Network 프로그래밍
(5/19)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(5/21)[교육전취업확정]Spring,MyBatis,XPlatform실무프로젝트과정
(5/26)SQL초보에서실전전문가까지
(5/26)안드로이드개발자과정
(5/26)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(5/27)JAVA,ORACLE 실무개발자과정
(5/30)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지
평일야간(19:00~21:50) 개강
(5/16)C#,ASP.NET마스터
(5/19)웹퍼블리싱 마스터
(5/21)Spring3.X, MyBatis, Hibernate실무과정
(5/21)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(5/22)안드로이드개발자과정
(5/23)SQL초보에서실전전문가까지
(5/29)JAVA&WEB프레임워크실무과정
주말(10:00~17:50) 개강
(5/17)Spring3.X, MyBatis, Hibernate실무과정
(5/17)C#,ASP.NET마스터
(5/17)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(5/24)안드로이드개발자과정
(5/24)SQL초보에서실전전문가까지
(5/24)실무예제로 배워보는 jQuery(개발자/디자이너를위한)
주말저녁(18:30~22:20) 개강
(5/24)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
댓글 없음:
댓글 쓰기