想从列表视图中检查项目

时间:2012-02-07 00:59:10

标签: c# asp.net listview checkbox

我在C#webapplication中使用listview。我的问题是我想从列表视图中检查项目。我试着找到selectitem和所有。我不知道如何从listview中的复选框中获取检查项目。我的代码如下: -

ASPX

    <asp:ListView ID="PackagesListView" runat="server" DataSourceID="PackagesDataSource" ItemPlaceholderID="itemPlaceholder" 
      GroupItemCount="4" GroupPlaceholderID="groupPlaceholder" OnItemDataBound="PackagesListView_ItemDataBound">
    <LayoutTemplate>
      <table style="margin-left:0px; width:570; table-layout:fixed; overflow:hidden;">
        <tr ID="groupPlaceholder" runat="server" >
        </tr>
      </table>
    </LayoutTemplate>
    <GroupTemplate>
      <tr class="productsTableRow">
        <td ID="itemPlaceholder" runat="server"></td>
      </tr>
    </GroupTemplate>

    <ItemTemplate>
      <td style="width:140px;">
        <div style="text-align:center; line-height:1.5;"><asp:Label ID="PackageLabel" runat="server" Text='<%#Eval("Name")%>' /></div>
        <div style="text-align:center;"><asp:CheckBox ID="PackageCheckBox" runat="server" OnCheckedChanged="OnPackageSelected" AutoPostBack="true" PackageID='<%#Eval("PackageID")%>' /></div>
      </td>
    </ItemTemplate>
  </asp:ListView>
   <asp:Button ID="ButtonSaveQuotation" runat="server" Text="Save Quotation" 
                  CssClass="button" Visible="false" onclick="ButtonSaveQuotation_Click1" />

aspx.cs

     protected void ButtonSaveQuotation_Click1(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();


    }

所以在这里sb我想附加所有选中复选框的标签的文字。谢谢你

3 个答案:

答案 0 :(得分:2)

您必须找到该复选框,因为它位于模板字段中 例如:

if (PackagesListView.Items.Count > 0)
{                
    for (int i = 0; i < PackagesListView.Items.Count; i++)
    {
        CheckBox PackageCheckBox= (CheckBox)PackagesListView.Items[i].FindControl("PackageCheckBox");
        if (PackageCheckBox!= null)
        {
            if (PackageCheckBox.Checked.Equals(true))
            {
                //do your stuff here
            }
        }
    }
}

答案 1 :(得分:1)

foreach (var item in PackagesListView.Items.Where(i => ((CheckBox)i.FindControl("PackageCheckBox")).Checked))
{
    var label =(Label) item.FindControl("PackageLabel");
    label.Text += " Appended text";
}

也许这对你有帮助吗?这应该在页面加载后进行。另外,请确保您没有重新绑定列表视图。

答案 2 :(得分:0)

var texts = PackagesListView.Items.Cast<Control>()
                .Where(c => ((CheckBox)c.FindControl("PackageCheckBox")).Checked)
                .Select(c => ((Label)c.FindControl("PackageLabel")).Text);

var sb = new StringBuilder();
foreach ( var text in texts)
   sb.AppendLine(text);