Solved

Checking status of checkbox in datalist

Posted on 2008-10-26
5
387 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

896 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

18 Experts available now in Live!

Get 1:1 Help Now