Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Checking status of checkbox in datalist

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
Avatar of M3mph15
M3mph15
Flag of Australia image

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

-M3mph15
Avatar of Peter Nordberg

ASKER

Thanks for help!

No, unfortunately that doesn't work either.

Peter
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
ASKER CERTIFIED SOLUTION
Avatar of M3mph15
M3mph15
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I managed to fix it with your information. Peter