【问题标题】:c# gridview select on button clickc# gridview 选择按钮单击
【发布时间】:2015-09-05 05:28:03
【问题描述】:

我有一个将 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>

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;

【问题讨论】:

  • 在您的问题上添加一些代码,以便我们更好地了解情况。
  • 你可以从GridView1.SelectedRow获取使用OnSelectedIndexChanged事件
  • 我会使用网格内按钮的位置来选择行

标签: c# select button gridview click


【解决方案1】:

如果您需要通过 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 即可解决问题(只要您可以事先确定您所在的行。

【讨论】:

    【解决方案2】:

    您可以使用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];
      }
    

    MSDN link for this event

    【讨论】:

      【解决方案3】:

      尝试使用这段代码来获取单击按钮所在的行。这可能不是确切的事情,但应该可以帮助您进一步进步。

       protected void buttonSubmit_click(object sender, EventArgs e)
          {
           GridViewRow gRow = ((GridViewRow)((Button)sender).parent.parent);
          }
      

      根据按钮的嵌套程度,您需要使用 parent 引用。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-11-27
        • 2013-07-27
        • 1970-01-01
        • 2022-01-26
        • 2014-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多