RadGrid 从项目模板按钮获取选定的行索引
我正在开发一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
CommandArgument
,并使用OnCommand
而不是OnClick
来获取行索引:代码隐藏:
Use the
CommandArgument
, and useOnCommand
instead ofOnClick
to get the row index:Code-behind:
您可以使用
CommandName=""
而不是OnClick
。还将
onitemdatabound="RadGrid1_ItemDataBound"
添加到主 telerik:RadGrid 标记中。然后在后面的代码中:
You can use
CommandName=""
instead ofOnClick
.Also add
onitemdatabound="RadGrid1_ItemDataBound"
to the main telerik:RadGrid tag.Then in the code behind:
浏览 Telerik 文档,看起来你想要:
你没有询问这一部分,但我认为这段代码:
是在自找麻烦。
尽管标记和代码隐藏总是高度耦合的,但如果你问我的话,直接引用单个单元格就是一种代码味道。我猜您想从
ItemTemplate
中的 ASPButton
中拉出“Select”。您可以为您的
Button
分配一个 ID,然后调用FindControl("buttonID")
来获取您需要的数据吗?这将有助于使您的代码更具可维护性和可读性。Looking through the Telerik documentation, it looks like you want:
You didn't ask about this piece, but I think this code:
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 yourItemTemplate
.Can you assign an ID to your
Button
, and callFindControl("buttonID")
to get the data you need? That will help keep your code more maintainable and readable.按钮点击事件中类似的东西应该可以工作
something like this in Button click event should work