Solved

Why is IsPostBack always = False

Posted on 2004-09-07
10
1,716 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 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

10 Experts available now in Live!

Get 1:1 Help Now