2014-09-12 69 views
-3

我發現的所有示例都沒有幫助。我的gridview腳本中有一個名爲「txtMyCoverage」的文本框。在我隱藏我只想引用一個文本框說如何引用從我的代碼隱藏的GridView中的文本框C#

a = txtMyCoverage.Text; 

我試圖

gvLimit.FindControl("txtMyCoverage").ToString(); 

但它回來空引用。謝謝你的幫助!

這裏是我的網

<asp:GridView Width="100%" ID="gvLimits" 
CssClass="gridView no_min_width" 
runat="server" 
AutoGenerateColumns="False" 
CellPadding="2" ShowFooter="true" 
DataKeyNames="PolicyLimitID, LimitID" 
HorizontalAlign="Center" 
OnRowDataBound="gvLimits_RowDataBound"> 
<RowStyle HorizontalAlign="Center" /> 
<Columns> 
    <asp:TemplateField HeaderText="Coverage"> 
     <ItemTemplate> 
      <asp:Label ID="txtLimitLetter" runat="server" Width="20px" CssClass="center" Text='<%# Bind("Limit.LimitLetter") %>' Enabled="false" ></asp:Label> 
     </ItemTemplate> 
     <FooterTemplate> 
      <asp:TextBox ID="txtMyCoverage" runat="server" Width="50px" CssClass="center" ></asp:TextBox> 
     </FooterTemplate> 

</Columns> 
</asp:GridView> 
+1

你爲什麼試圖將控件轉換爲字符串? – melancia 2014-09-12 14:52:04

+1

你能告訴你如何在標記中定義txtMyCoverage嗎?你是否有runat =「」並且你是否分配了ID?請提供/顯示更多信息 – MethodMan 2014-09-12 14:53:37

+0

向我們顯示您的HTML代碼(DataGrid)。你有沒有添加runat =「服務器」屬性? – 2014-09-12 14:53:56

回答

0

如果使用文本框像這樣:

<asp:TemplateField HeaderText="Coverage"> 
     <ItemTemplate> 
      <asp:Label ID="txtLimitLetter" runat="server" Width="20px" CssClass="center" Text='<%# Bind("Limit.LimitLetter") %>' Enabled="false" ></asp:Label> 
     </ItemTemplate> 
     <FooterTemplate> 
      <asp:TextBox ID="txtMyCoverage" runat="server" Width="50px" CssClass="center" ></asp:TextBox> 
     </FooterTemplate> 

那麼你可以找到rowbound這樣的:

在RowBoundEvent電網

protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.Footer) 
{ 
TextBox txtLimitLetter=(TextBox)e.Row.FindControl("txtLimitLetter"); 
} 

} 

或 你可以通過GridView查看行

foreach(GridViewRow row in GridView2.Rows) 
{ 
    if (e.Row.RowType == DataControlRowType.Footer) 
    { 
    TextBox txtLimitLetter=(TextBox)e.Row.FindControl("txtLimitLetter"); 
    } 

} 
+0

謝謝@Ganesh_Devlekar,它爲之奮鬥! – Omar 2014-09-12 15:07:51

+0

@Omar歡迎你 – 2014-09-12 15:15:00