【问题标题】:Get Cell Value in C# Gridview when Button clicked on row当按钮单击行时在 C# Gridview 中获取单元格值
【发布时间】:2013-06-28 13:45:03
【问题描述】:

我有一个gridviewMain。每当我单击第 1 行的关闭链接按钮时,我都想获得值 aa。单击第 2 行上的关闭获取 bb 的值。任何想法。

A    B   C  
aa  xx   3  CLOSE 
bb  yy   4  CLOSE
cc  zz   5  CLOSE

aspx

 <asp:BoundField DataField="afield" HeaderText="A" 
     SortExpression="afield" >
    <ItemStyle HorizontalAlign="Center" />
 </asp:BoundField>
 <asp:BoundField DataField="bfield" HeaderText="B" SortExpression="cfield" >
     <ItemStyle HorizontalAlign="Center" />
  </asp:BoundField>
  <asp:BoundField DataField="cfield" HeaderText="C" SortExpression="cfield" >
     <ItemStyle HorizontalAlign="Center" />
   </asp:BoundField>
 <asp:TemplateField ShowHeader="False">

       <ItemTemplate>
          <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False"  CommandName="CloseClicked" Text="Close"></asp:LinkButton>

        </ItemTemplate>

                    </asp:TemplateField>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    假设您在前三列中使用BoundFields,并且您希望处理LinkButton-click 事件(而不是GridView 中的RowCommand):

    protected void CloseLinkClicked(Object sender, EventArgs e)
    {
        var closeLink = (Control) sender;
        GridViewRow row = (GridViewRow) closeLink.NamingContainer;
        string firstCellText = row.Cells[0].Text; // here we are
    }
    

    如果您使用 TemplateFields 并且值 aa 在标签中(例如 LblValue):

    Label lblValue = (Label) row.FindControl("LblValue"); // lblValue.Text = "aa"
    

    【讨论】:

    • @Apollo:你不需要处理RowCommand,只需处理LinkButtonClick-event:&lt;asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" OnClick="CloseLinkClicked" Text="Close"&gt;&lt;/asp:LinkButton&gt;
    【解决方案2】:

    为此使用数据键。简单得多:

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="SomeValue, AnotherValue" ... >
    

    然后在代码隐藏中,只需获取与行关联的键:

    var rowIndex = 0;
    var someValue = GridView1.DataKeys[rowIndex]["SomeValue"] as string;
    

    【讨论】:

      【解决方案3】:

      您需要在GridView 标记中将CommandName 分配给您的LinkButton 列。从那里您还需要连接OnRowCommand 事件来处理您的Close 命令。

      下面是在GridView 上使用Add 命令的示例:

      Markup:
      <asp:gridview id="ContactsGridView" datasourceid="ContactsSource" allowpaging="true" 
          autogeneratecolumns="false" onrowcommand="ContactsGridView_RowCommand" runat="server">
          <columns>
             <asp:buttonfield buttontype="Link" commandname="Add" text="Add"/>
             <asp:boundfield datafield="ContactID" headertext="Contact ID"/>
             <asp:boundfield datafield="FirstName" headertext="First Name"/> 
             <asp:boundfield datafield="LastName" headertext="Last Name"/>
          </columns>
      </asp:gridview>
      
      Code-Behind:
      void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
          // If multiple buttons are used in a GridView control, use the
          // CommandName property to determine which button was clicked.
          if(e.CommandName=="Add")
          {
              // Convert the row index stored in the CommandArgument
              // property to an Integer.
              int index = Convert.ToInt32(e.CommandArgument);
      
              // Retrieve the row that contains the button clicked 
              // by the user from the Rows collection.
              GridViewRow row = ContactsGridView.Rows[index];
      
              // Create a new ListItem object for the contact in the row.     
              ListItem item = new ListItem();
              item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
              Server.HtmlDecode(row.Cells[3].Text);
      
              // If the contact is not already in the ListBox, add the ListItem 
              // object to the Items collection of the ListBox control. 
              if (!ContactsListBox.Items.Contains(item))
              {
                  ContactsListBox.Items.Add(item);
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        你可以这样

        <asp:GridView ID="grid" runat="server" 
        AutoGenerateColumns="false" onrowcommand="grid_RowCommand" >
        <Columns>
        <asp:TemplateField >
        <ItemTemplate>
        <asp:TextBox ID="txt" runat="server" Text='<%#Eval("xxx")%>'></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
        <ItemTemplate>
        <asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %> 
        CommandName="arg">Click</asp:LinkButton>
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
        

        代码

         protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
        int rowindex = int.Parse(e.CommandArgument.ToString());
        ((TextBox)(grid.Rows[rowindex].FindControl("txtgvunit"))).Text
        } 
        

        参考 http://dotnetinbox.blogspot.in/2014/02/get-gridview-row-data-in-c.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-14
          • 2023-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多