无法从gridview中的TemplateField获取文本

时间:2015-02-06 08:25:44

标签: c# asp.net gridview

我的网格视图:

<asp:GridView runat="server" ID="gv_tList" OnRowDataBound="gv_tList_RowDataBound">
        <Columns>
            <asp:BoundField DataField="taskID" HeaderText="taskID" />      
            <asp:TemplateField HeaderText="Description" >
                <ItemTemplate>
                    <asp:Label ID="descr" runat="server" Text='<%# Eval("Description") %>' ToolTip='<%# Eval("descr") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
</asp:GridView>

和这段代码:

   protected void gv_tList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string taskID = e.Row.Cells[0].Text;
            e.Row.Cells[1].Text = e.Row.Cells[1].Text.Length > 40 ? e.Row.Cells[1].Text.Substring(0, 40) + ".." : e.Row.Cells[1].Text;
        }
    }

e.Row.Cells [0] .Text返回一个文本字符串,但e.Row.Cells [1] .Text返回&#34;&#34;。任何人都知道如何从单元格中获取文本?

1 个答案:

答案 0 :(得分:4)

你在TemplateField边声明了标签,所以你可以尝试这样的事情

   protected void gv_tList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           Label descr=(Label)e.Row.FindControl("descr");
           string Mydescr=descr.Text
           string taskID = e.Row.Cells[0].Text;
           descr.Text = descr.Text.Length > 40 ? 
           descr.Text.Substring(0, 40) + ".." : descr.Text;
        }
    }