如何在RowCommand中从Gridview获取行标签的数据?

时间:2019-04-06 09:54:43

标签: asp.net gridview

我正在使用按钮字段来执行数据库更新。但是,尽管搜索了多个解决方案,但我无法从GridView获取行数据。

我尝试过类似的东西:

GridViewRow gvRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Label lbitemid = (Label)gvRow.FindControl("lblitemid");

,但也未成功。我收到此错误: System.InvalidCastException:'无法将类型为'System.Web.UI.WebControls.GridView'的对象转换为类型为'System.Web.UI.WebControls.Button'。”

<asp:GridView ID="QgridView" AutoGenerateColumns="False" 
 CssClass="table table-bordered" AllowPaging="True" PageSize="6" 
BackColor="White" BorderColor="Black" BorderStyle="Solid" 
 ForeColor="Black" GridLines="None" runat="server" 
 OnRowCommand="QgridView_RowCommand" 
OnSelectedIndexChanged="QgridView_SelectedIndexChanged" >
<Columns>

  <asp:TemplateField HeaderText="No"> 
    <ItemTemplate>
       <span>
       <%#Container.DataItemIndex + 1%>
           </span>
    </ItemTemplate>
    </asp:TemplateField>

   <asp:ImageField HeaderText="Image" DataImageUrlField="coverimg" >
     <ControlStyle CssClass="coverimage"/>
         <ItemStyle HorizontalAlign="Center" />                                                
    </asp:ImageField>

   <asp:BoundField HeaderText="Buyer" DataField="buyer" />
       <asp:TemplateField HeaderText="Item">
          <EditItemTemplate>
   <asp:TextBox ID="tbbuyer" runat="server" Text='<%# Bind("item") %>'> 
   </asp:TextBox>
      </EditItemTemplate>
          <ItemTemplate>
      <asp:Label ID="Lblbuyer" runat="server" Text='<%# Bind("item") %>'> 
       </asp:Label></ItemTemplate>
    </asp:TemplateField>

 <asp:BoundField HeaderText="Price offered" DataField="price" />



   <asp:buttonfield buttontype="Button" commandname="Accept"
    text="Accept"/>




  <asp:TemplateField HeaderText="quoteid">
     <EditItemTemplate>
          <asp:TextBox ID="tbquoteid" runat="server" Text='<%# 
             Bind("quoteid") %>'></asp:TextBox>
      </EditItemTemplate>
       <ItemTemplate>
           <asp:Label ID="lblquoteid" runat="server" Text='<%# 
            Bind("quoteid") %>'></asp:Label>
       </ItemTemplate>
       <HeaderStyle CssClass="hiddencol" />
       <ItemStyle CssClass="hiddencol" />
   </asp:TemplateField>

    <asp:TemplateField HeaderText="id">
       <EditItemTemplate>
         <asp:TextBox ID="tbitemid" runat="server" Text='<%# Bind("id") 
            %>'></asp:TextBox>
       </EditItemTemplate>
        <ItemTemplate>
       <asp:Label ID="lblitemid" runat="server" Text='<%# Bind("id") 
          %>'> 
         </asp:Label>
        </ItemTemplate>
        <HeaderStyle CssClass="hiddencol" />
       <ItemStyle CssClass="hiddencol" />
    </asp:TemplateField>

    <asp:TemplateField HeaderText="rtype">
      <EditItemTemplate>
         <asp:TextBox ID="tbtype" runat="server" Text='<%# Bind("rtype") 
          %>'></asp:TextBox>
      </EditItemTemplate>
      <ItemTemplate>
         <asp:Label ID="lbltype" runat="server" Text='<%# Bind("rtype") 
          %>'></asp:Label>
    </ItemTemplate>
      <HeaderStyle CssClass="hiddencol" />
      <ItemStyle CssClass="hiddencol" />
    </asp:TemplateField>

    <asp:TemplateField HeaderText="seller">
      <EditItemTemplate>
         <asp:TextBox ID="tbseller" runat="server" Text='<%# 
           Bind("seller") %>'></asp:TextBox>
       </EditItemTemplate>
       <ItemTemplate>
      <asp:Label ID="lblseller" runat="server" Text='<%# Bind("seller") 
        %>'></asp:Label>
     </ItemTemplate>
       <HeaderStyle CssClass="hiddencol" />
        <ItemStyle CssClass="hiddencol" />
     </asp:TemplateField>

    <asp:buttonfield buttontype="Button" 
            commandname="View"
        text="View"/>
</Columns>
   </asp:GridView>

 //Code behind
     protected void QgridView_RowCommand(object sender, 
       GridViewCommandEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("onrowcommand");

        if (e.CommandName == "View")
        {
            System.Diagnostics.Debug.WriteLine("ButtonView is clicked");
            Response.Redirect("ListingItems.aspx");

        }

        else if (e.CommandName == "Accept")
        {
            System.Diagnostics.Debug.WriteLine("buttonAccept is 
      clicked");

            productDAO productdao = new productDAO();
            //GridViewRow row = QgridView.SelectedRow;

       //Get Row data
      GridViewRow gvRow 
       (GridViewRow(((Button)e.CommandSource).NamingContainer);

       Label lbitemid = (Label)gvRow.FindControl("lblitemid"); 
        int id = Convert.ToInt32(lbitemid.Text); //Error returned

       //int quoteid = Convert.ToInt32(row.Cells[7].Text);


      System.Diagnostics.Debug.WriteLine("itemid = ", id);

            //productdao.GridPush(quoteid, id);
        }

        else
        {
            System.Diagnostics.Debug.WriteLine("no command name");
        }


    }

如何获取RowCommand中每一行的数据?

1 个答案:

答案 0 :(得分:0)

根据您的代码简单地更改控件ID和类型。我认为以下一项必须为您工作。 试试这个:

if (e.CommandName == "Accept")
    {
        Label arr = ((Label)GridView1.Rows[CurrentRow.RowIndex].FindControl("lblitemid"));
        string asdf= arr.Text.ToString(); 
     }

第二个选项:

行DataBound事件代码:

Button btnObject = e.Row.FindControl("Button1") as Button;
btnObject.CommandArgument = e.Row.RowIndex;

ButtonClick事件:

protected void btnObject_Click(object sender, CommandEventArgs e)
{
  int rowIndex = Convert.ToInt16(e.CommandArgument);

  TextBox arr = 
((TextBox)GridView1.Rows[CurrentRow.RowIndex].FindControl("TextBox1"));
  string asdf= arr.Text.ToString();

}

第三种选择:

protected void grdRules_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{

   GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
   LinkButton lb = (LinkButton)row.FindControl(”lnkbtnActionNames”);
   if (lb != null)
   {
        //Do something
   }
}