RadGrid 从项目模板按钮获取选定的行索引

发布于 2024-12-11 15:45:54 字数 1419 浏览 0 评论 0原文

我正在开发一个使用 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 控制?

I am working on a project using Telerik controls. I'm trying to figure out how to get the selected row index on an ItemTemplate button click event, like in the markup below:

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

Normally with a GridView I would just do something like:

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; }
}

What is the equivalent with the Telerik control?

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

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

发布评论

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

评论(5

來不及說愛妳 2024-12-18 15:45:54

使用 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];
}

Use the CommandArgument, and use OnCommand instead of OnClick to get the row index:

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

Code-behind:

protected void Button1_Command(object sender, CommandEventArgs e)
{
    GridDataItem item = RadGrid1.Items[(int)e.CommandArgument];
}
动听の歌 2024-12-18 15:45:54

您可以使用 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;
    }
}

You can use CommandName="" instead of OnClick.

Also add onitemdatabound="RadGrid1_ItemDataBound" to the main telerik:RadGrid tag.

Then in the code behind:

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

                int selectedRowIndex = dataItem.RowIndex;
    }
}
难以启齿的温柔 2024-12-18 15:45:54

浏览 Telerik 文档,看起来你想要:

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

你没有询问这一部分,但我认为这段代码:

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

是在自找麻烦。

尽管标记和代码隐藏总是高度耦合的,但如果你问我的话,直接引用单个单元格就是一种代码味道。我猜您想从 ItemTemplate 中的 ASP Button 中拉出“Select”。

您可以为您的 Button 分配一个 ID,然后调用 FindControl("buttonID") 来获取您需要的数据吗?这将有助于使您的代码更具可维护性和可读性。

Looking through the Telerik documentation, it looks like you want:

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

You didn't ask about this piece, but I think this code:

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

is asking for trouble.

Although markup and code-behind are always highly coupled, directly referencing individual cells is a code smell if you ask me. I'm guessing that you want to pull "Select" out of the ASP Button in your ItemTemplate.

Can you assign an ID to your Button, and call FindControl("buttonID") to get the data you need? That will help keep your code more maintainable and readable.

濫情▎り 2024-12-18 15:45:54
 <telerik:GridTemplateColumn UniqueName="IndexRow" HeaderText="#">
                            <ItemTemplate>
                                <%#Container.ItemIndex + 1%>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
 <telerik:GridTemplateColumn UniqueName="IndexRow" HeaderText="#">
                            <ItemTemplate>
                                <%#Container.ItemIndex + 1%>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
江心雾 2024-12-18 15:45:54

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

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

something like this in Button click event should work

        foreach (GridDataItem item in RadGrid1.SelectedItems)
        {
            GridDataItem item = (GridDataItem)RadGrid1.SelectedItems;
            var key = string.Empty;
            key = item.ItemIndex;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文