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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Bind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void Bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw Err;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
private void Button1_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int k=0;
for(int i=0;i<this.DataGrid1.Items.Count;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Open();
}
k=cmd.ExecuteNonQuery();
}
if(k>0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.RegisterStartupScript("","<script>alert('更新成功!');</script>");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!this.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Bind();
}
}
private void Bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw Err;
}
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
protected void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
int k = 0;
foreach (GridViewRow gv in GridView1.Rows)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
conn.Open();
}
k = cmd.ExecuteNonQuery();
}
if (k > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.RegisterStartupScript("", "<script>alert('更新成功!');</script>");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
posted @
2011-01-07 14:45
汉卿
Views(
1350)
Comments()
Edit
收藏
举报