Solved

Checking status of checkbox in datalist

Posted on 2008-10-26
5
382 Views
Last Modified: 2010-04-21
Hi

I'm trying to check status for checked/unchecked in a checkbox wrapped by a datalist control. The aspx code is imple and looks like this:
<asp:DataList
        ID="RolesDataList"
        DataSource="<%# MembershipHelper.GetRoles() %>"
        DataKeyField="RoleName"
        BorderStyle="None"
        RepeatColumns="1"
        RepeatDirection="Horizontal"
        RepeatLayout="Table"
        runat="server"
      >
        <ItemTemplate>
            <asp:CheckBox ID="RoleCheckBox" Text='<%# Eval("RoleName") %>' runat="server" />
        </ItemTemplate>
      </asp:DataList>
---------------------------------------------------
The code behin looks like this:

For Each control As Control In RolesDataList.Controls
                Dim roleCheckBox As CheckBox = control.FindControl("RoleCheckBox")

                If Not roleCheckBox Is Nothing AndAlso roleCheckBox.Checked Then
                    ' Do something
                End If
            Next
-----------------------------------------------------------
The strange thing is that independent if the checbox is checked or not the value is returned as checked=false (I have debugged the page to see). It finds the checkbox items ok and everything but I can not understand why it want show it as checked when it is.

If anyone hase any suggestions I would be grateful.

Peter
0
Comment
Question by:peternordberg
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:M3mph15
ID: 22809661
Hi,
Try using the DirectCast mehod. So it becomes
Dim roleCheckBox As CheckBox = DirectCast(control.FindControl("RoleCheckBox"), CheckBox)

-M3mph15
0
 

Author Comment

by:peternordberg
ID: 22811607
Thanks for help!

No, unfortunately that doesn't work either.

Peter
0
 

Author Comment

by:peternordberg
ID: 22813551
Hi agein,

It has something to do with viewstate. The checkboxes gets emptied on postback for some reason. The controls enebleviewstate is not set to false though. Any suggestions?

Peter
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 500 total points
ID: 22818634
Hi,
Ok i'm highly familar with DataLists but i'm thinking they have like a databind method that is being called on every postback. Also I'm going to assume that there is a column in your datalist that has a unique value in it.

Well on the postback what you could do is loop through every row and check if the checkbox is checked, if so then grab the unique value of that row and add it to an arraylist. Then in the Databinding method loop through every row and grab the unique value, see if it matches a unique value in the arraylist if so then make the checkbox checked.

This will record all the records with the checkbox checked, and re check them after a post back making it look like to the uiser that nothing has changed. The same method of recording which records have been checked can be used in the sorting method aswell.
I hope this makes sense
-M3mph15
0
 

Author Closing Comment

by:peternordberg
ID: 31510142
Thanks, I managed to fix it with your information. Peter
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

22 Experts available now in Live!

Get 1:1 Help Now