Solved

asp.net, checkbox

Posted on 2014-02-05
12
411 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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