Solved

Why is IsPostBack always = False

Posted on 2004-09-07
10
1,720 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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
 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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