如何从GridView中的模板字段获取值? [英] How to get values from template fields in GridView?
本文介绍了如何从GridView中的模板字段获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<列>这是我的GridView标记。
< asp:TemplateField HeaderText =客户名称>
< ItemTemplate>
< asp:Label ID =lblnamerunat =serverText ='<%#DataBinder.Eval(Container.DataItem,Customer.Name)%>>< / asp :标签>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =PickUpPoint>
< ItemTemplate>
< asp:Label ID =lblPickUpPointrunat =serverText ='<%#DataBinder.Eval(Container.DataItem,Pickuppoint)%>'>< / asp:Label> ;
< / ItemTemplate>
< / asp:TemplateField>
< /列>
我有一个按钮,它将值存储在excel对象的工作表单元格中。
$ (int i = 0; i< GridView2.Rows.Count; i ++){
for(int j){b
$ b
= 0; j< GridView2.Rows [i] .Cells.Count; j ++)
{
xlWorkSheet.Cells [i + 1,j + 1] = GridView2.Rows [i] .Cells [ J]。文本;
$ b $ p
$ b $ p如何获取GridView的值并将其存储在工作表中,作为 GridView2.Rows [i] .Cells [j]
.Text返回空字符串。
解决方案你缺少一个类型转换。这样做 -
标签名称=(标签)GridView2.Rows [i] .Cells [j] .FindControl( lblname);
xlWorkSheet.Cells [i + 1,j + 1] = name.Text;
更新 - 如果您可以将标签命名为Label0和Label1,在第二个循环中 -
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;
对于标题text- 字符串hText = GridView2.HeaderRow .Cells [你的列号] .Text;
This is my markup of 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>
I have a button which stores the values in the worksheet cells of excel object.
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;
}
}
How do I get the values of GridView and store it in a worksheet, as the GridView2.Rows[i].Cells[j]
.Text returns empty string.
解决方案 Your are missing a type cast. Do it like this-
Label name = (Label)GridView2.Rows[i].Cells[j].FindControl("lblname");
xlWorkSheet.Cells[i + 1, j + 1] = name.Text;
Update- If you can name your labels as Label0 and Label1, then in the second 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;
}
For Header text- string hText = GridView2.HeaderRow.Cells[your column number].Text;
这篇关于如何从GridView中的模板字段获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文