如何在RowCommand中从Gridview获取行标签的数据? [英] How can I get row label's data from Gridview in RowCommand?

查看:63
本文介绍了如何在RowCommand中从Gridview获取行标签的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a buttonfield to perform updating of database. However, I couldn't get the row data from GridView despite searching for multiple solutions.

我尝试过类似的东西:

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'."

but was not successful either. I got this error : System.InvalidCastException: 'Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type '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中每一行的数据?

How can I get the data for each row in my RowCommand?

推荐答案

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

Simple change the Control ID and type according to your code. I think one of below should must work for you. Try This:

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

第二个选项:

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
   }
}

这篇关于如何在RowCommand中从Gridview获取行标签的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆