简体   繁体   中英

How to update selected rows in ListView

I have listview in asp.net web form . I want to select rows and update selected after button click.
For this i want to use Checkbox/CheckboxList . But i don't understand how to send information about row or from column in selected row to Checkbox/CheckboxList item .
How can i select rows, and update them , using Checkbox/CheckboxList ?
I use Asp.net Linq Entity Framework.
My code

  <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> 

Selected rows don't display in Checkboxlist items and in string 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 + ","; } } 

I recently used this kind of UI. First I created Table UI, I created populate Table method in my code behind.I used ADO.net for Data Access.

Note : Create stored procs for getting the data and updating the data after button click.

Step1: Write Populate Table method in that create object for checkbox but I used radio Button.

                   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.

Step2: Create a method for event click of Checkbox.

My problem was to find checkbox, i just missed this :

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

and all is working .

My button_click method:

            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);

Update Method

  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();

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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