?
Solved

asp.net, checkbox

Posted on 2014-02-05
12
Medium Priority
?
419 Views
Last Modified: 2014-02-16
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" />
0
Comment
Question by:CoolDev2014
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 39837832
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

0
 

Author Comment

by:CoolDev2014
ID: 39839553
Thank but not working. It always returns "FALSE" regardless of it is checked or unchecked.
0
 
LVL 5

Expert Comment

by:Jeroen Timmermans
ID: 39841339
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:CoolDev2014
ID: 39842778
jeroenemans: the answer is no.

If you can post your codes, that will be very helpful.
0
 
LVL 5

Expert Comment

by:Jeroen Timmermans
ID: 39842797
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.
0
 

Author Comment

by:CoolDev2014
ID: 39844336
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?
0
 
LVL 5

Accepted Solution

by:
Jeroen Timmermans earned 2000 total points
ID: 39845016
You could set the ids of the checkboxes to something that represents the database values eg: chk_1

In your submit method, loop through the posted values with a key starting with chk_, strip the chk_ from the key and you'll have your ids.
0
 

Author Comment

by:CoolDev2014
ID: 39845437
can you please show me in codes? Thanks
0
 
LVL 5

Expert Comment

by:Jeroen Timmermans
ID: 39845494
You should be able to figure it out yourself by now.
0
 

Author Comment

by:CoolDev2014
ID: 39845664
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.
0
 
LVL 5

Expert Comment

by:Jeroen Timmermans
ID: 39845675
Yes you can. But you won't. Just like I won't do your work for you.
0
 

Author Comment

by:CoolDev2014
ID: 39846046
ok thank you. We just wait for other experts willing to help more. Again. Thank you for your time and effort.
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question