VIPUL PARMAR
VIPUL PARMAR

Reputation: 292

Select item from checkboxlist and display in listview

I have a checkboxlist and listview. I wan't to display selected values from checkboxlist in listview. For Ex. I select three items then click ok button then this three items are display in listview if i change it like select four items instead of three then it display four items.

I am new in this so please help me.

Thanks

Upvotes: 0

Views: 1596

Answers (1)

user2717258
user2717258

Reputation: 46

try like this way

Html Designer page

<asp:CheckBoxList   
            ID="CheckBoxList1"  
            runat="server"  
            RepeatColumns="2"  
            >  
            <asp:ListItem>SqlDataSource</asp:ListItem>  
            <asp:ListItem>XmlDataSource</asp:ListItem>  
            <asp:ListItem>AccessDataSource</asp:ListItem>  
        </asp:CheckBoxList> 

        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="Add Item"   

            Font-Bold="true"  
            ForeColor="SteelBlue" onclick="Button1_Click"  
            />  

            <asp:ListView ID="listview1" runat="server">
            <LayoutTemplate>
    <table runat="server" id="table1" >
      <tr runat="server" id="itemPlaceholder" ></tr>
    </table>
  </LayoutTemplate>
  <ItemTemplate>
    <tr id="Tr1" runat="server">
      <td id="Td1" runat="server">
        <%-- Data-bound content. --%>
        <asp:Label ID="NameLabel" runat="server"
          Text='<%#Eval("Name") %>' />
      </td>
    </tr>
  </ItemTemplate>
            </asp:ListView>

codebehind

protected void Button1_Click(object sender, EventArgs e)
    {
        List<ListItem> Citems = CheckBoxList1.Items.Cast<ListItem>().Where(n => n.Selected).ToList();
        DataSet dt = new DataSet();
        DataTable table1 = new DataTable();
        table1.Columns.Add("Name");


        if (Citems.Count() != 0)
        {

            for(int i=0;i<Citems.Count();i++)
            {
                table1.Rows.Add(Citems[i].Text);

            }
            dt.Tables.Add(table1);
            listview1.DataSource = dt;
            listview1.DataBind();
        }
    }

Upvotes: 1

Related Questions