

HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Panel ID="PanelItem" runat="server">
job_id:<asp:Literal ID="LitJob_id" Text='<%#Eval("job_id") %>' runat="server"></asp:Literal>
job_desc:<asp:Literal ID="LitJob_desc" Text='<%#Eval("job_desc") %>' runat="server"></asp:Literal>
min_lvl:<asp:Literal ID="Litmin_lvl" Text='<%#Eval("min_lvl") %>' runat="server"></asp:Literal>
max_lvl:<asp:Literal ID="Litmax_lvl" Text='<%#Eval("max_lvl") %>' runat="server"></asp:Literal>
<asp:Button ID="btnEdit" CommandName="edit" runat="server" Text="编辑" />
<asp:Button ID="btnDel" CommandName="delete" runat="server" Text="删除" />
</asp:Panel>
<asp:Panel ID="PanelEdit" runat="server" Visible="false">
job_id:<asp:Literal ID="LitJob_idEdit" Text='<%#Eval("job_id") %>' runat="server"></asp:Literal>
job_desc:<asp:TextBox ID="txtJob_desc" Text='<%#Eval("job_desc") %>' runat="server"></asp:TextBox>
min_lvl:<asp:TextBox ID="txtMin_lvl" Text='<%#Eval("min_lvl") %>' runat="server"></asp:TextBox>
max_lvl:<asp:TextBox ID="txtMax_lvl" Text='<%#Eval("max_lvl") %>' runat="server"></asp:TextBox>
<asp:Button ID="btnUpdate" CommandName="update" runat="server" Text="更新" />
<asp:Button ID="btnCancel" CommandName="cancel" runat="server" Text="取消" />
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
.cs代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string StrConn = @"User Id=sa;Password=perishryu;Initial Catalog=pubs;Data Source=oathryu/sql2005";
DataSet ds;
SqlDataAdapter da;
SqlConnection conn;
SqlCommand cmd;
public bool Make(string sql)
{
using (conn = new SqlConnection(StrConn))
{
cmd = new SqlCommand(sql, conn);
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
try
{
conn.Open();
cmd.ExecuteNonQuery();
return false;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
public override void DataBind()
{
da = new SqlDataAdapter("select * from jobs",StrConn);
ds = new DataSet();
da.Fill(ds);
this.Repeater1.DataSource = ds.Tables[0];
this.Repeater1.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBind();
}
}
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "edit")
{
((Panel)(e.Item.FindControl("PanelItem"))).Visible = false;
((Panel)(e.Item.FindControl("PanelEdit"))).Visible = true;
}
if (e.CommandName == "update")
{
string id = ((Literal)(e.Item.FindControl("LitJob_idEdit"))).Text;
string desc = ((TextBox)(e.Item.FindControl("txtJob_desc"))).Text;
string min = ((TextBox)(e.Item.FindControl("txtMin_lvl"))).Text;
string max = ((TextBox)(e.Item.FindControl("txtMax_lvl"))).Text;
string sql = "update jobs set job_desc='" + desc + "',min_lvl='" + min + "',max_lvl='" + max + "' where job_id='" + id + "'";
Make(sql);
((Panel)(e.Item.FindControl("PanelItem"))).Visible = true;
((Panel)(e.Item.FindControl("PanelEdit"))).Visible = false;
DataBind();
}
if (e.CommandName == "delete")
{
string id = ((Literal)(e.Item.FindControl("LitJob_id"))).Text;
string sql = "delete from jobs where job_id='" + id + "'";
Make(sql);
DataBind();
}
if (e.CommandName == "cancel")
{
((Panel)(e.Item.FindControl("PanelItem"))).Visible = true;
((Panel)(e.Item.FindControl("PanelEdit"))).Visible = false;
}
}
}
本文介绍了一个使用ASP.NET Repeater控件实现数据增删改查功能的例子。通过C#代码及HTML页面展示如何操作数据库,包括编辑、更新、删除等基本功能。
1636

被折叠的 条评论
为什么被折叠?



