Solved

Checking status of checkbox in datalist

Posted on 2008-10-26
5
396 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
[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
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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