Peter Nordberg
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="Horizonta l"
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("RoleC heckBox")
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
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="Horizonta
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("RoleC
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
ASKER
Thanks for help!
No, unfortunately that doesn't work either.
Peter
No, unfortunately that doesn't work either.
Peter
ASKER
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I managed to fix it with your information. Peter
Try using the DirectCast mehod. So it becomes
Dim roleCheckBox As CheckBox = DirectCast(control.FindCon
-M3mph15