【问题标题】:asp net c# get value of specific row in gridview using checkboxasp net c#使用复选框获取gridview中特定行的值
【发布时间】:2021-05-13 02:31:48
【问题描述】:

我有一个包含三列管理员 ID、管理员名称和复选框的网格视图,并且网格视图下方有一个按钮,

当我选中复选框然后单击按钮时,我想获取管理员名称列的值并将其显示在标签中,

我试过这样做,它工作正常,但我只能得到一个值,例如,如果我选中了两个复选框,然后单击按钮,它只得到最后一个值,我想全部获取。

<asp:GridView ID="GridView1"  CssClass="datatable"  runat="server" AutoGenerateColumns="False" AllowPaging="True"  PageSize="10"  
             AllowSorting="True" EnableViewState="false" width="863px" DataSourceID="SqlDataSource1"  >

<Columns >
                                                                                                                                            
    <asp:TemplateField HeaderText="req_id"  >          
         <ItemTemplate >
             <asp:Label ID="label_id" runat="server" Text='<%# Eval("admin_id") %>' ></asp:Label>
         </ItemTemplate>
         <ItemStyle Width="20px" />
         <HeaderStyle  Width="30px" />
         <FooterStyle Width="20px" />  
     </asp:TemplateField>
            
     <asp:TemplateField HeaderText="admin_number">          
         <ItemTemplate >
             <asp:Label ID="label_name" runat="server" Text='<%# Eval("admin_name") %>' ></asp:Label>
         </ItemTemplate>
         <ItemStyle Width="20px" />
         <HeaderStyle  Width="60px" />
         <FooterStyle Width="60px" />  
     </asp:TemplateField>
                                                                                                                                            
    <asp:TemplateField HeaderText="privileges" >
        <ItemTemplate>
            <asp:CheckBox ID="CheckBox_privileges" runat="server"  Checked='<%#   Convert.ToInt32(Eval("prev_1")) == 1 ?  true : false %>'  />

        </ItemTemplate>
        <ItemStyle Width="90px" />
        <HeaderStyle  Width="90px" />
        <FooterStyle Width="90px" />  
    </asp:TemplateField>

 </Columns>

    protected void btn_save(object sender, EventArgs e)
    {

          foreach (GridViewRow row in GridView1.Rows)
          {
           if (row.RowType == DataControlRowType.DataRow)
            {
            CheckBox chkRow = (row.Cells[2].FindControl("CheckBox_privileges") as CheckBox);
            if (chkRow.Checked)
            {
                string name= (row.Cells[1].FindControl("label_name") as Label).Text;

                Label4.Text = name;
            }
            }
          }
          
      
 }

【问题讨论】:

    标签: c# asp.net checkbox gridview


    【解决方案1】:

    在这一行中,您将覆盖文本:

    Label4.Text = name;
    

    要添加所有文本,您应该这样做:

    Label4.Text = name + Label4.Text;
    

    【讨论】:

      猜你喜欢
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 2018-04-29
      相关资源
      最近更新 更多