zoukankan
html css js c++ java
Asp.net 中的 DataGrid 和 GridView 批量更新
1.DataGrid 批量更新
在html的form中设置如下的DataGrid
<
asp:DataGrid
id
="DataGrid1"
style
="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 32px"
runat
="server"
Width
="912px"
Height
="136px"
AutoGenerateColumns
="False"
>
<
Columns
>
<
asp:TemplateColumn
HeaderText
="ID"
>
<
ItemTemplate
>
<
asp:Label
id
=Label1
runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.id") %
>
'>
</
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="用户名"
>
<
ItemTemplate
>
<
asp:TextBox
id
=TextBox1
runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.uname") %
>
'>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="性别"
>
<
ItemTemplate
>
<
asp:TextBox
runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.sex") %
>
' ID="Textbox2">
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="年龄"
>
<
ItemTemplate
>
<
asp:TextBox
runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.age") %
>
' ID="Textbox3">
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="电话"
>
<
ItemTemplate
>
<
asp:TextBox
runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.tel") %
>
' ID="Textbox4">
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
HeaderText
="地址"
>
<
ItemTemplate
>
<
asp:TextBox
runat
="server"
Text
='<%#
DataBinder.Eval(Container, "DataItem.addr") %
>
' ID="Textbox5">
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
</
Columns
>
</
asp:DataGrid
>
<
asp:Button
id
="Button1"
style
="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 232px"
runat
="server"
Text
="批量更新"
></
asp:Button
>
在cs文件中写如下代码
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
if
(
!
this
.IsPostBack)
{
this
.Bind();
}
}
public
void
Bind()
{
try
{
SqlDataAdapter da
=
new
SqlDataAdapter(
"
select * from userinfo
"
,conn);
DataSet ds
=
new
DataSet();
da.Fill(ds,
"
gg
"
);
this
.DataGrid1.DataSource
=
ds;
this
.DataGrid1.DataBind();
}
catch
(Exception Err)
{
throw
Err;
}
finally
{
}
}
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
/**/
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.Button1.Click
+=
new
System.EventHandler(
this
.Button1_Click);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
Button1_Click(
object
sender, System.EventArgs e)
{
int
k
=
0
;
for
(
int
i
=
0
;i
<
this
.DataGrid1.Items.Count;i
++
)
{
int
id
=
int
.Parse(((Label)DataGrid1.Items[i].FindControl(
"
Label1
"
)).Text);
string
username
=
((TextBox)DataGrid1.Items[i].FindControl(
"
TextBox1
"
)).Text;
string
sex
=
((TextBox)DataGrid1.Items[i].FindControl(
"
TextBox2
"
)).Text;
int
age
=
int
.Parse(((TextBox)DataGrid1.Items[i].FindControl(
"
TextBox3
"
)).Text);
string
tel
=
((TextBox)DataGrid1.Items[i].FindControl(
"
TextBox4
"
)).Text;
string
addr
=
((TextBox)DataGrid1.Items[i].FindControl(
"
TextBox5
"
)).Text;
string
sql
=
"
update userinfo set uname='
"
+
username
+
"
',sex='
"
+
sex
+
"
',age=
"
+
age
+
"
,tel='
"
+
tel
+
"
',addr='
"
+
addr
+
"
' where id=
"
+
id
+
""
;
SqlCommand cmd
=
new
SqlCommand(sql,conn);
if
(conn.State
==
ConnectionState.Closed)
{
conn.Open();
}
k
=
cmd.ExecuteNonQuery();
}
if
(k
>
0
)
{
Page.RegisterStartupScript(
""
,
"
<script>alert('更新成功!');</script>
"
);
}
}
2.GridView 批量更新
在html的form中设置如下的GridView
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
Width
="873px"
>
<
Columns
>
<
asp:TemplateField
HeaderText
="ID"
>
<
ItemTemplate
>
<
asp:Label
ID
="Label1"
runat
="server"
Text
='<%#
Bind("id") %
>
'>
</
asp:Label
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="用户名"
>
<
ItemTemplate
>
<
asp:TextBox
ID
="TextBox2"
runat
="server"
Text
='<%#
Bind("uname") %
>
'>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="性别"
>
<
ItemTemplate
>
<
asp:TextBox
ID
="TextBox3"
runat
="server"
Text
='<%#
Bind("sex") %
>
'>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="年龄"
>
<
ItemTemplate
>
<
asp:TextBox
ID
="TextBox4"
runat
="server"
Text
='<%#
Bind("age") %
>
'>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="电话"
>
<
ItemTemplate
>
<
asp:TextBox
ID
="TextBox5"
runat
="server"
Text
='<%#
Bind("tel") %
>
'>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="地址"
>
<
ItemTemplate
>
<
asp:TextBox
ID
="TextBox6"
runat
="server"
Text
='<%#
Bind("addr") %
>
'>
</
asp:TextBox
>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
</
asp:GridView
>
</
div
>
<
asp:Button
ID
="Button1"
runat
="server"
Text
="批量更新"
OnClick
="Button1_Click"
/>
在cs文件中写如下代码
SqlConnection conn
=
new
SqlConnection(ConfigurationManager.AppSettings[
"
conn
"
]);
protected
void
Page_Load(
object
sender, EventArgs e)
{
if
(
!
this
.IsPostBack)
{
this
.Bind();
}
}
private
void
Bind()
{
try
{
SqlDataAdapter da
=
new
SqlDataAdapter(
"
select * from userinfo
"
,conn);
DataSet ds
=
new
DataSet();
da.Fill(ds,
"
ff
"
);
this
.GridView1.DataSource
=
ds;
this
.GridView1.DataBind();
}
catch
(Exception Err)
{
throw
Err;
}
finally
{
}
}
protected
void
Button1_Click(
object
sender, EventArgs e)
{
int
k
=
0
;
foreach
(GridViewRow gv
in
GridView1.Rows)
{
int
id
=
int
.Parse(((Label)gv.FindControl(
"
Label1
"
)).Text);
string
username
=
((TextBox)gv.FindControl(
"
TextBox2
"
)).Text;
string
sex
=
((TextBox)gv.FindControl(
"
TextBox3
"
)).Text;
int
age
=
int
.Parse(((TextBox)gv.FindControl(
"
TextBox4
"
)).Text);
string
tel
=
((TextBox)gv.FindControl(
"
TextBox5
"
)).Text;
string
addr
=
((TextBox)gv.FindControl(
"
TextBox6
"
)).Text;
string
sql
=
"
update userinfo set uname='
"
+
username
+
"
',sex='
"
+
sex
+
"
',age=
"
+
age
+
"
,tel='
"
+
tel
+
"
',addr='
"
+
addr
+
"
' where id=
"
+
id
+
""
;
SqlCommand cmd
=
new
SqlCommand(sql, conn);
if
(conn.State
==
ConnectionState.Closed)
{
conn.Open();
}
k
=
cmd.ExecuteNonQuery();
}
if
(k
>
0
)
{
Page.RegisterStartupScript(
""
,
"
<script>alert('更新成功!');</script>
"
);
}
}
查看全文
相关阅读:
【VB/.NET】Converting VB6 to VB.NET 【Part II】【之一】
ubuntu常用命令
mkfifo()函数
截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果
vc ado 生僻使用
[HTML5SVG]JavaScript 的新领域 动态图片处理(SVG)
中国离“过多福利”还有多远?
mysql 修改列为not null报错Invalid use of NULL value
内存泄漏与内存溢出的区别
mysql ”Invalid use of null value“ 解决方法
原文地址:https://www.cnblogs.com/zhukezhuke/p/1929752.html
最新文章
基本数据结构:链表(list) C小加 C++博客
《构造正则表达式引擎》新鲜出炉啦! λcalculus(惊愕到手了欧耶,GetBlogPostIds.aspx) C++博客
groovy parttion and sql example
华润三九 感冒灵颗粒10g*9袋/盒【说明书、功效、副作用、价格】 京东好药师网上药店 这药又涨了1块钱
交易算法故障导致Knight资本集团损失超过4亿美元_IT新闻_博客园
GDB调试使用技巧 专职C++ C++博客
使用Flex图表组件
在Debian下安装Nvidia驱动
Flex编码过程
Flex开发模型
热门文章
Flex编程模型
GNU通用公共许可证( GPL)
构建Flex数据服务程序
试用笨笨兔
Linux Socket学习(一)
构建一个Flex程序
VC下的Time
第二章Getting Start with the Oracle Server(oracle入门)
求链表中倒数第K个节点
iOS网络编程iOS中解析Bonjour服务
Copyright © 2011-2022 走看看