Postback Blues Again

Results 1 to 2 of 2

Thread: Postback Blues Again

  1. #1
    Join Date
    Dec 1969

    Default Postback Blues Again

    Hi,<BR><BR>I&#039;m starting to suspect that I&#039;m missing some fundamental understanding of how postbacks work, even though they appear to be the simplest thing. The reason for this is that out of my last 3 projects, all of them have stumbled on some aspect of the postback routine that I can&#039;t get my head around. So, once again, I&#039;m here asking for some help :)<BR><BR>The issue on this is fairly simple. I&#039;ve build a web form which displays different items from a database depending on options that a user can click. On each click, the items on the page should change. Simple enough, it would seem. However what I&#039;ve written only causes the items to change every second click, and it&#039;s beyond my feeble old brain to understand why. Can someone explain this?<BR><BR>Here&#039;s the VB code (with some debug code in there):<BR><BR>Imports System.Data<BR>Imports System.Data.SqlClient<BR><BR>Public Class menu_polaris<BR> Inherits System.Web.UI.Page<BR><BR> Protected WithEvents Label1 As System.Web.UI.WebControls.Label<BR> Protected WithEvents hplMonday As System.Web.UI.WebControls.LinkButton<BR> Protected WithEvents hplWednesday As System.Web.UI.WebControls.LinkButton<BR> Protected WithEvents hplFriday As System.Web.UI.WebControls.LinkButton<BR> Protected WithEvents hplTuesday As System.Web.UI.WebControls.LinkButton<BR> Protected WithEvents hplThursday As System.Web.UI.WebControls.LinkButton<BR> Protected WithEvents hplLastWeek As System.Web.UI.WebControls.LinkButton<BR> Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater<BR> Protected WithEvents Label2 As System.Web.UI.WebControls.Label<BR> Protected WithEvents hplNextWeek As System.Web.UI.WebControls.LinkButton<BR><BR>#Regio n " Web Form Designer Generated Code "<BR><BR> &#039;This call is required by the Web Form Designer.<BR> &#060;System.Diagnostics.DebuggerStepThrough()&#06 2; Private Sub InitializeComponent()<BR><BR> End Sub<BR><BR> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init<BR> &#039;CODEGEN: This method call is required by the Web Form Designer<BR> &#039;Do not modify it using the code editor.<BR> InitializeComponent()<BR> End Sub<BR><BR>#End Region<BR> Public intWeekNumber As Integer<BR> Public intDayNumber As Integer<BR> Public intDiff As Integer<BR> Public dteNewDate As Date<BR> Public dteToday As Date<BR> Public Flag As Integer<BR><BR> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR> &#039;Put user code to initialize the page here<BR><BR> If Page.IsPostBack = True Then<BR> &#039;second (or more) times round<BR> FillData()<BR> Else <BR> ViewState("Today") = Now()<BR> FillData()<BR> End If<BR><BR> End Sub<BR><BR> Sub FillData()<BR><BR> Dim dteStart As Date<BR> Dim dteWkFwd As Date<BR> Dim dteWkBack As Date<BR> dteStart = "05/05/2003"<BR><BR> dteToday = ViewState("Today")<BR><BR> dteWkFwd = DateAdd(DateInterval.WeekOfYear, 1, dteToday)<BR> dteWkBack = DateAdd(DateInterval.WeekOfYear, -1, dteToday)<BR> Label1.Text = dteToday.ToShortDateString<BR> intWeekNumber = DateDiff(DateInterval.WeekOfYear, dteStart, dteToday)<BR> intWeekNumber = (intWeekNumber Mod 4) + 1<BR> intDayNumber = DatePart(DateInterval.Weekday, dteToday, Microsoft.VisualBasic.FirstDayOfWeek.Monday)<BR> ViewState("dayNo") = intDayNumber<BR><BR> Select Case intDayNumber<BR> Case 1<BR> hplMonday.ForeColor = Color.Black<BR> Case 2<BR> hplTuesday.ForeColor = Color.Black<BR> Case 3<BR> hplWednesday.ForeColor = Color.Black<BR> Case 4<BR> hplThursday.ForeColor = Color.Black<BR> Case 5<BR> hplFriday.ForeColor = Color.Black<BR> End Select<BR><BR> &#039;create connection string<BR> Dim strConnString As String<BR> strConnString = "server=olthwaite;uid=sa;pwd=;database=socrates"<B R> Dim cn As New SqlConnection(strConnString)<BR><BR> &#039;create adapters and fill dataset<BR> Dim strSQL = "SELECT * FROM yehudi WHERE Day = " & intDayNumber & " AND Week = " & intWeekNumber<BR> Dim adp As New SqlDataAdapter(strSQL, cn)<BR> Dim ds As New DataSet()<BR> adp.Fill(ds, "main")<BR> adp.SelectCommand.CommandText = "SELECT * FROM yehudi_specials"<BR> adp.Fill(ds, "specials")<BR><BR> Repeater1.DataSource = ds.Tables("main")<BR> Repeater1.DataBind()<BR><BR> End Sub<BR><BR> Sub DiffSub(ByVal sender As Object, ByVal e As CommandEventArgs)<BR> intDiff = e.CommandName<BR> intDiff = intDiff - ViewState("dayNo")<BR> Dim dteToday<BR> dteToday = DateAdd(DateInterval.Weekday, intDiff, ViewState("Today"))<BR> Label2.Text = "HELLO! " & ViewState("dayNo") & " " & intDiff & " " & ViewState("Today")<BR> ViewState("Today") = dteToday<BR> End Sub

  2. #2
    Join Date
    Dec 1969

    Default RE: Postback Blues Again

    I am betting it has to do with the way you are storing and using the "Today" in ViewState. Read up the page life cycle, focusing on when the ViewState is loaded and saved. It is not saved immediately, so you setting of dteToday in FillData() is not going to do what you think it is going to do.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts