【问题标题】:asp.net VB Findcontrol cannot find control outside of formviewasp.net VB Findcontrol 在窗体视图之外找不到控件
【发布时间】: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


【解决方案1】:

我认为你可以使用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

【讨论】:

  • 我一开始就试过了。不幸的是,它也不是那样工作的。
【解决方案2】:

只是跟进我必须采取哪些措施来解决此问题。这一定是 MS Visual Studio 编译代码的方式中的一个错误。它应该与上面的代码一起工作,但它从来没有。我将每个按钮都设为 CommandName="Insert" 按钮。基本 asp.net 代码的工作方式是触发 button_click 代码,然后触发 sqldataserver 插入代码。

我创建了一个使用每个按钮的 onClick 事件设置的全局标志。在 sqldatasource 的 after_Inserted 事件上,我读取了标志以确定单击了哪个按钮。我在 after Inserted 事件中有一个 if then else 语句,它根据标志的设置方式重定向页面。

不优雅,但它完全按照我想要的方式工作。

【讨论】:

    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2010-12-10
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    相关资源
    最近更新 更多