这样写取不到值
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="id">
                    <ItemTemplate><asp:Label ID="lblId" runat="server"><%#Eval("id") %></asp:Label></ItemTemplate>                </asp:TemplateField>
                <asp:TemplateField HeaderText="name">
                    <ItemTemplate><asp:Label ID="lblName" runat="server"><%#Eval("name") %></asp:Label></ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>但写成这样就可以取到值了
<ItemTemplate><asp:Label ID="lblId" runat="server" Text='<%#Eval("id") %>'></asp:Label></ItemTemplate>
取值是用  ((Label)(GridView1.Rows[0].FindControl("lblId"))).Text
为什么呢
直接在页面上放一个<asp:Label id="Label1">1</asp:Label>,也是可以取到值的

解决方案 »

  1.   

    lable在页面转换为span标签
     innerhtml
    foreach(GridviewRow gr in Gridview1.Rows)
    {
     Label l=gr.FindControl("lblId") as Label;
    }
      

  2.   

    模板列中label绑定的用法就是这样的。绑定的数据要绑定在label的text属性上,text属性有值 你才能取到值。数据没有绑定到 text属性,你去取什么?
      

  3.   

    <ItemTemplate><asp:Label ID="lblId" runat="server"><%#Eval("id") %></asp:Label></ItemTemplate>这样写,并没有将值赋给属性Text
    而你在服务端又是直接通过Text属性去取值  ((Label)(GridView1.Rows[0].FindControl("lblId"))).Text<asp:Label ID="lblId" runat="server"><%#Eval("id") %></asp:Label>就像在客户端直接给label添加innerHtml一样,只不过步骤是在服务器端完成一样
      

  4.   

    <ItemTemplate><asp:Label ID="lblId" runat="server" Text='<%#Eval("id")%>'></asp:Label></ItemTemplate>试试?
      

  5.   

    <ItemTemplate><asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label></ItemTemplate>
      

  6.   

    <asp:TemplateField HeaderText="类型" Visible="False">
                                        <EditItemTemplate>
                                            <asp:TextBox ID="TextBox16" runat="server" Text='<%# Bind("emp_type") %>'></asp:TextBox>
                                        </EditItemTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="Label15" runat="server" Text='<%# Bind("emp_type") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" />
                                        <HeaderStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
      

  7.   

    你看一下生成页面的html源代码   不一样的 
      

  8.   

    为什么不在girdview中写,直接拖个label<asp:Label id="lblName" runat="server">中国</asp:Label>
    用lblName.Text可以取到值呢