我在遇到特定情況時遇到問題。我有一箇中繼器控制,在我有一個佔位符(爲了測試的緣故,我已經放置了一個div並嘗試了)。我想,在我的代碼頁面後,找到該佔位符或div,並將控件(現在,3個文本框和2個按鈕)在中繼器控件中查找Div或佔位符,並向其添加控件
我有沒有問題向佔位符添加控件,我知道如何這是有效的,我無法得到的是如何在這裏找到這個動態控制。
下面是一些代碼片段,以幫助說明我正在嘗試做什麼。
(在ASP)
<asp:Repeater id="rptSpecialNotes" runat="server">
<ItemTemplate>
<asp:UpdatePanel ID="udpSpecialNotesRepeater" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="plhSpecialNotesRepeater" runat="server">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<p><%# eval("subject") %></p>
<div id="specialNotes" runat="server"></div>
<asp:imagebutton runat="server" AlternateText="add Motion" ImageUrl="images/controls/Exclaim.png" width="40" height="40"></asp:imagebutton>
<asp:imagebutton runat="server" AlternateText="add document" ImageUrl="images/controls/Documents.png" width="40" height="40"></asp:imagebutton>
<asp:imagebutton runat="server" AlternateText="move up" ImageUrl="images/controls/Arrow-Up.png" width="40" height="40" CommandName="moveUp" CommandArgument='<%# Container.ItemIndex & "," & eval("ItemID") %>'></asp:imagebutton>
<asp:imagebutton runat="server" AlternateText="move down" ImageUrl="images/controls/Arrow-Down.png" width="40" height="40" CommandName="moveDown" CommandArgument='<%# Container.ItemIndex & "," & eval("ItemID") %>'></asp:imagebutton>
<asp:imagebutton runat="server" AlternateText="delete" ImageUrl="images/controls/Delete.png" width="40" height="40" CommandName="delete" CommandArgument='<%# Container.ItemIndex & "," & eval("ItemID") %>'></asp:imagebutton>
<asp:imagebutton runat="server" AlternateText="edit" ImageUrl="images/controls/Globe.png" width="40" height="40" CommandName="edit" CommandArgument='<%# Container.ItemIndex & "," & eval("ItemID") %>'></asp:imagebutton>
</ItemTemplate>
</asp:Repeater>
圖像按鈕編輯,打rptSpecialNotes_Item命令,該命令調用一個方法(編輯特別說明),它看起來像這樣
Dim commandArguments() As String = Split(e.CommandArgument, ",")
Dim divId As String = commandArguments(0)
Dim itemId As String = commandArguments(1)
'determine action based on command name
If e.CommandName = "edit" Then
Call editSpecialNotes(itemId,divId)
End If
和編輯特別說明將東西放在給定的divID中。爲了達到這個目的,我給了divID一個我知道存在的靜態值(rptSpecialNotes_plhSpecialNotesRepeater_1)或類似的範圍。但是,我總是以null對象引用結束。
所以,如果我做這樣的事情? Dim plh As PlaceHolder = Me.Page.FindControl(「rptSpecialNotes_udpSpecialNotesRepeater_0」)我應該找到控件。但是,這仍然傳播錯誤。 – Lenigod 2012-08-01 20:02:59
啊,你知道,findcontrol沒有出錯,但是我不能在之後添加任何控件。 像這樣的東西:plh.controls.add(lbl) 錯誤出來 – Lenigod 2012-08-01 20:05:57
@Lenigod - 這是別的。你有一個異常和堆棧跟蹤? – Oded 2012-08-01 20:06:53