见意使用Repeater,这我经常用的更新全部代码:部分代码
<asp:Repeater ID="RptStudents" runat="server">
<HeaderTemplate>
<table border="0" align="center" cellpadding="3" cellspacing="1" class="UserTable">
<tr class="UserTableRow2">
<td width="75px" align="center"><b>姓名</b></td>
<td width="50px" align="center"><b>性别</b></td>
<td width="50px" align="center"><b>年龄</b></td>
<td width="150px" align="center"><b>电话</b></td>
<td width="150px" align="center"><b>地址</b></td>
<td width="150px" align="center"><b>Email</b></td>
<td width="75px" align="center"><b>选择<asp:CheckBox ID="ChkAllDel" runat="server" AutoPostBack="true" OnCheckedChanged="ChkAllDel_CheckedChanged" /></b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="UserTableRow1">
<td width="75px" onmouseover="c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'" onmouseout="this.style.backgroundColor=c" align="center">
<asp:HyperLink ID="LnkName" NavigateUrl='<%# "Admin_Users.aspx?addname="+Server.UrlEncode(Eval("ename").ToString()) %>' Text='<%# Eval("ename") %>' Target="_self" runat="server"></asp:HyperLink>
</td>
<td width="50px">
<asp:DropDownList ID="DListSex" runat="server">
<asp:ListItem Value="男">男</asp:ListItem>
<asp:ListItem Value="女">女</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="LabPwd" runat="server" Visible="false" Text='<%# Eval("pwd") %>'></asp:Label>
<asp:Label ID="LabId" runat="server" Visible="false" Text='<%# Eval("id") %>'></asp:Label>
</td>
<td width="50px"><asp:TextBox ID="TxtAge" Width="35" runat="server" Text='<%# Eval("age") %>'></asp:TextBox><asp:RequiredFieldValidator ID="ReqTxtAge" ControlToValidate="TxtAge" ValidationGroup="UpdateStudents" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator></td>
<td width="150px"><asp:TextBox ID="TxtPhone" Width="135" runat="server" Text='<%# Eval("phone") %>'></asp:TextBox><asp:RequiredFieldValidator ID="ReqTxtPhone" ControlToValidate="TxtPhone" ValidationGroup="UpdateStudents" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator></td>
<td width="150px"><asp:TextBox ID="TxtAddress" Width="135" runat="server" Text='<%# Eval("address") %>'></asp:TextBox><asp:RequiredFieldValidator ID="ReqTxtAddress" ControlToValidate="TxtAddress" ValidationGroup="UpdateStudents" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator></td>
<td width="150px"><asp:TextBox ID="TxtEmail" Width="135" runat="server" Text='<%# Eval("email") %>'></asp:TextBox><asp:RequiredFieldValidator ID="ReqTxtEmail" ControlToValidate="TxtEmail" ValidationGroup="UpdateStudents" runat="server" ErrorMessage="*"></asp:RequiredFieldValidator></td>
<td width="75px"><asp:CheckBox ID="ChkDel" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr class="UserTableRow2">
<td colspan="7" align="right">
<asp:Button ID="BtnCanel" runat="server" Text="重置" Width="50" />
<asp:Button ID="BtnUpdate" runat="server" Text="更新" OnClick="BtnUpdate_Click" ValidationGroup="UpdateStudents" Width="50" />
</td>
</tr>
</table></FooterTemplate>
</asp:Repeater>
//遍历更新学生信息表
protected void BtnUpdate_Click(object sender, EventArgs e)
{
int UpdateCount = 0;
for (int i = 0; i <= RptStudents.Items.Count - 1; i++)
{
HyperLink LnkName=(HyperLink)RptStudents.Items[i].FindControl("LnkName");
TextBox TxtAge = (TextBox)RptStudents.Items[i].FindControl("TxtAge");
TextBox TxtPhone = (TextBox)RptStudents.Items[i].FindControl("TxtPhone");
TextBox TxtAddress = (TextBox)RptStudents.Items[i].FindControl("TxtAddress");
TextBox TxtEmail = (TextBox)RptStudents.Items[i].FindControl("TxtEmail");
DropDownList DListSex = (DropDownList)RptStudents.Items[i].FindControl("DListSex");
if (LnkName == null)
continue;
studentsInfo.EName = LnkName.Text.ToString();
studentsInfo.Age = Convert.ToInt32(TxtAge.Text.ToString());
studentsInfo.Phone = TxtPhone.Text.ToString();
studentsInfo.Address = TxtAddress.Text.ToString();
studentsInfo.Email = TxtEmail.Text.ToString();
studentsInfo.Sex = DListSex.SelectedValue.ToString();
studentsInfo.Adddate = DateTime.Now.ToString();
RepeaterItem item = LnkName.Parent as RepeaterItem;
Label LabFId = item.FindControl("LabId") as Label;
Label LabFPwd = item.FindControl("LabPwd") as Label;
studentsInfo.Pwd = LabFPwd.Text.ToString();
studentsInfo.Id = LabFId.Text.ToString();
if (LabFId.Text.ToString()!="")
{
if (students.Update(studentsInfo) > 0)
UpdateCount += 1;
}
}
ScriptManager.RegisterStartupScript(UpDPanelStudents, typeof(UpdatePanel), "JsAlert", "alert('更新" + UpdateCount.ToString() + "条记录成功!')", true);
BindRptStudents("1");
}
这个比较容易实现,可以做一个GridView的行遍历,本质上与单行没有不同。这有篇文章,请参考:
http://www.cnblogs.com/jiangyuxuan/archive/2007/06/02/768692.html