我正在創建一個具有formview的DNN頁面。 sqldatasource位於formview的外部。我需要控制代碼隱藏的sqldatasource.insert()調用。 (有2個按鈕--1個插入並進入一個頁面,1個插入並將formview更改爲編輯模式以添加其他數據)。asp.net VB FindControl無法在formview外部找到控件
代碼隱藏找不到窗體視圖之外的控件。我將僅將代碼粘貼到前端sqldatasource(formview非常複雜且很長)。
我正在使用遞歸的findcontrol。我開始查看me.page級別(頂級?),但我仍然獲得對sql數據源的空引用。 (不能找到它) (代碼工作時,我但常規命令=插入按鈕,但我需要控制一個重定向取決於哪個按鈕被按下)
任何想法?
前端SQL數據源:
<asp:SqlDataSource ID="PromotionSqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings%>"
InsertCommand="INSERT code is here"
SelectCommand="select code is here"
UpdateCommand="UPDATE code is here"
DeleteCommand="DELETE code is here">
<InsertParameters>
lots of parameters
</InsertParameters>
<EditParameters>
lots of parameters
</EditParameters>
<DeleteParameters>
</DeleteParameters>
</asp:SqlDataSource>
FormView控件:
<asp:FormView ID="FormView1" runat="server" AllowPaging="True"
DataKeyNames="Promo_ID"
DataSourceID="PromotionSqlDataSource" DefaultMode="Insert">
Lots of form code here
<asp:Button ID="Button6" runat="server" Text="Next" onclick="Button6_Click" />
</asp:FormView>
後面的代碼:
Protected Sub Button6_Click(sender As Object, e As System.EventArgs)
MessageBox("BUTTON 6 CLICK")
Dim PromotionSqlDataSource As SqlDataSource = TryCast(FindControlRecursive(Me.Page, "PromotionSqlDataSource"), SqlDataSource)
PromotionSqlDataSource.Insert()
FormView1.ChangeMode(FormViewMode.Edit)
End Sub
Public Function FindControlRecursive(root As Control, id As String) As Control
If root.ID = id Then
Return root
End If
Return root.Controls.Cast(Of Control)().[Select](Function(c) FindControlRecursive(c, id)).FirstOrDefault(Function(c) c IsNot Nothing)
End Function
Button6在哪裏?你不能直接引用'PromotionSqlDataSource'嗎? – Jumpei
它在formview中。 (我編輯了上面的代碼來顯示按鈕) – Joe
我不確定你的意思是指它的sqldatasource。如果您在按鈕上設置了DataSourceID =「PromotionSqlDataSource」,它仍然不起作用。 – Joe