2013-12-12 84 views
0

我正在創建一個具有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 
+0

Button6在哪裏?你不能直接引用'PromotionSqlDataSource'嗎? – Jumpei

+0

它在formview中。 (我編輯了上面的代碼來顯示按鈕) – Joe

+0

我不確定你的意思是指它的sqldatasource。如果您在按鈕上設置了DataSourceID =「PromotionSqlDataSource」,它仍然不起作用。 – Joe

回答

0

只是跟進了我必須要做的事情來解決這個問題。這必須是MS Visual Studio編譯代碼的錯誤。它應該與上面的代碼一起工作,但它從來沒有。我做了每個按鈕一個CommandName =「插入」按鈕。基準asp.net代碼的工作方式是它觸發button_click代碼,然後是sqldataserver插入代碼。

我用每個按鈕的onClick事件創建了一個全局標誌。在sqldatasource的after_Inserted事件中,我讀取標誌以確定哪個按鈕被點擊。在插入事件之後,我有一個if then else語句,用於根據標記的設置重定向頁面。

不優雅,但它正在工作,我想它如何工作。

0

我認爲你可以使用PromotionSqlDataSource不使用FindControl。 請在下面嘗試。

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

我試過這個開始。不幸的是,它也無法如此。 – Joe

+0

不起作用...怎麼樣?我可以收到錯誤訊息嗎? – Jumpei

相關問題