我有一个gridview:
<asp:GridView ID="gdvOpinions" runat="server" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" Height="100px" Visible="False"
AutoGenerateColumns="False" onrowcommand="gdvOpinions_RowCommand" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Confirm" Text="تائید" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="حذف" />
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="مقاله" HeaderText="مقاله" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="نظر کاربر" HeaderText="نظر کاربر" >
<ControlStyle Width="250px" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
如您所见,我每行都有两个按钮区域,但是当我点击其中一个按钮并希望获取行时
gdvOpinions_RowCommand
的
gdvOpinions.SelectedRow
返回null; 我该如何找到选择哪一行?
答案 0 :(得分:0)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
OnRowCommand = "OnRowCommand">
<Columns>
<asp:ButtonField CommandName = "ButtonField" DataTextField = "CustomerID"
ButtonType = "Button"/>
</Columns>
</asp:GridView>
protected void OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gvRow = GridView1.Rows[index];
}
答案 1 :(得分:0)
根据我的理解,如果您想要获取单击该按钮的行,那么在这种情况下,这可能是正确的做法:
GridViewRow gRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
现在,此处适用于Button
或ImageButton
等控件。您可能需要为此ButtonField
即兴创作。
您还可以使用ButtonField
将TemplateField
更改为简单的Button
。
希望这有帮助。