我需要一個建議如何糾正我的代碼。我正在使用FindControl方法在Repeater中查找TextBox。這是我的標記:在Repeater中使用FormView的FindControl方法
<asp:Repeater ID="Repeater1">
HERE ARE SOME OTHER DATA
<ItemTemplate>
<asp:FormView ID="FormViewAddComment" runat="server"
DataSourceID="SqlDataSourceInsertComments" DefaultMode="Insert"
OnItemInserted="FormViewAddComment_ItemInserted"
OnItemInserting="FormViewAddComment_ItemInserting">
<InsertItemTemplate>
<asp:TextBox ID="txtAddComment" runat="server" CssClass="textbox"
Text='<%# Bind("CommentText") %>' Width="200px" />
<asp:Button ID="btnAddComment" runat="server" CssClass="button"
Text="Comment" CommandName="Insert" CausesValidation="false"/>
</InsertItemTemplate>
</asp:FormView>
</ItemTemplate>
</asp:Repeater>
這是我的代碼背後:
Protected Sub FormViewAddComment_ItemInserting(sender As Object, e As FormViewInsertEventArgs)
Dim FormView As FormView = DirectCast(Repeater1.FindControl("FormViewAddComment"), FormView)
Dim Comment As TextBox = DirectCast(FormView.FindControl("txtAddComment"), TextBox)
If Comment.Text = "" Then
Exit Sub
End If
End Sub
的註釋文本框沒有發現,當它試圖訪問Text屬性代碼拋出一個對象引用錯誤。
我這樣做是因爲頁面無法自動找到FormViewAddcomment,因爲它找不到txtAddComment。 – jstorm31 2013-02-26 08:45:35
看到編輯,讓我知道如果這可以幫助你... – 2013-02-26 10:22:25
是的,我會在晚上嘗試它 – jstorm31 2013-02-26 12:59:33