【问题标题】:RadGrid Get Selected Row Index from Item Template ButtonRadGrid 从项目模板按钮获取选定的行索引
【发布时间】:2011-12-13 18:30:51
【问题描述】:

我正在开发一个使用 Telerik 控件的项目。我试图弄清楚如何在 ItemTemplate 按钮单击事件上获取选定的行索引,如下面的标记:

<telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" 
    DataSourceID="cusGrid" GridLines="None" Skin="Default" AllowPaging="True" DataKeyValue="CustomerID" 
    PageSize="500" AllowMultiRowSelection="True" ShowStatusBar="true" >
        <MasterTableView AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="cusGrid">
            <RowIndicatorColumn>
                <HeaderStyle Width="20px"></HeaderStyle>
            </RowIndicatorColumn>
            <ExpandCollapseColumn>
                <HeaderStyle Width="20px"></HeaderStyle>
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
                    <ItemTemplate>
                        <asp:Button runat="server" Text="Select" OnClick="SelRecord" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
    ...

通常使用GridView 我会做类似的事情:

protected void SelRecord(object sender, EventArgs e)
{
    var gRow = (GridViewRow)(sender as Control).Parent.Parent;
    var key = string.Empty;
    if (gRow != null) { key = gRow.Cells[0].Text; }
}

Telerik 控件的等价物是什么?

【问题讨论】:

    标签: c# asp.net telerik


    【解决方案1】:

    使用CommandArgument,并使用OnCommand而不是OnClick来获取行索引:

    <asp:Button ID="Button1" runat="server" CommandArgument='<%#Container.ItemIndex%>' OnCommand="Button1_Command" ... />
    

    代码隐藏:

    protected void Button1_Command(object sender, CommandEventArgs e)
    {
        GridDataItem item = RadGrid1.Items[(int)e.CommandArgument];
    }
    

    【讨论】:

    • 同意这种方法,这是迄今为止最简单的方法。
    • 注意强制转换为 int try : RadGrid1.Items[int.parse((e.CommandArgument.ToString())];
    【解决方案2】:

    您可以使用CommandName="" 代替OnClick

    还将onitemdatabound="RadGrid1_ItemDataBound" 添加到主telerik:RadGrid 标记中。

    然后在后面的代码中:

    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem dataItem = e.Item as GridDataItem;
    
                    int selectedRowIndex = dataItem.RowIndex;
        }
    }
    

    【讨论】:

    • 我看到这被排除在答案之外,但你能解释一下这是为了解决这个问题吗?
    【解决方案3】:

    查看Telerik documentation,看起来像你想要的:

    var gRow = ((sender as Button).NamingContainer as GridItem).Selected;
    

    你没有问这块,但我认为这段代码:

    if (gRow != null) { key = gRow.Cells[0].Text; }
    

    在自找麻烦。

    虽然标记和代码隐藏总是高度耦合,但如果你问我,直接引用单个单元格是一种代码味道。我猜您想在您的ItemTemplate 中将“Select”从 ASP Button 中提取出来。

    您能否为您的Button 分配一个ID,并致电FindControl("buttonID") 以获取您需要的数据?这将有助于使您的代码更易于维护和阅读。

    【讨论】:

      【解决方案4】:
       <telerik:GridTemplateColumn UniqueName="IndexRow" HeaderText="#">
                                  <ItemTemplate>
                                      <%#Container.ItemIndex + 1%>
                                  </ItemTemplate>
                              </telerik:GridTemplateColumn>
      

      【讨论】:

      • 谢谢,这就是我一直在寻找的……在不改变代码的情况下做同样的事情:)
      【解决方案5】:

      按钮点击事件中的类似操作应该可以工作

              foreach (GridDataItem item in RadGrid1.SelectedItems)
              {
                  GridDataItem item = (GridDataItem)RadGrid1.SelectedItems;
                  var key = string.Empty;
                  key = item.ItemIndex;
              }
      

      【讨论】:

        猜你喜欢
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多