Solved

asp.net, checkbox

Posted on 2014-02-05
12
404 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Accepted Solution

by:
Jeroen Timmermans earned 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now