2015-02-06 44 views
1

中獲取文本我有這樣的GridView:不能從一個TemplateField一個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]。文本返回文本字符串,但e.Row.Cells [1] .Text返回「」。任何人都知道如何從單元格中獲取文本?

回答

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

感謝你的東西,就像一個魅力。 – 2015-02-06 08:33:30

+0

@PresidentCamacho歡迎您將其標記爲答案,如果認爲這有幫助... :) – 2015-02-06 08:34:20

+0

是的,但我必須等15分鐘才能創建問題。 – 2015-02-06 08:36:41