在带有分页的RadGrid中只选择一个单选按钮,在c# ASP.NET的服务器端代码中选择任意PageSize

本文关键字:选择 PageSize ASP 任意 服务器端 单选按钮 NET 代码 分页 RadGrid 一个 | 更新日期: 2023-09-27 17:50:54

检查/选择只有一个单选按钮在Rad网格或网格视图与分页和任何PageSize在服务器端c#代码后面

我的目标是保持用户选择的asp:RadioButton内部telerik:RadGrid被选择/检查,即使与网格的分页和相应的任何PageSize。

我所面临的挑战是无法为网格中每一行的每个RadioButton保持唯一的GroupName。因此,我必须在后面的代码中处理这个功能。

我如何尝试在互联网上搜索解决方案,我发现了一些javascript的答案有很多工作,我对他们不满意。

<telerik:RadGrid ID="rgWithPaging" AllowPaging="True" CellSpacing="0" GridLines="None"
                AllowSorting="True" runat="server" AutoGenerateColumns="False"
                AllowFilteringByColumn="true" EnableLinqExpressions="false"
                PagerStyle-PageSizeControlType="RadDropDownList" PagerStyle-AlwaysVisible="true" Visible="true"
                OnNeedDataSource="rgWithPaging_NeedDataSource">
...........
    <telerik:GridTemplateColumn HeaderText="Check One" HeaderStyle-CssClass="gradient" AllowFiltering="false" HeaderStyle-Width="10%">
     <ItemTemplate>
      <asp:RadioButton ID="rbWPFileName" runat="server"/>
     </ItemTemplate>
    </telerik:GridTemplateColumn>
...........
</telerik:RadGrid>

在带有分页的RadGrid中只选择一个单选按钮,在c# ASP.NET的服务器端代码中选择任意PageSize

服务器端代码的答案如下所示:

<telerik:RadGrid ID="rgWithPaging" AllowPaging="True" CellSpacing="0" GridLines="None"
                AllowSorting="True" runat="server" AutoGenerateColumns="False"
                AllowFilteringByColumn="true" EnableLinqExpressions="false"
                PagerStyle-PageSizeControlType="RadDropDownList" PagerStyle-AlwaysVisible="true" Visible="true"
                OnNeedDataSource="rgWithPaging_NeedDataSource" OnDataBound="rgWithPaging_OnDataBound">
...........
    <telerik:GridTemplateColumn HeaderText="Check One" HeaderStyle-CssClass="gradient" AllowFiltering="false" HeaderStyle-Width="10%">
     <ItemTemplate>
      <asp:RadioButton ID="rbWPFileName" runat="server" AutoPostBack="true" OnCheckedChanged="rbWPFileName_OnCheckedChanged"/>
     </ItemTemplate>
    </telerik:GridTemplateColumn>
...........
</telerik:RadGrid>
现在在后面的代码中,为了实现这一点,我们只需要两个事件

OnCheckedChanged ="rbWPFileName_OnCheckedChanged"和OnDataBound = " rgWithPaging_OnDataBound "

后面的代码:

    protected void rbWPFileName_OnCheckedChanged(object sender, EventArgs e)
        {
            var rbtnSelected = sender as RadioButton;
            if (rbtnSelected == null) return;
            foreach (GridDataItem item in rgWithPaging.Items)
            {
                var radFileName = (RadioButton)item.FindControl("rbWPFileName");
                if (radFileName == null) return; 
                radFileName.Checked = rbtnSelected.ClientID == radFileName.ClientID;
                if (radFileName.Checked)
                {
                    var position = (rgWithPaging.PageSize*rgWithPaging.CurrentPageIndex) + item.ItemIndex;
                    Session["Position"] = position;
                }
            }
        }
protected void rgWithPaging_OnDataBound(object sender, EventArgs e)
    {
        if (Session["Position"] == null) return;
        var position = (int)Session["Position"];
        var pageIndex = position / rgWithPaging.PageSize;
        var itemIndex = position%rgWithPaging.PageSize;
        if (pageIndex == rgWithPaging.CurrentPageIndex)
        {
            foreach (GridDataItem item in rgWithPaging.Items)
            {
                var radFileName = (RadioButton)item.FindControl("rbWPFileName");
                if (radFileName == null) return;
                radFileName.Checked = itemIndex == item.ItemIndex;
            }
        }
    }

任务完成了!!享受…:-)