如何在ListView

时间:2017-04-14 11:24:29

标签: c# jquery asp.net listview

我在asp.net网络表单中有listview。我想在按钮点击后选择行并更新 为此,我想使用Checkbox / CheckboxList。但我不明白如何将有关行或从选定行中的列的信息发送到Checkbox / CheckboxList项目 如何使用Checkbox / CheckboxList选择行并更新它们? 我使用Asp.net Linq Entity Framework 我的代码

  <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 + ",";
            }
        }
       
       
    

2 个答案:

答案 0 :(得分:0)

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

注意:创建存储过程以获取数据并在按钮点击后更新数据。

步骤1:在复选框的创建对象中编写Populate Table方法,但我使用了单选按钮。

                   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的事件。

答案 1 :(得分:0)

我的问题是找到复选框,我错过了这个:

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

        }
相关问题