C# GridView是否标识单击的行值?
我有这样的观点:C# GridView是否标识单击的行值?,c#,asp.net,linq,gridview,C#,Asp.net,Linq,Gridview,我有这样的观点: <div class="content"> <asp:GridView ID="DocumentGrid" runat="server" AutoGenerateColumns="False" OnRowCommand="DocumentGrid_RowCommand" > <Columns> <asp:BoundField HeaderText="ID" DataField="ID" I
<div class="content">
<asp:GridView ID="DocumentGrid" runat="server" AutoGenerateColumns="False" OnRowCommand="DocumentGrid_RowCommand" >
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-Width="120px"/>
<asp:ButtonField HeaderText="Download Link" Text="Download"/>
</Columns>
</asp:GridView>
</div>
如您所见,当按下“下载”按钮时,会调用DocumentGrid_row命令,如何查找单击的行的值?如果GridView中有多个按钮字段,请设置
CommandName
属性。这样我们就可以确定在RowCommand事件中按下了哪个按钮。所以总是设置commandName属性
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" ItemStyle-Width="120px"/>
<asp:ButtonField HeaderText="Download Link" Text="Download" CommandName="cmd"/>
</Columns>
如果这样设置标记
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownload" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>'
runat="server" Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
不久前,我在这里问了一个类似的问题。也许会有帮助,或者这个
<Columns>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownload" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>'
runat="server" Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
if (e.CommandName == "Download")
{
int index = Convert.ToInt32(e.CommandArgument);
}