我有一个将TemplateField设置为按钮的GridView,我在OnClick事件上有代码,如果已经选择了该行,则该代码可以正常工作。
我努力寻找的是一种使用Button_Click选择行的方法。
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="OrderSummaryID" DataSourceID="SqlDataSource1"
OnSelectedIndexChanged="GridView2_SelectedIndexChanged" Width="1230px">
<Columns>
<asp:BoundField DataField="OrderSummaryID" HeaderText="OrderSummaryID"
ReadOnly="True" SortExpression="OrderSummaryID" />
<asp:BoundField DataField="BariatricRequestID" HeaderText="BariatricRequestID" SortExpression="BariatricRequestID" />
<asp:BoundField DataField="DeviceTypeID" HeaderText="DeviceTypeID"
SortExpression="DeviceTypeID" />
<asp:BoundField DataField="DeviceType" HeaderText="Device Type"
SortExpression="DeviceType" />
<asp:BoundField DataField="SubCategory" HeaderText="Sub Category"
SortExpression="SubCategory" />
<asp:BoundField DataField="Make" HeaderText="Make"
SortExpression="Make" />
<asp:BoundField DataField="Model" HeaderText="Model"
SortExpression="Model" />
<asp:BoundField DataField="WeightLimit" HeaderText="Weight Limit"
SortExpression="WeightLimit" />
<asp:BoundField DataField="DeviceSearchResultStatus" HeaderText="Device Source"
SortExpression="DeviceSearchResultStatus" />
<asp:BoundField DataField="ReqConfirmDateTime" HeaderText="Confirm Date/Time"
SortExpression="ReqConfirmDateTime" />
<asp:BoundField DataField="LocalDeviceAssetID" HeaderText="Internal Asset ID"
SortExpression="LocalDeviceAssetID" />
<asp:TemplateField HeaderText="Confirm Order">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
&#13;
protected void Button1_Click(object sender, EventArgs e)
{
var rowID = GridView2.SelectedRow.Cells[0].Text;
string Source = GridView2.SelectedRow.Cells[8].Text;
var AssetID = GridView2.SelectedRow.Cells[10].Text;
&#13;
答案 0 :(得分:0)
尝试使用这段代码来抓取单击按钮的行。这可能不是确切的事情,但可以帮助你进一步发展。
protected void buttonSubmit_click(object sender, EventArgs e)
{
GridViewRow gRow = ((GridViewRow)((Button)sender).parent.parent);
}
根据按钮的嵌套程度,您需要使用parent
引用。
希望这有帮助。
答案 1 :(得分:0)
您可以使用GridView.RowCommand Event
来处理您的活动。你要做的是给你的按钮一个命令名,以确定点击了哪个按钮。通过使用GridView.RowCommand Event
和按钮的命令名称,您可以检索选定的行。
以下是一个示例。
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "GetIndex")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
}
答案 2 :(得分:0)
如果需要通过RowCommand事件选择一行,在检查了正确的CommandName后,可以选择如下行:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "GetIndex")
{
GridViewRow gvr = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
gvr.RowState = DataControlRowState.Selected;
}
}
只需设置RowState即可(只要您可以事先确定您所在的行。