迭代 RadGrid 中的行/复选框

发布于 2024-08-28 03:42:48 字数 798 浏览 4 评论 0原文

我有一个带有 GridTemplateColumn 的 Telerik RadGrid,其中包含一个复选框,如下所示:

<telerik:GridTemplateColumn HeaderText="MINE" UniqueName="MyTemplateColumn">
     <ItemTemplate>
          <asp:CheckBox id="MyCheckBox" runat="server"></asp:CheckBox>
     </ItemTemplate>
</telerik:GridTemplateColumn>

我想根据从数据库读取的值将该框设置为“选中”。我可以处理 ItemDataBound 事件并在绑定每一行时读取数据库,但这会导致 n 次查找。相反,我想处理 DataBound,然后立即设置所有值。因此,在该方法中,我想要这样的代码:

// read all values from database first, then...
foreach(var chkbox in MyRadGrid.MasterTableView.Columns.FindByUniqueName("MyTemplateColumn").FindControl("MyCheckBox")) {
    chkbox.Checked = oneValue;
}

这不起作用,因为 FindControl 不是 GridColumn 的方法,并且它不会生成可迭代的复选框列表。迭代模板列中的复选框的正确方法是什么?谢谢!

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 MyRadGrid.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2024-09-04 03:42:48

Telerik 在他们的论坛上回复了我,并给出了答案,如下:

foreach (GridDataItem item in MyRadGrid.MasterTableView.Items) 
{ 
  CheckBox chk = (CheckBox)item.FindControl("MyCheckBox");
  // Set the value here
}

希望这对某人有用!

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!

半世蒼涼 2024-09-04 03:42:48

我遇到了同样的问题..这就是我所做的..

'创建了一个本地哈希表以立即使用,否则

Private _GroupMembers As New Hashtable

'在页面加载时加载它
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

'检查是否包含
Protected Sub RadGrid2_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) 处理 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文