Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net, checkbox

Posted on 2014-02-05
12
Medium Priority
?
422 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

597 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