Controls on Formview - I guess I have made a fundemental Mistake RRS feed

  • Question

  • User129150582 posted
    Hope someone can point me in the right direction here as I am new to vb.net. In summary I have a web application under development which uses a Master page (not sure that is relevant for this issue). There is a page that allows for the selection of a Clients details from an Ajax Toolkit DDL, this then fills the Formview with the relevant data for updating / changing via Textboxes and bound DDL's to other tables via foreign keys. This all works fine, so I thought)
    I then added a series of Command buttons to the Formview, that I would use to carry out some specific changes to the current record displayed. I have used the following code to locate the control on the Formview :-
    Public Class clientprocess
        Inherits System.Web.UI.Page
        Protected Sub Formview1_Databound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
            Dim Cmdtxterm As Button = CType(FormView1.FindControl("cmdTXTerm"), Button)
            Dim ddlvenue As DropDownList = CType(FormView1.FindControl("ddlvenue"), DropDownList)
        End Sub
    The Msgbox fires every time I change to a different client in the Formview, so I know the Databound event is firing. The problem I have is that I have added some code to a button on the Formview to do some date manipulation :-
    Public Sub Cmdtxterm(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim ddlVenue As DropDownList = CType(FormView1.FindControl("ddlVenue"), DropDownList)
            Dim ddltermID As DropDownList = CType(FormView1.FindControl("ddltermID"), DropDownList)
            Dim termStartTextBox As TextBox = CType(FormView1.FindControl("termStartTextBox"), TextBox)
            Dim dayofweekOffset As Integer = 0
            Dim nextDateCurosr As Integer = 0
            Dim nextDate As Date
            Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim cmdgetDates As SqlCommand = New SqlCommand(("SELECT * FROM classTerms WHERE term LIKE 'T%' ORDER BY termSTART DESC"))
            Dim apcmdgetDates As SqlDataAdapter = New SqlDataAdapter(cmdgetDates.CommandText, conn)
            Dim dscmdgetDates As DataSet = New DataSet
            Dim dateCursor As Integer = 0
            Dim i As Integer = 0
            Select Case ddlVenue.SelectedItem.Text
                Case "Test 1, "Test 2"
                    dayofweekOffset = 0
                Case "Test 3", "Test 4"
                    dayofweekOffset = 1
                Case "Test 5"
                    dayofweekOffset = 2
                Case "Test 6"
                    dayofweekOffset = 4
            End Select
            For Each Row As DataRow In dscmdgetDates.Tables(0).Rows
                If (Row.Item("termStart")) = CDate(termStartTextBox.Text) Then
                    If i = 0 Then
                        MsgBox("No more terms, you need to add a new term")
                        Exit For
                    End If
                    dateCursor = i
                    nextDateCurosr = dateCursor - 1
                    nextDate = dscmdgetDates.Tables(0).Rows(nextDateCurosr)("termstart")
                    MsgBox("Next term Date is " & nextDate & " " & nextDateCurosr & " Offset = " & dayofweekOffset)
                    Exit For
                End If
                i = i + 1
        End Sub
    This is experimental POC code and this works as anticipated, however here is the gotcha - When my clientprocess.aspx form is called it has always been prepolulated with the first row of its coresponding sqldatasource, not by design that just happened (something I would attend to later). So all of my testing was done on this initial 'landing record' which worked! However if I then change to another record and click the cmdtxterm button nothing happens.
    From a lay point of view and excuse the terminolgy as it may be off - it is as if the Button control is only instantiated on the first record (or instance of the Formviews Databound event) and never again. I can confirm that if I change record from the 'landing page' before clicking the button it never fires on other records, but on returning to the first record it fires fine.
    As the title describes, I am expecting this to be a fundemental lack of understanding on my part.
    Thanks for any help.
    Sunday, March 25, 2018 2:11 PM

All replies