从 ASP.NET GridView 获取 DataRow [英] Getting a DataRow from an ASP.NET GridView

查看:26
本文介绍了从 ASP.NET GridView 获取 DataRow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到 ObjectDataSource(绑定到 MySQL 数据库)的 ASP.NET GridView.在这个网格上,我有 2 个未绑定的 ButtonField 列,我想触发服务器端事件.因此,我在 GridViewRowCommand 事件中添加了一个 eventhandler 方法.

I have an ASP.NET GridView that's bound to an ObjectDataSource (which is bound to a MySQL database). On this grid, I have 2 unbound ButtonField columns that I want to trigger server-side events. Hence I have added an eventhandler method to the GridView's RowCommand event.

在所述事件处理程序的代码中,我需要以某种方式获取用户单击的底层 DataRow.但是,我似乎无法让它发挥作用;如果我使用如下代码,selectedRow 变量总是 null:

In the code of said eventhandler, I need to somehow get hold of the underlying DataRow that was clicked on by the user. However, I can't seem to get this to work; if I use code like the following the selectedRow variable is always null:

protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
  CallDataRow selectedRow = (CallDataRow) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].DataItem;
}

我在谷歌上搜索并找到了诸如 http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event,但我发现没有任何效果.我很确定我只是遗漏了一些明显的东西,但我不知道是什么......

I've Googled and found pages such as http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event, but nothing I've found has worked. I'm pretty sure I'm just missing something obvious, but I can't figure out what...

如果有帮助,这里是 ASP.NET 代码:

Here's the ASP.NET code if that helps:

  <asp:GridView ID="searchResultsGridView" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="PersonNo,CallDate" 
    Width="100%" AllowPaging="True" EnableSortingAndPagingCallbacks="True" 
    onrowcommand="searchResultsGridView_RowCommand" PageSize="20">
    <Columns>
      <asp:BoundField DataField="PersonNo" HeaderText="Account number" ReadOnly="True" 
        SortExpression="PersonNo" />
      <asp:BoundField DataField="AgentNo" HeaderText="Agent number" 
        SortExpression="AgentNo" />
      <asp:BoundField DataField="AgentName" HeaderText="Agent name" 
        SortExpression="AgentName" />
      <asp:BoundField DataField="TelNumber" HeaderText="Telephone number" 
        SortExpression="TelNumber" />
      <asp:BoundField DataField="CallDate" HeaderText="Call date/time" ReadOnly="True" 
        SortExpression="CallDate" />
      <asp:ButtonField CommandName="play" HeaderText="Audio" ShowHeader="True" 
        Text="Play" />
      <asp:ButtonField CommandName="download" Text="Download" />
    </Columns>
  </asp:GridView>

推荐答案

最终通过执行以下操作使其工作:

Finally got it to work by doing the following:

  1. 添加包含绑定的 HiddenField 的 TemplateField.

  1. Adding a TemplateField containing a bound HiddenField.

<asp:TemplateField ShowHeader="False">
  <ItemTemplate>
    <asp:HiddenField ID="audioFileName" runat="server" Value='<%# Eval("AudioFileName") %>' />
  </ItemTemplate>
</asp:TemplateField>

  • 在 RowCommand 事件处理程序中添加以下代码:

  • Adding the following code in the RowCommand event handler:

    protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      string audioFile = ((HiddenField) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("audioFileName")).Value;
    }
    

  • 这是一个 cludge 并且不是特别安全,但它有效,这就是我现在需要的.尽管如此,仍然欢迎任何更好的解决方案......

    It's a cludge and it's not particularly secure, but it works and that's all I need right now. Any better solutions are still welcome though...

    这篇关于从 ASP.NET GridView 获取 DataRow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆