我有一个ASP.NET GridView
绑定到ObjectDataSource
(绑定到MySQL数据库)。在这个网格上,我有2个未绑定的ButtonField
列,我想触发服务器端事件。因此,我在GridView
的{{1}}事件中添加了一个事件处理程序方法。
在所述eventhandler的代码中,我需要以某种方式获取用户点击的基础RowCommand
。但是,我似乎无法让这个工作;如果我使用如下代码,则DataRow
变量始终为selectedRow
:
null
我用Google搜索并找到http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event之类的网页,但我找不到任何内容。我很确定我只是遗漏了一些明显的东西,但我无法弄清楚是什么......
如果有帮助,这是ASP.NET代码:
protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
CallDataRow selectedRow = (CallDataRow) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].DataItem;
}
答案 0 :(得分:4)
最后通过以下方式让它发挥作用:
添加包含绑定HiddenField的TemplateField。
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:HiddenField ID="audioFileName" runat="server" Value='<%# Eval("AudioFileName") %>' />
</ItemTemplate>
</asp:TemplateField>
在RowCommand事件处理程序中添加以下代码:
protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
string audioFile = ((HiddenField) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("audioFileName")).Value;
}
这是一个淤泥,它并不是特别安全,但它有效,这就是我现在所需要的。尽管如此,仍然欢迎任何更好的解决方案......
答案 1 :(得分:3)
我甚至迟到了派对!但是当我从谷歌来到这里时,有些人可能仍会打这个,所以我会给出答案:
protected void SomeDataGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
System.Data.DataRowView dr = (System.Data.DataRowView)e.Row.DataItem;
string aVar = dr["DataFieldIdLikePlease"].ToString();
}
}
那么你就可以获得代码隐藏中的任何数据字段。
答案 2 :(得分:1)
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = searchResultsGridView.Rows[index];
答案 3 :(得分:1)
我意识到这已经晚了2。5年(抱歉!之前没有看到它......),但我使用两个静态/共享方法直接转换行。也许这就是你想要做的事情?
VB.Net:
Public Shared Function GridViewRowToDataRow(ByVal gvr As GridViewRow) As DataRow
Dim di As Object = Nothing
Dim drv As DataRowView = Nothing
Dim dr As DataRow = Nothing
If gvr IsNot Nothing Then
di = TryCast(gvr.DataItem, System.Object)
If di IsNot Nothing Then
drv = TryCast(di, System.Data.DataRowView)
If drv IsNot Nothing Then
dr = TryCast(drv.Row, System.Data.DataRow)
End If
End If
End If
Return dr
End Function
Public Shared Function GridViewRowEventArgsToDataRow(ByVal e As GridViewRowEventArgs) As DataRow
Dim gvr As GridViewRow = Nothing
Dim di As Object = Nothing
Dim drv As DataRowView = Nothing
Dim dr As DataRow = Nothing
If e.Row.RowType = DataControlRowType.DataRow Then
gvr = TryCast(e.Row, System.Web.UI.WebControls.GridViewRow)
dr = Helpers.GridViewRowToDataRow(gvr)
End If
Return dr
End Function
C#.Net(使用:http://www.developerfusion.com/tools/convert/vb-to-csharp/转换):
public static DataRow GridViewRowToDataRow(GridViewRow gvr)
{
object di = null;
DataRowView drv = null;
DataRow dr = null;
if (gvr != null) {
di = gvr.DataItem as System.Object;
if (di != null) {
drv = di as System.Data.DataRowView;
if (drv != null) {
dr = drv.Row as System.Data.DataRow;
}
}
}
return dr;
}
public static DataRow GridViewRowEventArgsToDataRow(GridViewRowEventArgs e)
{
GridViewRow gvr = null;
object di = null;
DataRowView drv = null;
DataRow dr = null;
if (e.Row.RowType == DataControlRowType.DataRow) {
gvr = e.Row as System.Web.UI.WebControls.GridViewRow;
dr = Helpers.GridViewRowToDataRow(gvr);
}
return dr;
}
答案 4 :(得分:0)
干净的方法似乎是使用&#34; DataKeyNames&#34; GridView上的属性。数据源应包含一个列,该列是唯一标识符或主键。然后,您可以将gridview上的DataKeyNames属性设置为该属性的名称。
然后,在RowCommand中可以使用与所选行对应的列的值,它可用于确保将命令应用于唯一标识的对象
<asp:GridView ID="gridview" runat="server" ... DataKeyNames="pkey">
protected void gridview_OnRowCommand(object sender, EventArgs e)
{
string selectedPkey = null;
GridViewCommandEventArgs gridEvent = (GridViewCommandEventArgs)e;
int index = Convert.ToInt32(gridEvent.CommandArgument);
if (index >= 0 && index < gridview.Rows.Count)
{
GridViewRow r = gridview.Rows[index];
selectedPkey = gridview.DataKeys[index].Value.ToString();
}