Link to home
Start Free TrialLog in
Avatar of CoolDev2014
CoolDev2014

asked on

asp.net, checkbox

I hav
I have below codes to get a list of checkbox.
and now if a submit button is clicked,
I want to capture all of the CHECKED value in the checkbox.

How can I do that?



 <asp:DataList CellPadding="4" CellSpacing="200"
                        DataKeyField="ItemID"  
                        RepeatColumns="3" ID="costItems" runat="server"
                        >
                        <ItemTemplate>
                         <asp:Panel Width="250px" runat="server" ID="panelControl">                            
                            <asp:CheckBox TextAlign="Left"  runat="server" ID="subItemCost"  />&nbsp;
                            <asp:Label runat="server" ID="description" Text='<%# Eval("Description") %>'></asp:Label>
                            ($<asp:Label runat="server" ID="cost" Text='<%# Eval("Cost") %>'></asp:Label>)
                         </asp:Panel>
                        </ItemTemplate>
                        </asp:DataList>
<asp:Button runat="server" ID="submit" CssClass="btn" Text="Book Now" onclick="submit_Click" />
Avatar of Mrunal
Mrunal
Flag of India image

hi,
Please find below code.
this will help you,

foreach (DataListItem dli in costItems.Items)
            {
                if (dli.ItemType == ListItemType.Item || dli.ItemType == ListItemType.AlternatingItem)
                {
                    CheckBox itemCostChk = dli.FindControl("subItemCost") as CheckBox;
                    if (itemCostChk != null)
                    {
                        bool isCostSelected = itemCostChk.Checked;
                    }
                }
            }

Open in new window

Avatar of CoolDev2014
CoolDev2014

ASKER

Thank but not working. It always returns "FALSE" regardless of it is checked or unchecked.
in the Page_Load method, is there a check on whether the page is a postback or not?

(!Page.IsPostback){
//Populate DataList
}

Open in new window

jeroenemans: the answer is no.

If you can post your codes, that will be very helpful.
There's your problem. You're recreating the checkboxes before reading their checked-property.
Modify your code so it won't rebind on postback, and you're done.
got it. I am able to get the information. But I got one more question if you don't mind.

i have database table below

id desc cost
1  tire   100
2 brake 200

so now i am able to use datalist and checkbox to read the list.
but now I need to capture the checked value in the checkbox and save them into
new table

so if first checkbox is checked, i want to record into the database. can you show me how to do that?
ASKER CERTIFIED SOLUTION
Avatar of Jeroen Timmermans
Jeroen Timmermans
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
can you please show me in codes? Thanks
You should be able to figure it out yourself by now.
It is okay if you can't show me in codes. Thank you for your time to help me...but  i can't give you points based on your comments you posted.
Yes you can. But you won't. Just like I won't do your work for you.
ok thank you. We just wait for other experts willing to help more. Again. Thank you for your time and effort.