zoukankan
html css js c++ java
Repeater嵌套,增删改查综合运用
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
Default2 : System.Web.UI.Page
...
{
private
const
string
connectionStr
=
@"
Data Source=.SQLEXPRESS;AttachDbFilename=D:QianTaoApp_Datadb.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();
}
}
}
//
判断是否添加模块
if
(e.CommandName
==
"
AddModule
"
)
...
{
TextBox tb
=
e.Item.FindControl(
"
txtModuleName
"
)
as
TextBox;
using
(SqlConnection conn
=
new
SqlConnection(connectionStr))
...
{
conn.Open();
using
(SqlCommand cmd
=
new
SqlCommand(
"
insert into tb_Module(classId,moduleName)values(@classId,@moduleName)
"
, conn))
...
{
cmd.Parameters.AddWithValue(
"
@moduleName
"
, tb.Text);
cmd.Parameters.AddWithValue(
"
@classId
"
, e.CommandArgument);
cmd.ExecuteNonQuery();
SetBind();
}
}
}
}
/**/
/**/
/**/
/**/
/**/
/**/
/**/
///
<summary>
///
嵌套repeater的ItemCommand事件
///
</summary>
protected
void
rptModule_ItemCommand(
object
source, RepeaterCommandEventArgs e)
...
{
//
判断是否删除模块
if
(e.CommandName
==
"
DelModule
"
)
...
{
using
(SqlConnection conn
=
new
SqlConnection(connectionStr))
...
{
conn.Open();
using
(SqlCommand cmd
=
new
SqlCommand(
"
delete from tb_Module where id=@Id;
"
, conn))
...
{
cmd.Parameters.AddWithValue(
"
@Id
"
, e.CommandArgument);
cmd.ExecuteNonQuery();
SetBind();
}
}
}
//
判断是否修改模块
if
(e.CommandName
==
"
UpdateModule
"
)
...
{
TextBox tb
=
e.Item.FindControl(
"
txtModuleName
"
)
as
TextBox;
using
(SqlConnection conn
=
new
SqlConnection(connectionStr))
...
{
conn.Open();
using
(SqlCommand cmd
=
new
SqlCommand(
"
update tb_Module set moduleName=@Name where id=@id
"
, conn))
...
{
cmd.Parameters.AddWithValue(
"
@Name
"
, tb.Text);
cmd.Parameters.AddWithValue(
"
@id
"
, e.CommandArgument);
cmd.ExecuteNonQuery();
SetBind();
}
}
}
}
/**/
/**/
/**/
/**/
/**/
/**/
/**/
///
<summary>
///
绑定Repeater
///
</summary>
private
void
SetBind()
...
{
DataSet ds
=
new
DataSet();
using
(SqlConnection conn
=
new
SqlConnection(connectionStr))
...
{
SqlDataAdapter adapter
=
new
SqlDataAdapter(
"
select * from tb_Class
"
,conn);
adapter.Fill(ds);
}
rptClass.DataSource
=
ds;
rptClass.DataBind();
}
protected
void
rptClass_ItemDataBound(
object
sender, RepeaterItemEventArgs e)
...
{
if
(e.Item.ItemType
==
ListItemType.Item
||
e.Item.ItemType
==
ListItemType.AlternatingItem)
...
{
Repeater rpt
=
e.Item.FindControl(
"
rptModule
"
)
as
Repeater;
//
找到分类Repeater关联的数据项
DataRowView rowv
=
(DataRowView)e.Item.DataItem;
//
提取分类ID
int
id
=
(
int
)rowv[
"
id
"
];
DataSet ds
=
new
DataSet();
using
(SqlConnection conn
=
new
SqlConnection(connectionStr))
...
{
SqlDataAdapter adapter
=
new
SqlDataAdapter(
"
select * from tb_Module where classId=
"
+
id
+
""
, conn);
adapter.Fill(ds);
}
rpt.DataSource
=
ds;
rpt.DataBind();
}
}
}
<%
...
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default2.aspx.cs
"
Inherits
=
"
Default2
"
%>
<!
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
>
<
asp:Repeater
ID
="rptClass"
runat
="server"
OnItemCommand
="rptClass_ItemCommand"
OnItemDataBound
="rptClass_ItemDataBound"
>
<
HeaderTemplate
>
<
table
>
<
tr
style
="background-color:yellow"
>
<
td
>
添加类别:
<
asp:TextBox
ID
="txtClassName"
runat
="server"
>
</
asp:TextBox
>
<
asp:Button
ID
="btnAddClass"
runat
="server"
CommandName
="AddClass"
Text
="添加"
/>
</
td
>
</
tr
>
</
HeaderTemplate
>
<
ItemTemplate
>
<
tr
style
="background-color:#CCCCCC"
>
<
td
>
<
asp:Button
ID
="btnDelClass"
CommandArgument
='<%#
Eval("id") %
>
' CommandName="DelClass" runat="server" Text="删除类别" />
<
asp:TextBox
ID
="txtClassName"
runat
="server"
Text
='<%#
Eval("className") %
>
'>
</
asp:TextBox
>
<
asp:Button
ID
="btnUpdateClass"
CommandArgument
='<%#
Eval("id") %
>
' CommandName="UpdateClass" runat="server" Text="修改类别" />
</
td
>
</
tr
>
<
tr
>
<
td
>
添加模块:
<
asp:TextBox
ID
="txtModuleName"
runat
="server"
>
</
asp:TextBox
>
<
asp:Button
ID
="btnAddModule"
runat
="server"
CommandName
="AddModule"
CommandArgument
='<%#
Eval("id") %
>
' Text="添加" />
</
td
>
</
tr
>
<
tr
>
<
td
>
<
asp:Repeater
ID
="rptModule"
runat
="server"
OnItemCommand
='rptModule_ItemCommand'
>
<
ItemTemplate
>
<
tr
>
<
td
>
-----------------
<
asp:Button
ID
="btnDeleteModule"
CommandArgument
='<%#
Eval("id") %
>
' CommandName="DelModule" runat="server" Text="删除模块" />
<
asp:TextBox
ID
="txtModuleName"
Text
='<%#
Eval("moduleName") %
>
' runat="server">
</
asp:TextBox
>
<
asp:Button
ID
="btnUpdateModule"
CommandArgument
='<%#
Eval("id") %
>
' CommandName="UpdateModule" runat="server" Text="修改模块" />
</
td
>
</
tr
>
</
ItemTemplate
>
</
asp:Repeater
>
</
td
>
</
tr
>
</
ItemTemplate
>
<
FooterTemplate
>
</
table
>
</
FooterTemplate
>
</
asp:Repeater
>
</
div
>
</
form
>
</
body
>
</
html
>
查看全文
相关阅读:
centos7 实时查看tomcat错误信息
soapUI测试webservice(参数为xml格式的处理方式)
四个好看的CSS样式表格
ORM篇——有关NHibernate查询封装
ORM篇——使用NHibernate配置对象实体的一些小问题
c#操作XML文件的通用方法
C#中正则表达式的使用
C# Dictionary用法总结
DLinq查询
DataGrid通过DataSet保存为xml文件,并导入
原文地址:https://www.cnblogs.com/aaa6818162/p/1452016.html
最新文章
Floyd-Warshall算法(求解任意两点间的最短路) 详解 + 变形 之 poj 2253 Frogger
深度优先搜索 之 CODE[VS] 1295 N皇后问题
深度优先搜索 之 CODE[VS] 1294 全排列
深度优先搜索 之 CODE[VS] 1116 四色问题
深度优先搜索 之 CODE[VS] 1018 单词接龙 2000年NOIP全国联赛普及组NOIP全国联赛提高组
宽度优先搜索 之 CODE[VS] 1099 字串变换 2002年NOIP全国联赛提高组
《C++ Primer》学习 之 编译器推断auto类型
宽度优先搜索 之 CODE[VS] 1026 逃跑的拉尔夫
[LeetCode系列]N皇后问题递归解法 -- 位操作方式
[LeetCode系列]爬梯问题的递归解法转换为迭代解法
热门文章
[LeetCode系列]最大连续子列递归求解分析
[深度学习]Wake-Sleep算法
[深度学习]受限玻尔兹曼机生成手写数字训练样本原理
[深度学习]Python/Theano实现逻辑回归网络的代码分析
[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
[LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
[翻译]你真的知道你看到的UTF-8字符是什么吗?
Eclipse的Java开发中jar导入后无法使用包内class的解决方案
.NET调用JAVA的WebService方法
CentOS7安装字体库
Copyright © 2011-2022 走看看