亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在ASP.NET中如何實現批量更新?

在ASP.NET中如何實現批量更新?

蠱毒傳說 2018-11-13 05:02:29
顯示控件為GridView或者Repeater,GridView自帶的只能實現單條記錄的更新,現在只設置一個更新按鈕來實現。
查看完整描述

2 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

見意使用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");
        }


查看完整回答
反對 回復 2018-11-14
  • 2 回答
  • 0 關注
  • 650 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號