读取gridview单元格值和文本

时间:2013-03-16 10:12:17

标签: c# asp.net c#-4.0

我使用以下代码来读取gridview控件的值或文本。但它返回null值。我找不到。但是gridview有一些值的记录。请帮我解决这个问题。

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

我的部分网格视图代码

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

3 个答案:

答案 0 :(得分:0)

你的代码很好。

如果您在页面加载时绑定gridview,请确保您的代码如下所示

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

答案 1 :(得分:0)

  

将代码更改为并检查:

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

答案 2 :(得分:0)

我认为你想找到 Checkbox2 ,你发现 chkSelect

请替换此

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

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