A simple BBS demo including(CRUD) - 6
9. Create a webform named "Update" in the website.
Update.aspx
==========
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Update.aspx.cs" Inherits="Update" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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>Update</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Title: </td>
<td><asp:TextBox ID="txtTitle" runat="server" Width="327px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvTxtTitle" runat="server"
ControlToValidate="txtTitle" ErrorMessage="*"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Writer: </td>
<td>
<asp:TextBox ID="txtWriter" Width="327px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvTxtWriter" runat="server"
ControlToValidate="txtWriter" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>Content: </td>
<td><asp:TextBox ID="txtContent" runat="server" Height="227px" TextMode="MultiLine"
Width="327px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RfvTxtContent" runat="server"
ControlToValidate="txtContent" ErrorMessage="*"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2"><asp:Label ID="lblMsg" runat="server" Visible="false" ForeColor="Red"></asp:Label></td>
</tr>
</table>
<p>
<asp:Button ID="btnUpdate" Text="Update" runat="server"
onclick="btnUpdate_Click" />
<asp:Button ID="btnCancel" Text="Cancel" runat="server"
onclick="btnCancel_Click" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:ConfirmButtonExtender ID="ConfirmButtonExtender1" ConfirmText="Do you really wanna update it?" TargetControlID="btnUpdate" runat="server">
</cc1:ConfirmButtonExtender>
</p>
</div>
</form>
</body>
</html>
===========================================================================================
Update.aspx.cs
============
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Update : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int id = Convert.ToInt32(Request.Params["ID"]);
BLL bbs = new DAL().GetDataByID(id);
txtTitle.Text = bbs.Title;
txtWriter.Text = bbs.Writer;
txtContent.Text = bbs.Content;
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.Params["ID"]);
BLL bbs = new BLL();
bbs.Title = txtTitle.Text;
bbs.Writer = txtWriter.Text;
bbs.Content = txtContent.Text;
try
{
new DAL().UpdateDataByID(bbs, id);
}
catch (Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = "최대 3000자 까지 입력 가능합니다.";
}
Response.Redirect("List.aspx");
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("Read.aspx?ID="+Convert.ToInt32(Request.Params["ID"]));
}
}