Question

I am creating a DNN page that has a formview. The sqldatasource is on the outside of the formview. I need to control the sqldatasource.insert() call from the codebehind. (there are 2 buttons - 1 which inserts and goes to one page, 1 which inserts and changed the formview to edit mode to add additional data).

The codebehind cannot find the control outside of the formview. I will paste the code behind and the front end sqldatasource only (the formview is very complex and long).

I am using a recursive findcontrol. I start looking at the me.page level (top level?), but I still get a null reference to the sql data source. (Cant find it) (The code works when I but a regular command=insert button, but I need to control a redirect depending on which button is pressed)

Any ideas???

Front end SQL data source:

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

Code behind:

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
Was it helpful?

Solution 2

Just a follow up on what I had to do to fix this issue. This MUST have been a bug in the way MS Visual Studio is compiling the code. It should have worked with the code above, but it never did. I made each button a CommandName="Insert" button. The way the base asp.net code works is that it fires the button_click code, then the sqldataserver Insert code.

I created a global flag that is set with the onClick event of each button. On the after_Inserted event of the sqldatasource, I read the flag to determine which button was clicked. I have an if then else statement within the after Inserted event that redirects the pages based on how the flag is set.

Not elegant but it is working exactly how I want it to work.

OTHER TIPS

I think you can use PromotionSqlDataSource without using FindControl. Try below.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top