简体   繁体   English

读取gridview单元格值和文本

[英]Read gridview cell values and text

I am using the below code to read gridview controls value or text. 我正在使用下面的代码来读取gridview控件的值或文本。 But it return null value. 但是它返回空值。 I can't find out. 我找不到 But the gridview having some record with the value. 但是gridview具有一些具有该值的记录。 Please help me to solve this. 请帮我解决这个问题。

 GridViewRow row = null;
 for (int i = 0; i < grd.Rows.Count; i++)
 {
      row = grd.Rows[i];
      HiddenField file_id = (HiddenField)row.FindControl("FLD_ID");
      Label pack_name = (Label)row.FindControl("FLD_NAME");
      string text = file_id.Value;
      bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

      if (isChecked)
      {
           //Here its comming
      }
  }

My partial gridview code here 我的部分GridView代码在这里

 <asp:GridView ID="grd" runat="server" BackColor="#CCCCCC" BorderColor="#93afba"
                                    BorderStyle="Solid" BorderWidth="1px" CellPadding="4" AllowPaging="True" AutoGenerateColumns="False"
                                    ShowHeaderWhenEmpty="True" PageSize="50" Width="100%" CellSpacing="2" ForeColor="#93afba"
                                    Font-Size="14px">
                                    <Columns>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                <%--<asp:CheckBox ID="chkall" runat="server" OnChange="checkAll();" />--%>
                                                <input id="Checkbox2" type="checkbox" onclick="CheckAll(this)" runat="server" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chkSelect" runat="server" />
                                            </ItemTemplate>
                                            <ItemStyle Width="20px" />
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="FLD_ID" Visible="false">
                                            <ItemTemplate>
                                                <asp:HiddenField ID="lbl_id" runat="server" Value='<%#Bind("FLD_ID") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                          <asp:TemplateField HeaderText="NAME">
                                            <ItemTemplate>
                                                <asp:Label ID="lbl_packname" runat="server" Text='<%#Bind("FLD_NAME") %>' ></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>

your code is fine. 您的代码很好。

if you are binding gridview on page load be sure your code is as below block 如果您在页面加载时绑定gridview,请确保您的代码如下

if (!IsPostBack)
            {
                //code to bind gridview
            }

Change the code to and check: 将代码更改为并检查:

for (int i = 0; i < grd_proforma_bill.Rows.Count; i++)  
{ 
  HiddenField file_id = (HiddenField)grd_proforma_bill.Rows[i].FindControl("lbl_id");  
  Label pack_name = (Label)grd_proforma_bill.Rows[i].FindControl("lbl_packname");  
  string text = file_id.Value;  
  CheckBox cb = (CheckBox)grd_proforma_bill.Rows[i].FindControl("Checkbox2"); 
  bool isChecked = cb.Checked;

  if (isChecked)  
  {  
       //Here its comming  
  }  
}  

i think you want to find Checkbox2 and you are finding chkSelect 我认为您想找到Checkbox2,并且您正在找到chkSelect

please replace this 请更换这个

bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

with

bool isChecked = ((System.Web.UI.HtmlControls.HtmlInputCheckBox)grd_proforma_bill.HeaderRow.FindControl("Checkbox2")).Checked;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM