ASP.NET 메모 예제
Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login"
Debug="true" %>
<!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>memo</title>
<style type="text/css">
body, div, p, span, input, td, table, a, textarea
{
font-family: 맑은 고딕;
}
#PanelMain, #PanelLog
{
border: 1px solid gray;
margin: 0px auto;
padding: 3px;
}
#PanelLog
{
margin: 5px;
}
input
{
border: 1px solid gray;
}
</style>
<script type="text/javascript">
function Check() {
if (event.keyCode == 13) {
event.srcElement.form.submit();
}
}
</script>
</head>
<body onload="document.getElementById('TextBoxPwd').focus();">
<form id="form1" runat="server">
<div>
<asp:Panel ID="PanelMain" runat="server" HorizontalAlign="Center" Width="800px">
<asp:Panel ID="PanelLog" runat="server">
<asp:TextBox ID="TextBoxPwd" runat="server" Width="50px" AutoPostBack="True" TextMode="Password" AccessKey="s" onkeydown="Check();"></asp:TextBox>
</asp:Panel>
</asp:Panel>
</div>
</form>
</body>
</html>
Login.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (TextBoxPwd.Text == "111")
{
Session["ing"] = "ok";
Response.Redirect("Memo.aspx");
}
PanelLog.Visible = true;
}
}
Memo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Memo.aspx.cs" Inherits="Memo"
Debug="true" Trace="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 id="Head1" runat="server">
<title>in memo</title>
<style type="text/css">
body, div, p, span, input, td, table, a, textarea {
font-family:맑은 고딕;
}
body
{
background: url(images/back.png);
}
#PanelMain, #PanelInput, #PanelList, #PanelOut
{
border: 1px solid gray;
margin: 0px auto;
padding: 3px;
}
#PanelInput, #PanelList, #PanelOut
{
margin: 5px;
background: white;
}
input, textarea
{
border: 1px solid gray;
}
#TextBoxInput
{
width: 500px;
height: 50px;
}
#ButtonInput
{
width: 50px;
height: 50px;
background: white;
}
.labelDate
{
font-size:11px;
}
.labelPanel
{
word-break:break-all;
}
.labelMemo
{
line-height:140%;
}
.inLink
{
color:#666666;
text-decoration:none;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function SendDelete() {
if (!confirm("delete?"))
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="PanelMain" runat="server" HorizontalAlign="Center" Width="800px">
<asp:Panel ID="PanelInput" runat="server">
<table style="margin: 0px auto;">
<tr>
<td>
<asp:DropDownList ID="DropDownListCategory" runat="server" Font-Bold="True" Font-Names="맑은 고딕" Font-Size="X-Large" ForeColor="Silver">
<asp:ListItem>메모</asp:ListItem>
<asp:ListItem>주소</asp:ListItem>
<asp:ListItem>살것</asp:ListItem>
<asp:ListItem>공부</asp:ListItem>
<asp:ListItem>취미</asp:ListItem>
<asp:ListItem>기억</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:TextBox ID="TextBoxInput" runat="server" AccessKey="s" TextMode="MultiLine"
Font-Names="맑은 고딕"></asp:TextBox>
</td>
<td>
<asp:Button ID="ButtonInput" runat="server" Text="in" OnClick="ButtonInput_Click" AccessKey="a" />
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="PanelList" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="seq"
DataSourceID="SqlDataSource1" HorizontalAlign="Center" OnRowDataBound="GridView1_RowDataBound"
ShowHeader="False" Width="700px" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%--<asp:Image ID="ImageItem" runat="server" />--%>
<asp:ImageButton ID="ImageItem" runat="server"
CommandArgument='<%# Eval("seq") %>' oncommand="ImageItem_Command" />
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="PanelMemo" runat="server" CssClass="labelPanel">
<div style="text-align:left;margin:20px 20px 0px 10px;">
<asp:Label ID="LabelMemo" runat="server" Font-Size="Small" ForeColor="#666666" CssClass="labelMemo"></asp:Label>
</div>
<div style="text-align:right;margin-top:10px;margin-right:10px;">
<asp:Label ID="LabelDate" runat="server"
ForeColor="#999999" CssClass="labelDate"></asp:Label>
</div>
</asp:Panel>
</ItemTemplate>
<ItemStyle Width="550px" HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandArgument='<%# Eval("seq") %>' OnCommand="ImageButtonDelete_Command" BorderColor="White" BorderWidth="0px"
ImageUrl="images/Unreadable.png" onclientclick="return SendDelete();" />
</ItemTemplate>
<ItemStyle Width="50px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:inConnectionString %>"
SelectCommand="SELECT * FROM [Memo] ORDER BY [regDate] DESC"></asp:SqlDataSource>
</asp:Panel>
<asp:Panel ID="PanelOut" runat="server" Font-Bold="True" Font-Names="맑은 고딕" Font-Size="X-Large"
ForeColor="Silver" HorizontalAlign="Right">
<asp:LinkButton ID="LinkButton1" runat="server" Font-Underline="False" ForeColor="Silver"
OnClick="LinkButton1_Click">나가기</asp:LinkButton></asp:Panel>
</asp:Panel>
</div>
<div style="font-size: 11px; text-align: center; color: Gray; margin: 12px;">
Copyrights © 2012 in All Rights reserved.
</div>
</form>
</body>
</html>
Memo.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
public partial class Memo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["ing"] == null || Session["ing"].ToString() != "ok")
{
Response.Write("<script>alert('block!!');history.back();</script>");
Response.End();
}
}
protected void ButtonInput_Click(object sender, EventArgs e)
{
if (TextBoxInput.Text.Trim() == "")
{
return;
}
try
{
SqlConnection con = new SqlConnection("server=localhost;database=ASPNET;uid=sa;pwd=zangna1");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (ButtonInput.Text == "in")
{
cmd.CommandText = "insert into Memo (memo, category) values (@memo, @category)";
}
else if (ButtonInput.Text == "edit")
{
cmd.CommandText = "update Memo set memo=@memo, category=@category where seq=@seq";
cmd.Parameters.Add(new SqlParameter("@seq", SqlDbType.Int));
cmd.Parameters["@seq"].Value = ButtonInput.Attributes["no"].ToString();
}
cmd.Parameters.Add(new SqlParameter("@memo", SqlDbType.VarChar, 4000));
cmd.Parameters.Add(new SqlParameter("@category", SqlDbType.VarChar, 50));
cmd.Parameters["@memo"].Value = TextBoxInput.Text;
cmd.Parameters["@category"].Value = DropDownListCategory.SelectedValue;
cmd.ExecuteNonQuery();
con.Close();
}
catch { }
finally
{
Response.Redirect("Memo.aspx");
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Cookies["ing"].Value = null;
Response.Redirect("Login.aspx");
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = e.Row.DataItem as DataRowView;
//메모
Label labelMemo = e.Row.FindControl("LabelMemo") as Label;
string memo = row["memo"].ToString().Replace("\r\n", "<br />");
Regex regex = new Regex(@"(mailto\:|(news|(ht|f)tp(s?))\://)(([^[:space:]]+)|([^[:space:]]+)( #([^#]+)#)?)");
foreach (Match m in regex.Matches(memo))
{
string temp = string.Format("<a href='{0}' target='_blank' class='inLink'>", m.Value);
memo = memo.Insert(m.Index + m.Value.Length, "</a>");
memo = memo.Insert(m.Index, temp);
}
labelMemo.Text = memo;
//날짜
Label labelDate = e.Row.FindControl("LabelDate") as Label;
labelDate.Text = row["regDate"].ToString();
//이미지
Image img = e.Row.FindControl("ImageItem") as Image;
img.ImageUrl = "images/" + GetIcon(row["category"].ToString());
}
}
private string GetIcon(string category)
{
switch (category)
{
case "메모":
return "memo.png";
case "주소":
return "address.png";
case "살것":
return "buy.png";
case "공부":
return "study.png";
case "취미":
return "hobby.png";
case "기억":
return "memory.png";
default:
return "empty.png";
}
}
protected void ImageButtonDelete_Command(object sender, CommandEventArgs e)
{
try
{
SqlConnection con = new SqlConnection("server=localhost;database=ASPNET;uid=sa;pwd=zangna1");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "delete from Memo where seq=@seq";
cmd.Parameters.Add(new SqlParameter("@seq", SqlDbType.Int));
cmd.Parameters["@seq"].Value = e.CommandArgument;
cmd.ExecuteNonQuery();
con.Close();
}
catch { }
finally
{
Response.Redirect("Memo.aspx");
}
}
protected void ImageItem_Command(object sender, CommandEventArgs e)
{
try
{
SqlConnection con = new SqlConnection("server=localhost;database=ASPNET;uid=sa;pwd=zangna1");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from Memo where seq=@seq";
cmd.Parameters.Add(new SqlParameter("@seq", SqlDbType.Int));
cmd.Parameters["@seq"].Value = e.CommandArgument;
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
DropDownListCategory.SelectedValue = reader["category"].ToString();
TextBoxInput.Text = reader["memo"].ToString();
}
reader.Close();
con.Close();
ButtonInput.Text = "edit";
ButtonInput.Attributes["no"] = e.CommandArgument.ToString();
}
catch { }
finally
{
}
}
}
web.config
<?xml version="1.0"?>
<!--
ASP.NET 응용 프로그램을 구성하는 방법에 대한 자세한 내용을 보려면
http://go.microsoft.com/fwlink/?LinkId=169433 을 방문하십시오.
-->
<configuration>
<connectionStrings>
<add name="inConnectionString" connectionString="Data Source=localhost;Initial Catalog=ASPNET;Persist Security Info=True;User ID=sa;Password=zangna1" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<defaultDocument>
<files>
<add value="n.aspx"/>
<add value="m.htm"/>
</files>
</defaultDocument>
</system.webServer>
</configuration>