Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to programmatically clear Checkbox
Message
From
01/05/2008 16:24:41
 
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01313608
Message ID:
01314629
Views:
10
>>Given the following HTML fragment, how can I clear the checkmark, in the checkbox with id = "checkAll", in code behind? I can get e reference to the containing grid with:
>>
>>
DirectCast(Page.FindControl("dgListItems"), DataGrid)
>>
>>but I can't figure out how to get a reference to the checkbox in the HeaderTemplate in the ColumnTemplate.
>>
>
>If you're doing this from an event of the checkbox, you can just use:
>
>
>CheckBox check = sender as CheckBox;
>if (check != null)
>   check.Checked = true;
>
>
>If it's from some other event handler, or just in something like a normal postback, you can't do this directly. Actually, I don't think they expose a FindControl for that kind of thing (at least, I didn't see one). You could do this yourself though by traversing the Controls collection of the grid:
>
>
>CheckBox check = this.FindGridControl(this.grdSample.Controls, "checkAll") as CheckBox;
>if (check != null)
>    check.Checked = true;
>
>protected Control FindGridControl(ControlCollection control, string controlName)
>{
>    Control matchingControl = null;
>
>    foreach (Control subControl in control)
>    {
>        if (subControl.ClientID.EndsWith(controlName))
>            return subControl;
>
>        matchingControl = this.FindGridControl(subControl.Controls, controlName);
>        if (matchingControl != null)
>            break;
>    }
>
>    return matchingControl;
>}
>
Hi Paul

Thanks for pointing me in the right direction. Here is what I came up with (VB Version)
Dim chkTemp As CheckBox = DirectCast(Me.FindMyControl(DirectCast(dgTemp, Control), "checkAll"), CheckBox)
If Not chkTemp Is Nothing Then
    chkTemp.Checked = False

End If
      
   Private Function FindMyControl(ByRef ControlToSearch As Control, _                                  
        ByVal controlID As String) As Control                                                       
                                                                                                    
        Dim matchingControl As Control = Nothing                                                    
                                                                                                    
        Try                                                                                         
            For Each subControl As Control In ControlToSearch.Controls                              
                If ((Not subControl.ID Is Nothing) AndAlso (subControl.ID.EndsWith(controlID))) Then
                    matchingControl = subControl                                                    
                    Exit For                                                                        
                                                                                                    
                Else                                                                                
                    If (Not matchingControl Is Nothing) Then    'found it                           
                        Exit For                                                                    
                                                                                                    
                    ElseIf subControl.HasControls() Then                                            
                        matchingControl = Me.FindMyControl(subControl, controlID)                   
                                                                                                    
                    End If                                                                          
                                                                                                    
                End If                                                                              
                                                                                                    
            Next                                                                                    
                                                                                                    
        Catch objEx As Exception                                                                    
                                                                                                    
        Finally                                                                                     
            FindMyControl = matchingControl                                                         
                                                                                                    
        End Try                                                                                     
                                                                                                    
    End Function      
Previous
Reply
Map
View

Click here to load this message in the networking platform