Search code examples
asp.netvb.netdatalistdatalistitem

How do I get all value in Datalist with one button in asp.net/vb.net


I'm having problem getting all the values in datalist

here is the problem:

I have datalist which is populated dynamically from table in database, the aspx page is the bulk order page so there are many items in datalist and I want the user to be able to selct multiple orders at once in mode and select a buuton in which is called check out, The question is how do I loop throuhg all the checkboxes and textboxes and get the value. any idea a coding would help termendously as I did not code yet at all.

here is my aspx page:

 <asp:DataList ID="DataList1" runat="server" BackColor="White" BorderColor="#CCCCCC" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyField="Id" 
        DataSourceID="SqlDataSource1" GridLines="Both">
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <ItemStyle ForeColor="#000066" />
    <SelectedItemStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <FooterTemplate>
        <asp:Button ID="btnNext" runat="server" Text="CheckOut" 
            onclick="btnNext_Click" />
    </FooterTemplate>
    <ItemTemplate>
        <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
        <br />
        <asp:Image ID="Image1" ImageUrl='<%# Eval("PictureUrlMedium") %>' runat="server" />

        <br />
         <asp:Label ID="DescriptionLabel" runat="server" 
            Text='<%# Eval("Description") %>' />
        <br />
        <br />
        <asp:Table ID="Table1" runat="server">
        <asp:TableRow>
        <asp:TableCell><asp:CheckBox ID="chkSmall" runat="server" Enabled="true" Width="20px"/>

Small

Medium

Large

XLarge

2XLarge

3XLarge

4XLarge

5XLarge


    </ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="<%$ ConnectionStrings:LocalSqlServer %>"  

    SelectCommand="SELECT [Id], [Title], [Description], [Price], [CategoryId], [PictureUrlSmall], [PictureUrlMedium], [PictureUrlLarge], [Deleted] FROM [Product]"></asp:SqlDataSource>

Solution

  • You can achieve this by looping through the Datalist Items in your Click Event:

    foreach(DataListItem item in YourDataList.Items){
        CheckBox chkSmall = (CheckBox)item.FindControl("chkSmall");
        chkSmall.Checked gives you the value
    }