简体   繁体   中英

Read gridview cell values and text

I am using the below code to read gridview controls value or text. But it return null value. I can't find out. But the gridview having some record with the value. 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

 <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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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