首页 新闻 会员 周边

在ASP.NET中如何实现批量更新?

0
悬赏园豆:15 [已解决问题] 解决于 2008-12-15 17:10

显示控件为GridView或者Repeater,GridView自带的只能实现单条记录的更新,现在只设置一个更新按钮来实现。

问题补充: 如果要更新的数据很多,数据量比较大,应该如何一次性全部更新?
sl2008的主页 sl2008 | 菜鸟二级 | 园豆:407
提问于:2008-12-15 10:32
< >
分享
最佳答案
0

见意使用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" />&nbsp;&nbsp;
        <asp:Button ID="BtnUpdate" runat="server" Text="更新" OnClick="BtnUpdate_Click" ValidationGroup="UpdateStudents" Width="50" />&nbsp;&nbsp;
        </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");
        }

Astar | 高人七级 |园豆:40805 | 2008-12-15 11:03
其他回答(1)
0

这个比较容易实现,可以做一个GridView的行遍历,本质上与单行没有不同。这有篇文章,请参考:

http://www.cnblogs.com/jiangyuxuan/archive/2007/06/02/768692.html

GUO Xingwang | 园豆:3885 (老鸟四级) | 2008-12-15 10:37
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册