Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Arrgh ! My Save button does a post back
Message
From
09/09/2003 06:31:44
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Arrgh ! My Save button does a post back
Miscellaneous
Thread ID:
00827409
Message ID:
00827409
Views:
58
My Save button does a postback before I get the chance to save to my table.

The click event gets fired after the postback and by this time my daDataRow has vanished and I have to re=-create it.

Having to re-create classes and variables in this manner seems a bit long winded :(

Why does not the application on the server keep these public variables live for me.

I know it can does this sort of thing because it can do it for shared properties/ methods quite happily. The big snag here is that shared P&Ms are GLOBAL! and all users worldwide get to share the same set of variables. In the main this is not desirable to say the least.

At present I have things working, put the whole things still feels a bit Rube Goldberg/Heath Robinson to me.

Here is my code so far. Please let me know of any major boobs I am making
'Imports System.Web
'Imports System.Web.SessionState

Imports System.Diagnostics


Public Class GetDataForm1
    Inherits System.Web.UI.Page
    Protected WithEvents HelpNote1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents PgBanner1 As System.Web.UI.WebControls.Label

    Dim PgStatus As String
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents DropAppName As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DropLeadDept As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents txtSystemOwner As System.Web.UI.WebControls.TextBox
    Protected WithEvents DropSystemFormat As System.Web.UI.WebControls.DropDownList
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents txtSystemDataFirstStored As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents Note As System.Web.UI.WebControls.Label
    Protected WithEvents txtSystemDesc As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtSystemNotes As System.Web.UI.WebControls.TextBox
    Protected WithEvents TxtSystemDateRecorded As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label10 As System.Web.UI.WebControls.Label
    Protected WithEvents txtSystemDateFinished As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label11 As System.Web.UI.WebControls.Label
    Protected WithEvents Label12 As System.Web.UI.WebControls.Label
    Protected WithEvents Textbox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents ReviewButton As System.Web.UI.WebControls.Button

    Public objSess As New clsGetData_sess()   ' Create the session object for this page
    Protected WithEvents txtSystemTitle As System.Web.UI.WebControls.TextBox
    Public oApp As Object
    Public oBizApp As Object 'DPBIZ.AppData()
    Public oAppRow As DataRow
    Public dsAppName As DataSet
    Public dsAppdata As DataSet
    Public dsLeadDept As DataSet
    Public strAppId As String
    Public isNewApp As Boolean ' Set to true if its a new application
    Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents debug As System.Web.UI.WebControls.Button
    Protected WithEvents savebutton As System.Web.UI.WebControls.Button
    Public strAppName As String

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()

    End Sub

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        isNewApp = True  ' Its first time in so lets set this to true as if its a new appl

        objSess = Session("oGetDataForm1") ' this will either load the dummy session or a real session

        'code thats executed once when the page is loaded goes in here 
        If Not IsPostBack Then

            Me.PgStatus = Request.QueryString("pgStatus") ' It extracts this from the URL  Add Update View Report Maint

            PgBannerDisplay() ' run the method that displays the banner and help text at the top of the page
            PgSettings() 'Run the method that determines what objects are read only during ' Add Update View Report Maint

            DropAppName.SelectedIndex = DropAppName.Items.IndexOf(DropAppName.Items.FindByValue(objSess.strAppId))

            oBizApp = New DPBIZ.AppData() ' create the business object 
            dsAppName = oBizApp.GetAppNames()
            dsLeadDept = oBizApp.GetLeadDept()

            bindfields()   ' Ok now we need to do our data bindings. We should only have to do this once

            ' Now we have to pull back our main data record
            ' This will also have to happen when ever they choose a different aqpplication from the dropdown
            strAppId = Me.DropAppName.SelectedItem.Value()

            ' retrieve the data from the table
            dsAppdata = oBizApp.GetAppData(strAppId)

            ' Get the main data record into this row object
            oAppRow = dsAppdata.Tables("appdata").Rows(0)

            ' now that we have the main record we must refresh the page and populate any dropdowns that depend on it
            ' hmmm this function must only fire when we select a new applicatio or when we first enter
            RefreshFields() ' This function will populate the page fields from the record set
            PersistPage() ' now make sure our new page is correctly stored in our session class
        End If



        ' When a normal postback occurs we have to do the following
        '1.  Find out if they have chosens a diffrent app from the pulldown
        ' 2. Find out if they have changed any of the data in the record
        If IsPostBack Then
            '  1. Find out if they have chosen a different app from the appdropdown
            ' If a different app has been chosen then the new id will be different from the session id
            strAppId = Session("oGetDataForm1").strAppId ' get back our session info 
            Dim lAppChanged As Boolean = False
            If strAppId <> DropAppName.SelectedItem.Value Then
                ' ok we have a new value so we have to do a refresh
                strAppId = DropAppName.SelectedItem.Value
                lAppChanged = True
            End If

            System.Diagnostics.Debug.WriteLine("Post Back In Effect")
            oBizApp = New DPBIZ.AppData() ' re-create the business object because this vanishes on post back
            ' retrieve the data from the table
            dsAppdata = oBizApp.GetAppData(strAppId)
            ' Get the main data record into this row object
            oAppRow = dsAppdata.Tables("appdata").Rows(0)
            If lAppChanged Then
                RefreshFields() ' This function will populate the page fields from the record set
                PersistPage() ' now make sure our session class is saved
            End If
        End If
    End Sub


    Sub bindfields()
        With DropAppName
            .DataSource = dsAppName
            .DataTextField = "systemtitle"
            .DataValueField = "Systemid"
            .DataBind()
        End With

        With DropLeadDept
            .DataSource = dsLeadDept
            .DataBind()
        End With

        With DropSystemFormat
            .DataTextField = "systemformat"
            .DataValueField = "SystemFormat"
            .DataBind()
            .Items.Add("Computer")
            .Items.Add("Manual")
            .Items.Add("Computer/Manual")
            .Items.Add("CCTV")
        End With

        '       Me.txtSystemDesc.DataBind()
        '       Me.txtSystemDataFirstStored.DataBind()

    End Sub


    Sub RefreshFields()
        Me.txtSystemTitle.Text = oAppRow.Item("systemtitle")
        Me.txtSystemOwner.Text = oAppRow.Item("SystemOwner")
        Me.txtSystemDesc.Text = oAppRow.Item("SystemDesc")

        ' We also have to ensure the dropdowns are pointing at the right items
        Me.DropLeadDept.SelectedIndex = DropLeadDept.Items.IndexOf(DropLeadDept.Items.FindByValue(oAppRow.Item("LeadDeptId")))
        Me.DropSystemFormat.SelectedIndex = DropSystemFormat.Items.IndexOf(DropSystemFormat.Items.FindByValue(oAppRow.Item("SystemFormat")))
    End Sub


    Private Sub PgBannerDisplay()
        If Me.PgStatus = "Add" Then
            Me.PgBanner1.Text() = "Add a New system as Lead department"
            Me.HelpNote1.Text = "Use this page if your dept has Acquired a new system which needs registering under the terms of the Act"
        ElseIf Me.PgStatus = "Update" Then
            Me.PgBanner1.Text() = "Update a New system as Lead department"
            Me.HelpNote1.Text = "This page will allow you to change the details of a system that has already been entered. Select the system you wish to update from the dropdown list below"
        End If

    End Sub

    Private Sub PgSettings()
        If Me.PgStatus = "Add" Then
            'Me.AppLabel.Text = "Name of System"
            'Me.AppSelectLabel.Visible = False
            Me.DropAppName.Visible = False
            'Me.TextBoxApplication.Visible = True
        ElseIf Me.PgStatus = "Update" Then
            'Me.TextBoxApplication.Visible = False
            ''Me.AppSelectLabel.Visible = True
            Me.DropAppName.Visible = True
        End If

    End Sub



    Private Sub damnbutt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Dim mtest As DataSet
        'Dim mybiz As New DPBIZ.AppData()
        'mtest = mybiz.GetAppData("Business Travel & Expenses Re-imbursement")
        DPBIZ.AppData.SendMail("mark.young@eastsussexcc.gov.uk", "myself", "hello", "the quick brown Fox", False)
        System.Diagnostics.Debug.WriteLine("mail is sent")

    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Session("oGetDataForm1") = objSess
        PersistPage()
        Response.Redirect(objSess.page_from)
    End Sub

    Private Sub DropAppName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'With Me.DropAppName
        ' objSess.strAppId = .Items(.SelectedIndex).Value
        ' objSess.add2 = .Items(.SelectedIndex).Text
        'Session("oGetDataForm1") = objSess
        'End With
        PersistPage()
    End Sub


    ' This function will save changes to the page between postbacks
    ' not that this is not saving the actual data to the record
    ' the function below this one will do that

    ' ************ MARK YOU ARE HERE **************
    Private Sub PersistPage()
        With Me.DropAppName
            objSess.strAppId = .Items(.SelectedIndex).Value
        End With
        Session("oGetDataForm1") = objSess
    End Sub

    Private Sub saverecord()
        PersistPage() ' ensure the page session is kept updated (may be superfluous here)
        ' Ok now lets save these babies off to the data table
        oAppRow.Item("SystemDesc") = Me.txtSystemDesc.Text
        oAppRow.Item("LeadDeptid") = DropLeadDept.Items(DropLeadDept.SelectedIndex).Value







        oBizApp.UpdateAppRecord(dsAppdata, "appdata")
    End Sub


    Private Sub ReviewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReviewButton.Click

    End Sub

    Private Sub debug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles debug.Click

    End Sub


    Private Sub savebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles savebutton.Click
        saverecord()
    End Sub

End Class
Next
Reply
Map
View

Click here to load this message in the networking platform