我有一个ASP.NET GridView
是绑定到一个ObjectDataSource
(这势必MySQL数据库)。 在这个网,我有2个绑定ButtonField
,我想触发服务器端事件列。 因此,我已经添加的事件处理程序的方法的GridView
的RowCommand
事件。
在该事件处理程序的代码,我需要以某种方式获得潜在的保持DataRow
被点击用户理解。 不过,我似乎无法得到这个工作; 如果我使用类似于下面的代码的selectedRow
变量始终是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 ,但没有我已经找到工作过。 我敢肯定,我只是失去了一些东西很明显,但我想不出什么...
下面是ASP.NET代码是否有帮助:
<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>
终于得到它通过执行以下工作:
添加含有结合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; }
这是一个cludge,它不是特别安全的,但它的作品,这一切我现在所需要的。 没有更好的办法仍然是受欢迎的,但...
我甚至后来的党! 但是,当我从谷歌来到这里,有些人可能还是打这个所以我给我的答案:
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年半晚(对不起!没有看到过它...),但我用了两个静态/共享方法,该行直接转换。 也许这是你希望做什么?
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;
}
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = searchResultsGridView.Rows[index];
一个干净的方式来做到这似乎是使用在GridView中“的DataKeyNames”属性。 数据源应包含一个列,其是一个唯一的标识符或主键。 然后,您可以设置在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();
}