如何从GridView中的模板字段获取值?

这是我的GridView的标记。

<Columns> <asp:TemplateField HeaderText="Customer Name"> <ItemTemplate> <asp:Label ID="lblname" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Customer.Name")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="PickUpPoint"> <ItemTemplate> <asp:Label ID="lblPickUpPoint" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Pickuppoint")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> 

我有一个button,它将值存储在excel对象的工作表单元格中。

 for (int i = 0; i < GridView2.Rows.Count; i++) { for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++) { xlWorkSheet.Cells[i + 1, j + 1] = GridView2.Rows[i].Cells[j].Text; } } 

如何获取GridView的值并将其存储在工作表中,如GridView2.Rows[i].Cells[j] .Text返回空string。

你缺less一个types转换。 像这样做-

 Label name = (Label)GridView2.Rows[i].Cells[j].FindControl("lblname"); xlWorkSheet.Cells[i + 1, j + 1] = name.Text; 

更新 –如果您可以将您的标签命名为Label0和Label1,那么在第二个for loop-

 for (int j = 0; j < GridView2.Rows[i].Cells.Count; j++) { Label xyz = (Label)GridView2.Rows[i].Cells[j].FindControl("Label"+j); xlWorkSheet.Cells[i + 1, j + 1] = xyz.Text; } 

对于标题文本string hText = GridView2.HeaderRow.Cells[your column number].Text;

为了检索值,请执行以下操作:

 for (int i = 0; i < GridView2.Rows.Count; i++) { //extract the TextBox values Label lblname= (Label)GridView2.Rows[i].Cells[0].FindControl("lblname"); Label lblPickUpPoint= (Label)GridView2.Rows[i].Cells[0].FindControl("lblPickUpPoint"); //Do your excel binding here } 

尝试使用这个代码,我有一个类似的问题。 我认为这个代码更具dynamic性,你不必每次都find标签的名字。 但要保持控件之间的对应关系和索引控件[]:

 for (int row = 1; row <= totalRows; row++) { for (int col = 0; col < totalCols; col++) { if (GridView1.Columns[col].Visible) { if (String.IsNullOrEmpty(GridView1.Rows[row - 1].Cells[col].Text)) { if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("Label")) { Label LB = (Label)GridView1.Rows[row - 1].Cells[col].Controls[1]; workSheet.Cells[row + 1, col + 1].Value = LB.Text; } else if (GridView1.Rows[row - 1].Cells[col].Controls[1].GetType().ToString().Contains("LinkButton")) { LinkButton LB = (LinkButton)GridView1.Rows[row - 1].Cells[col].Controls[1]; workSheet.Cells[row + 1, col + 1].Value = LB.Text; } } else { workSheet.Cells[row + 1, col + 1].Value = GridView1.Rows[row - 1].Cells[col].Text; } 

{cell}.Text只有在TemplateField没有控件时才能使用。 您已经为模板添加了一个标签,这就是为什么您首先需要查找控件,将对象转换为控件并根据需要访问控件的属性的原因。

如果你想要一个更通用的方法,你总是可以做到以下几点(删除标签控件,只需添加评估字段):

 <Columns> <asp:TemplateField HeaderText="Customer Name"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Customer.Name")%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="PickUpPoint"> <ItemTemplate> <%# DataBinder.Eval(Container.DataItem, "Pickuppoint")%> </ItemTemplate> </asp:TemplateField> </Columns> 

当您使用最初使用的代码时, {cell}.Text不应再返回空白。

  protected void Button1_Click(object sender, EventArgs e) { int i = 0; for (i = 0; i <= GvSchedule.Rows.Count - 1; i++) { if (((CheckBox)GvSchedule.Rows[i].FindControl("ChkIsService")).Checked) { string catName = ((Label)GvSchedule.Rows[i].FindControl("lblCatName")).Text; var subCatName = ((Label)GvSchedule.Rows[i].FindControl("lblSubCatName")).Text; } } } 

使用此types“Button btnledName =(Button)sender;”它会帮助你保护无效btnledName_Click(对象发件人,EventArgs e){

  Button btnledName = (Button)sender; GridViewRow Grow = (GridViewRow)btnledName.NamingContainer; Label lblname = new Label(); Label lblPickUpPoint= new Label(); lblname = (Label)Grow.FindControl("lblname"); lblPickUpPoint = (Label)Grow.FindControl("lblname"); #region if (lblname.Text.Length > 0) { //Add in your work sheet } #endregion }