Solved

Why is IsPostBack always = False

Posted on 2004-09-07
10
1,706 Views
Last Modified: 2008-02-01
I have a problem with the following code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then

            Try

                Dim strUserInfo() As String

                strUserInfo = Split(Me.Request.ServerVariables("LOGON_USER").ToString(), "\")

                strCurrentUser = strUserInfo(1)
                strCurrentDomain = strUserInfo(0)
             
            Catch ex As Exception
                strCurrentUser = ""
            End Try
        End if
End sub

The code is always executed because IsPostback = False. The page also includes a Asp:Button. According to al awailable documentation ispostback is false only the first time the page is loaded.
0
Comment
Question by:anpr
  • 6
  • 3
10 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 11996039
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsPostBack Then

            Try

                Dim strUserInfo() As String

                strUserInfo = Split(Me.Request.ServerVariables("LOGON_USER").ToString(), "\")

                strCurrentUser = strUserInfo(1)
                strCurrentDomain = strUserInfo(0)
             
            Catch ex As Exception
                strCurrentUser = ""
            End Try
        End if
End sub
0
 
LVL 35

Expert Comment

by:YZlat
ID: 11996046
post the code for your Button onClick event
0
 

Author Comment

by:anpr
ID: 11996143
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        UpdateSchedule()
   
End Sub

The btnUpdate_Click simply cals a function called UpdateSchedule. This part works fine.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 11996261
let me see the code for update schedule. The reason why the page is not posting back might be there.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 11996283
also what exactly do you want to do? meaning when do you want your code to execute, under what circumstances
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:anpr
ID: 11996421
   Private Function UpdateSchedule()

        Dim intCount As Integer
        Dim NewRow As New TableRow
        Dim strLabel As String
        Dim strsql As String
        Dim rs As New ADODB.Recordset
        Dim Monday As Date
        Dim strMonday As String
        Dim Friday As Date
        Dim strFriday As String

        For intCount = 0 To 5
            Dim NewCell As New TableCell
            NewCell.BorderStyle = BorderStyle.Solid

            If intCount = 0 Then
                strLabel = "Time"
            ElseIf intCount = 1 Then
                strLabel = "Monday   "
            ElseIf intCount = 2 Then
                strLabel = "Tuesday  "
            ElseIf intCount = 3 Then
                strLabel = "Wednesday"
            ElseIf intCount = 4 Then
                strLabel = "Thursday "
            ElseIf intCount = 5 Then
                strLabel = "Friday   "
            End If

            NewCell.Controls.Add(New LiteralControl(strLabel & ":"))
            NewRow.Cells.Add(NewCell)
        Next
        NewRow.Font.Bold = True
        NewRow.ForeColor = Color.Blue

        Me.TblSchedule.Rows.Add(NewRow)


        Monday = Now.Date.AddDays(-intCurrentDayOfWeek + 1)
        Friday = Monday.AddDays(4)
        strMonday = Monday.Month & "/" & Monday.Day & "/" & Monday.Year
        strFriday = Friday.Month & "/" & Friday.Day & "/" & Friday.Year

        For intCount = intCurrentWeekNo To intSelectedWeekNo

        Next

        strsql = "SELECT StartTime, EndTime, Subject, Location" _
            & " FROM Appointments WHERE mailbox = '" & CStr(ddlInitials.SelectedValue) & "@mail'" _
            & " AND StartTime >='" & strMonday & "'" _
            & " AND StartTime <= '" & strFriday & "' ORDER BY StartTime asc"

        rs = myDB.SQLAceessToDB(strsql)

        Dim intDayCount As Integer
        Dim dateWork As Date
        Dim ts As TimeSpan
        dateWork = Monday

        For intCount = 0 To 8 Step 2
            Dim NewScheduleRow As New TableRow
            dateWork.AddHours(intCount)
            Dim newTimeCell As New TableCell
            Dim intTime As Integer
            intTime = 8 + intCount
            newTimeCell.Controls.Add(New LiteralControl(CStr(intTime) & " -"))
            newTimeCell.BorderStyle = BorderStyle.Solid
            NewScheduleRow.Cells.Add(newTimeCell)

            For intDayCount = 0 To 4
                Dim StartTime As Date
                Dim EndTime As Date
                Dim newScheduleCell As New TableCell
                Dim dateDB As Date
                Dim strSchedule As String

                newScheduleCell.BorderStyle = BorderStyle.Solid

                Try
                    While Not rs.EOF
                        dateDB = rs.Fields("StartTime").Value
                        If dateWork.Date = dateDB.Date Then
                            strSchedule = rs.Fields("Subject").Value
                        End If
                        rs.MoveNext()
                    End While

                Catch
                End Try
                dateWork = dateWork.AddDays(1)

                If Len(strSchedule) = 0 Then strSchedule = "-"
                newScheduleCell.Controls.Add(New LiteralControl(strSchedule))
                NewScheduleRow.Cells.Add(newScheduleCell)
                strSchedule = ""
                Try
                    rs.MoveFirst()
                Catch
                End Try

            Next

            Me.TblSchedule.Rows.Add(NewScheduleRow)

        Next
        Try
            rs.Close()
        Catch
        End Try

    End Function

The application contains two dropdownlist. One with weeknumber and another one with initialts. The users can use this application to se their schedule in the different weeknumbers.
The application will by default show the schedule for the current week and for the current ADS user. The Initials and the schedule tasks are picked up in a SQL database.
0
 
LVL 35

Expert Comment

by:YZlat
ID: 11996692
I have a suggestion to make:

1)Delete the code for button click

2)Replace your Page_Load with the following code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If IsPostBack Then

                  Try

                      Dim strUserInfo() As String

                      strUserInfo = Split(Me.Request.ServerVariables("LOGON_USER").ToString(), "\")

                      strCurrentUser = strUserInfo(1)
                      strCurrentDomain = strUserInfo(0)
             
                  Catch ex As Exception
                      strCurrentUser = ""
                  End Try
      Else
            UpdateSchedule()

      End if
End Sub
0
 

Author Comment

by:anpr
ID: 11996819
For some reason there is an effect. IsPostBack is now true at the second load. Can you explain me the differens in the two samples?
0
 
LVL 35

Accepted Solution

by:
YZlat earned 50 total points
ID: 11996987
because postback event occurs before button click event.

Read this article, it explains a lot about event .NET programming
http://64.233.167.104/search?q=cache:P9GuRRzxNrwJ:www.personal.psu.edu/faculty/j/a/jat14/misbd470/Event%2520driven%2520Programming%2520and%2520Postback.doc
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11999536
PostBack occurs when an ASP.NET form in a page has been posted back to the server, such as via a click on a submit button, or an AutoPostBack trigger.

PostBack does not occur if the page simply redirects onto itself (forming a new "first" request"), or is re-achieved via any method other than the submission of its ASP.NET form.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now