views:

161

answers:

1

Hi

I have a simple usercontrol, with a datalist, and checkboxes inside.

 <asp:DataList ID="DataListDroits" runat="server" DataKeyField="droit_id" DataSourceID="SqlDroits">
      <ItemTemplate>
           <asp:HiddenField ID="HiddenFieldDroitID" runat="server" Value='<%# Eval("droit_id") %>' />
           <asp:CheckBox ID="CheckBoxDroit" runat="server" Text='<%# Eval("droit_label") %>' />
      </ItemTemplate>
 </asp:DataList>

I check them using code behind in the usercontrol :

 Public Sub CheckRole(ByVal role As Integer)
     For Each dliOrganisme As DataListItem In Me.DataListOrganismes.Items
         Dim DataListDroits As DataList = dliOrganisme.FindControl("DataListDroits")
         If DataListDroits IsNot Nothing Then
             For Each dliDroit As DataListItem In DataListDroits.Items
                 If role = CInt(CType(dliDroit.FindControl("HiddenFieldDroitID"), HiddenField).Value) Then
                     Dim CheckBoxDroit As CheckBox = dliDroit.FindControl("CheckBoxDroit")
                     CheckBoxDroit.Checked = True
                 End If
             Next ' DataListDroits
         End If
     Next ' DataListItem
 End Sub

And in the page_load of the calling webform :

            Dim CheckBoxesRoles1 As ASP.organisme_checkboxesroles_ascx = Me.FormViewRubrique.FindControl("CheckBoxesRoles1")
            Dim rolesCoches As New List(Of Integer)
            Dim cmdRoles As New SqlCommand("SELECT droit_id FROM o_droit_rubrique WHERE rubrique_id = @rubrique", conn)
            cmdRoles.Parameters.AddWithValue("rubrique", Request.QueryString("rid"))
            Dim rdrRoles As SqlDataReader = cmdRoles.ExecuteReader
            While rdrRoles.Read
                CheckBoxesRoles1.CheckRole(rdrRoles("droit_id"))
            End While
            rdrRoles.Close()

... and yet, they are not checked.
But if I do this :

 Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
    Dim CheckBoxesRoles1 As ASP.organisme_checkboxesroles_ascx = Me.FormViewRubrique.FindControl("CheckBoxesRoles1")
    If CheckBoxesRoles1 IsNot Nothing Then
        For Each role As Integer In CheckBoxesRoles1.CheckedRoles
            Response.Write("role : " & role & "<br>")
        Next
    End If
 End Sub

I tells me they are...

I'm going mad here ! Why does it tells me they are checked while they obviously are not ?

A: 

Well... for one thing, you aren't checking if your checkboxes are checked, all you're doing is outputting the value of "role". What exactly are you expecting here?

Two suggestions: 1) Set the Checked property of your CheckBox in the aspx like so:

<asp:CheckBox ID="CheckBoxDroit" runat="server"  Text='<%# Eval("droit_label") %>' Checked='<%# (Eval("droit_id") > 0).ToString()' />

2) Set the property in OnItemDataBound in code-behind

One of two things is happening: Either the code you expect to be executing is not really executing (ie, is your if block ever true? Is the control not being found? Try a breakpoint), OR you are doing it at the wrong time -- after the page has already been rendered.

Bryan
- For Each role As Integer In CheckBoxesRoles1.CheckedRoles loops through all the checked checkboxes in the control. So if it writes "role : x", it means the checkbox is checked.- I can't do as you suggest, it just doesn't applies.- Breakpoints tell me the code goes everywhere I want it to go.I will try to run the "check the box" code on different events. Maybe, as you suggest, are they checked too late in the page life cycle.
cosmo0