簡體   English   中英

讀取gridview單元格值和文本

[英]Read gridview cell values and text

我正在使用下面的代碼來讀取gridview控件的值或文本。 但是它返回空值。 我找不到 但是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
      }
  }

我的部分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>

您的代碼很好。

如果您在頁面加載時綁定gridview,請確保您的代碼如下

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

將代碼更改為並檢查:

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

我認為您想找到Checkbox2,並且您正在找到chkSelect

請更換這個

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

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