Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1749
  • Last Modified:

Why is IsPostBack always = False

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
anpr
Asked:
anpr
  • 6
  • 3
1 Solution
 
YZlatCommented:
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
 
YZlatCommented:
post the code for your Button onClick event
0
 
anprAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
YZlatCommented:
let me see the code for update schedule. The reason why the page is not posting back might be there.
0
 
YZlatCommented:
also what exactly do you want to do? meaning when do you want your code to execute, under what circumstances
0
 
anprAuthor Commented:
   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
 
YZlatCommented:
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
 
anprAuthor Commented:
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
 
YZlatCommented:
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
 
ThogekCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now