I have a Telerik RadGrid with a GridTemplateColumn that contains a checkbox, as follows:
<telerik:GridTemplateColumn HeaderText="MINE" UniqueName="MyTemplateColumn">
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
I want to set the box to be "checked" based on a value read from the database. I could handle the ItemDataBound event and read the database when each row is bound, but that results in n lookups. Instead, I want to handle DataBound, and then set all the values at once. So, in that method, I want code like this:
// read all values from database first, then...
foreach(var chkbox in MyRadG开发者_StackOverflowrid.MasterTableView.Columns.FindByUniqueName("MyTemplateColumn").FindControl("MyCheckBox")) {
chkbox.Checked = oneValue;
}
That doesn't work, because FindControl isn't a method of GridColumn, and it won't generate an iterable list of the checkboxes. What is the correct way to iterate through the checkboxes in the template column? Thanks!
Telerik got back to me on their forums with the answer, as follows:
foreach (GridDataItem item in MyRadGrid.MasterTableView.Items)
{
CheckBox chk = (CheckBox)item.FindControl("MyCheckBox");
// Set the value here
}
Hope this is useful for someone!
I am having the same issue.. this was how I did it..
'Created a local hashtable to use now and otherwise
Private _GroupMembers As New Hashtable
'Loaded it up on page load Private Function GetMembers() As Boolean
Try
Dim da As New DataAccess
Dim ht As New Hashtable
Dim i As Int16 = 0
ht.Add("CAC", Session("cac"))
ht.Add("GroupID", _GroupID)
If da.GetData("rap_spGetGroupMemberList", ht) = True Then
If da.SQLDataRows.HasRows Then
While da.SQLDataRows.Read()
i = i + 1
_GroupMembers.Add(i, da.SQLDataRows("UserID"))
End While
End If
da.SQLDataRows.Dispose()
End If
da = Nothing
Catch ex As Exception
Console.Write(ex.Message)
End Try
End Function
'Check for contains Protected Sub RadGrid2_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid2.ItemDataBound
Try
If e.Item.IsDataBound Then
If Not e.Item.DataItem("UserID") Is Nothing Then
If Not IsDBNull(e.Item.DataItem("UserID")) Then
Dim UserID As Long = e.Item.DataItem("UserID")
If _GroupMembers.ContainsValue(UserID) Then
e.Item.Selected = True
End If
End If
End If
End If
Catch ex As Exception
Console.Write(ex.Message)
End Try
End Sub
精彩评论