繁体   English   中英

如何在ListView中更新选定的行

[英]How to update selected rows in ListView

我有asp.net Web形式的listview。 我想选择行并单击按钮后更新选择的内容。
为此,我想使用Checkbox / CheckboxList。 但是我不明白如何将有关行或从选定行中的列的信息发送到Checkbox / CheckboxList项目。
如何使用Checkbox / CheckboxList选择行并更新它们?
我使用Asp.net Linq实体框架。
我的密码

  <asp:Button ID="ButtonTest" runat ="server" OnClick="ButtonTest_Click" /> <asp:ListView ID="ListView2" ItemType="DocCat.Models.ReqInf" SelectMethod="GetReqF" OnItemDataBound="ListView2_ItemDataBound" DataKeyNames="requestN" EnableViewState="true" runat="server" UpdateMethod="ListView2_UpdateItem" DeleteMethod="ListView2_DeleteItem" InsertMethod="ListView2_InsertItem"> <LayoutTemplate> <div class="outerContainer" style="overflow: scroll"> <table id="docTable"> <thead> <tr> <th> Выбрать </th> <th>First</th> <th>Request</th> <th>Third</th> <th>Four</th> </tr> </thead> <tbody runat="server" id="itemPlaceholder"></tbody> </table> </div> </LayoutTemplate> <ItemTemplate> <tr> <td> <asp:CheckBoxList runat="server" ID="CheckNew" ><asp:ListItem>Выбрать</asp:ListItem></asp:CheckBoxList></td> <td> </td> <td><%# Item.BirthDate.Date%></td> <td><%# Item.F1 %></td> <td><%# Item.F2 %></td> <td><%# Item.F3 %></td> </tr> </ItemTemplate> </asp:ListView> 

选定的行不会显示在Checkboxlist项目和字符串selectedItems中:

  CheckBoxList cblRoles = ListView2.Items[0].FindControl("CheckNew") as CheckBoxList; string selectedItems = ""; for (int i = 0; i < cblRoles.Items.Count; i++) { if (cblRoles.Items[i].Selected) { selectedItems = selectedItems + cblRoles.Items[i].Value + ","; } } 

我最近使用了这种UI。 首先创建Table UI,然后在后面的代码中创建填充Table方法。我使用ADO.net进行数据访问。

注意 :单击按钮后,创建用于获取数据和更新数据的存储过程。

步骤1:在该复选框的创建对象中编写“填充表”方法,但我使用了单选按钮。

                   using (mTableRow = new HtmlTableRow()){
                    {

                        #region Radio Button

                        using (HtmlTableCell lTableCell = new HtmlTableCell())
                        {
                            RadioButton mradioButton = new RadioButton();
                                mradioButton.ID = "Radio" + listInfo.ID;

                                mradioButton.GroupName = "rowSelector1";
                                mradioButton.AutoPostBack = true;
                                mradioButton.Checked = false;

                                mradioButton.CheckedChanged += new EventHandler(AvailableRadioButton_CheckedChanged);

                                lTableCell.Attributes["class"] = "RadioButton";
                                lTableCell.Controls.Add(mradioButton);

                                mTableRow.Cells.Add(lTableCell);

                                #endregion
                         // add all the remaining columns
                           // add table row to the table.

步骤2:为Checkbox事件单击创建方法。

我的问题是找到复选框,我只是想念这个:

  foreach (ListViewDataItem item in this.ListView2.Items)
                {
                    if (item.ItemType == ListViewItemType.DataItem)
                    { 

并且一切正常。

我的button_click方法:

            List<int> ls = new List<int>();

            {

                foreach (ListViewDataItem item in this.ListView2.Items)
                {
                    if (item.ItemType == ListViewItemType.DataItem)
                    {
                        CheckBox chkRow = item.FindControl("CheckBox") as CheckBox;

                        if (chkRow.Checked)
                        {
                            int request = int.Parse((item.FindControl("FirstFind") as Label).Text.Trim());

                            ls.Add(request);


                        }
                    }
                }


                repository.Approved(ls, newstat);

更新方式

  public void Approved(List<int> list,int stat )
        {


            var friends = context.Requery.Where(f => list.Contains(f.parametr)).ToList();

            friends.ForEach(a =>
            {
                a.par1 = 0;
                a.par2 = stat;
            });


            context.SaveChanges();

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM