using
System;
using
System.Data;
using
System.Data.SqlClient;
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;
public
partial
class
_Default : System.Web.UI.Page
{
private
const
string
connectionStr =
@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\QianTao\App_Data\db.mdf;Integrated Security=True;User Instance=True"
;
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(!IsPostBack)
{
SetBind();
}
}
protected
void
rptClass_ItemCommand(
object
source, RepeaterCommandEventArgs e)
{
//判断是否添加类别
if
(e.CommandName ==
"AddClass"
)
{
TextBox tb = e.Item.FindControl(
"txtClassName"
)
as
TextBox;
using
(SqlConnection conn =
new
SqlConnection(connectionStr))
{
conn.Open();
using
(SqlCommand cmd =
new
SqlCommand(
"insert into tb_Class(className) values(@className)"
, conn))
{
cmd.Parameters.AddWithValue(
"@className"
, tb.Text);
cmd.ExecuteNonQuery();
SetBind();
}
}
}
//判断是否删除类别
if
(e.CommandName ==
"DelClass"
)
{
using
(SqlConnection conn =
new
SqlConnection(connectionStr))
{
conn.Open();
using
(SqlCommand cmd =
new
SqlCommand(
"delete from tb_Module where classId=@classId;delete from tb_Class where id=@classId"
, conn))
{
cmd.Parameters.AddWithValue(
"@classId"
, e.CommandArgument);
cmd.ExecuteNonQuery();
SetBind();
}
}
}
//判断是否修改类别
if
(e.CommandName ==
"UpdateClass"
)
{
TextBox tb = e.Item.FindControl(
"txtClassName"
)
as
TextBox;
using
(SqlConnection conn =
new
SqlConnection(connectionStr))
{
conn.Open();
using
(SqlCommand cmd =
new
SqlCommand(
"update tb_Class set className=@className where id=@id"
, conn))
{
cmd.Parameters.AddWithValue(
"@className"
, tb.Text);
cmd.Parameters.AddWithValue(
"@id"
, e.CommandArgument);
cmd.ExecuteNonQuery();
SetBind();
}
|
Reapter增删改查
最新推荐文章于 2016-01-11 20:45:12 发布
该博客介绍了如何使用ASP.NET中的Repeater控件进行嵌套数据展示,并实现类别和模块的添加、删除和修改功能。通过在Repeater中嵌套Repeater,实现了类别下的模块管理,同时提供了TextBox和Button控件以交互式地更新数据。
摘要由CSDN通过智能技术生成