President Camacho
President Camacho

Reputation: 1920

Can't get text from a TemplateField within a gridview

I've this 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>

and this code:

   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 returns a text string but e.Row.Cells[1].Text returns "". Anyone knows how to get the text from the cell?

Upvotes: 1

Views: 1196

Answers (1)

Dgan
Dgan

Reputation: 10295

You declare Label in side TemplateField so You can try something like this

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

Upvotes: 4

Related Questions