【发布时间】:2013-12-30 05:38:42
【问题描述】:
我正在创建一个具有表单视图的 DNN 页面。 sqldatasource 位于表单视图的外部。我需要控制来自代码隐藏的 sqldatasource.insert() 调用。 (有 2 个按钮 - 1 个用于插入并转到一页,1 个用于插入并将 formview 更改为编辑模式以添加其他数据)。
代码隐藏在表单视图之外找不到控件。后面的代码我只贴前端的sqldatasource(formview很复杂很长)。
我正在使用递归查找控件。我开始查看 me.page 级别(顶级?),但我仍然得到对 sql 数据源的空引用。 (找不到) (当我使用常规的 command=insert 按钮时,代码可以工作,但我需要根据按下的按钮来控制重定向)
有什么想法吗???
前端 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>
窗体视图:
<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吗? -
它在表单视图中。 (我编辑了上面的代码以显示按钮)
-
我不确定您将其引用到 sqldatasource 是什么意思。如果在按钮上设置了DataSourceID="PromotionSqlDataSource",还是不行。
标签: asp.net vb.net dotnetnuke formview findcontrol