Solved

asp.net, checkbox

Posted on 2014-02-05
12
417 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
Industry Leaders: 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 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

Industry Leaders: 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!

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 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