Solved

Postback error on ASP page

Posted on 2009-04-02
6
644 Views
Last Modified: 2012-05-06
Greetings all

I am experiencing a Invalid Postback error on my page (code included under snippets).

The steps are:

1.  Browse the default page

2.  Tab to a selected month and select a day (note: if you select a day on the curretn calendar
     page, then error doesn't occur)

3.  Click the hyperlink to go to a diferent page.

4.  Click the browser's "<--" to return to the page
     At this stage, the view is that which we saw when we first browsed the page

5.  Click the "<" button on the calendar to go to the month from which I previously selected a
     date  --- POST BACK ERROR


 A few notes ...

a.  I'm using IExplorer

b.  If I press <F5> after Step2 and then proceed a normal, then it works fine

c. I am using the following code to block caching for Fox. If I apply this to IE, then it
   also works.
                           if (Request.Browser.MSDomVersion.Major == 0) // Non IE Browser?)
                              Response.Cache.SetNoStore(); // No client side cashing for non IE browsers


What am I missing? What do I need to change, to get it working without?

In advance, thanks!

    allanmark


DEFAULT PAGE SOURCE:
 

<%@ Page Language="C#" MasterPageFile="~/MimeMaster01.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
 

<asp:Content ID="Content1" ContentPlaceHolderID="leftnav" Runat="Server">
 

    <ajaxext:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">

    </ajaxext:ScriptManagerProxy>
 

    <center>

    <asp:HyperLink  ID="hypContacts" runat="server" CssClass="hypLink" NavigateUrl="~/Miscellaneous/Contacts.aspx" >Contacts</asp:HyperLink>

    </center>   

    <br />    

    <br />
 

    <ajaxext:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

         

        <center>

        <asp:Calendar ID="calEvents" runat="server" BackColor="White" BorderColor="#999999"

            CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt"

            ForeColor="Black" Height="180px" Width="200px" OnSelectionChanged="calEvents_SelectionChanged">

            <SelectedDayStyle BackColor="#ffc0cb" Font-Bold="True" ForeColor="White"  />

            <TodayDayStyle BackColor="#ffc0cb" ForeColor="Black" />

            <SelectorStyle BackColor="#CCCCCC" />

            <WeekendDayStyle BackColor="#CD8C95" />

            <OtherMonthDayStyle ForeColor="#808080" />

            <NextPrevStyle VerticalAlign="Bottom" />

            <DayHeaderStyle BackColor="#ffb6c1" Font-Bold="True" Font-Size="7pt" />

            <TitleStyle BackColor="#ffc0ca" BorderColor="Black" Font-Bold="True" />

        </asp:Calendar>

        </center>

        

        <br />

        <center>

        <h5 class="h5header" >Events for the day:</h5>

        <asp:ListBox ID="lbxEvents" runat="server" CssClass="listbox"  ></asp:ListBox>

        </center>

        

    </ContentTemplate>

    </ajaxext:UpdatePanel>

    
 

</asp:Content>
 

<asp:Content ID="Content2" ContentPlaceHolderID="righnav" Runat="Server">
 

<h3>Welcome!</h3>

<p>This is the administration site of Marriage Encounter (ME) - Western Cape. Access is restricted to ME members who are a part of the administration team. If you are a team member but do not have access, please contact the Administrator (click the "Contacts" link in the left pane to be taken to our contacts page) and request the necessary clearance.</p>

<br />

<p><i>The Admin Team!</i></p>

    

</asp:Content>
 
 
 

DEFAULT PAGE CODE BEHIND:
 

using System;

using System.Data;

using System.Text;

using System.Configuration;

using System.Collections;

using System.Collections.Generic;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using BusinessLogic;

using DataTransferObjects;
 

public partial class _Default : System.Web.UI.Page

{

    private List<ME_Event> dailyEvents;
 

    private bool loggedIn;
 

    protected void Page_Load(object sender, EventArgs e)

    {

        bool needToCallEvents;
 
 

        needToCallEvents = false;
 

        if (Request.Browser.MSDomVersion.Major == 0) // Non IE Browser?)

          Response.Cache.SetNoStore(); // No client side cashing for non IE browsers
 

        // Load the Page-specific stylesheet.

        HtmlLink lnk = (HtmlLink)Master.FindControl("cssLink");

        if (lnk != null)

            lnk.Href = "~/CSS/Default.css";
 

        if (Page.IsPostBack)

        {

            if (Session["loggedIn_Session"] != null)

            {

                if ((bool)Session["loggedIn_Session"] != User.Identity.IsAuthenticated)

                {

                    loggedIn = User.Identity.IsAuthenticated;

                    Session["loggedIn_Session"] = loggedIn;

                    FetchDailyEvents();

                }

                else

                {

                    loggedIn = (bool)Session["loggedIn_Session"];
 

                    dailyEvents = (List<ME_Event>)Session["dailyEvents_Session"];

                    lbxEvents.DataSource = dailyEvents;

                    lbxEvents.DataBind();
 

                    calEvents.VisibleDate = calEvents.SelectedDate;

                }
 

            }

            else

            {

                loggedIn = User.Identity.IsAuthenticated;

                Session["loggedIn_Session"] = loggedIn;

                FetchDailyEvents();

            }

        }

        else

        {            

            // Not PostBack

            needToCallEvents = false;
 

            if (Session["loggedIn_Session"] != null)

            {

                if ((bool)Session["loggedIn_Session"] != User.Identity.IsAuthenticated)

                {

                    loggedIn = User.Identity.IsAuthenticated;

                    Session["loggedIn_Session"] = loggedIn;

                    needToCallEvents = true;

                }

                else

                {

                    loggedIn = (bool)Session["loggedIn_Session"];

                }
 

                if (Session["selectedDate_Session"] != null)

                {

                    calEvents.SelectedDate = (DateTime)Session["selectedDate_Session"];

                    calEvents.VisibleDate = calEvents.SelectedDate;

                }

                else

                {

                    calEvents.SelectedDate = calEvents.TodaysDate;

                    calEvents.VisibleDate = calEvents.SelectedDate;

                }

            }

            else

            {

                loggedIn = User.Identity.IsAuthenticated;

                calEvents.SelectedDate = calEvents.TodaysDate;

                needToCallEvents = true;

            }
 

                FetchDailyEvents();
 
 

        }
 

        

    }
 

    protected void calEvents_SelectionChanged(object sender, EventArgs e)

    {

        FetchDailyEvents();
 

    }
 

    private void FetchDailyEvents()

    {     

        lbxEvents.DataSource = "";

        dailyEvents = null;
 

        if (loggedIn)

        {

            dailyEvents = BusinessLogic.Business.GetEvents(calEvents.SelectedDate, "Y");

        }

        else

        {

            dailyEvents = BusinessLogic.Business.GetEvents(calEvents.SelectedDate, "N");

        }
 

        lbxEvents.DataSource = dailyEvents;

        lbxEvents.DataTextField = "Title";

        lbxEvents.DataValueField = "Id";

        lbxEvents.DataBind();
 

        Session["selectedDate_Session"] = calEvents.SelectedDate;

        calEvents.VisibleDate = calEvents.SelectedDate;
 

        Session["dailyEvents_Session"] = dailyEvents;

    }
 

}
 
 

MASTER PAGE SOURCE:
 

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MimeMaster01.master.cs" Inherits="MimeMaster01" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns="http://www.w3.org/1999/xhtml" >
 

<head id="Head1" runat="server">

    

    <title>Mime Master</title>

    

     <link id="cssLink" rel="stylesheet" type="text/css" runat="server"

    href="CSS/Mime.CSS" />  

    

</head>
 

<body id="MasterBody" runat="server" >
 

<form id="form1" runat="server" style="height: 100%;">

    

<div id="container" >
 

    <a name="theTop" ></a>  

    

	<div id="header">

	<!-- Header start -->		  

          		  

		  <ajaxext:ScriptManager ID="scriptmanager1" runat="server">

          </ajaxext:ScriptManager>          

          

          <asp:LoginName  ID="lgUser" runat="server" CssClass="loginName" FormatString="Member: {0}"/>

          

          <center>

		  <h1>Marriage Encounter - Western Cape</h1>

		  <h5>(Administration Management)</h5>

          </center>

          

          <asp:Label ID="lblFollowup" runat="server" CssClass="followup" Visible="false">** Follow-ups Outstanding **</asp:Label>

                         

          <asp:Image ID="headerPict" runat="server" CssClass="headerPic" ImageUrl="~/Graphics/ME05.Gif" />
 

          <center><ul id='menu' style="width: 67%">

                <li ><a href='<%=ResolveUrl("Default.aspx")%>'  id='menuHome' >Home</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuPeople' >People</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuCircles' >Circles</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuWeekends' >Weekends</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuEvents' >Events</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuGeneral' >General</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuPortfolios' >Portfolio</a></li>

                <li ><a href='<%=ResolveUrl("Default2.aspx")%>' id='menuUsers' >Users</a></li>

              </ul>

              

              <asp:LoginStatus ID="logOn" runat="server" CssClass="loginStat" />              

            <!-- Give a little space -->

            <div style="height:15px"></div>
 

              <hr  width="96%" class="redline"/>

          </center>
 

          

	<!-- Header end -->

	</div>

	

	<div id="body">

    <!-- Body start -->

    

	    <div id="dummy" style="position:relative; height: 40px"></div>

		

        <div id="wrapperDiv" class="wrapper"> 	

	  

	    <div id="left" class="leftnav">	

		       <asp:ContentPlaceHolder ID="leftnav" runat="server">

                </asp:ContentPlaceHolder>

        </div>

        

        <div id="right" class="rightnav">	

              <asp:ContentPlaceHolder ID="righnav" runat="server">

              </asp:ContentPlaceHolder>

        </div>

      

      <!-- <div id="wrapper"> 	 -->

      </div>

        

        <!-- Give a little space -->

        <div style="height:45px"></div>

        

    <!-- Body end -->

	</div>

	

	<div id="footer">

	<!-- Footer start -->

	

		  <center><hr width="75%" class="redline" /></center>

		  

		  <!-- Give a little space -->

          <div style="height:10px"></div>

		  

		  <center><a href="#theTop" class="anchor"  >Back to top</a></center>

		  

		  <br />

          <center>Marriage Encounter Western Cape &reg; is a registered trademark of Marriage Encounter Western Cape, Inc<br />

          &copy; 2009 Marriage Encounter Western Cape, Inc.

          </center>            

        

	<!-- Footer end -->

	</div>    
 

<!-- Div: "Container"  end -->

</div>
 

</form>
 

</body>
 

</html>
 
 

MASTER PAGECODE BEHIND:
 

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;
 

public partial class MimeMaster01 : System.Web.UI.MasterPage

{

    protected void Page_Load(object sender, EventArgs e)

    {
 

    }
 
 

}

Open in new window

0
Comment
Question by:allanmark
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:Rejojohny
ID: 24055104
what is the postback error and why are you posting back when changing month in the calendar?
0
 

Author Comment

by:allanmark
ID: 24057199
Hi!

Please attached snippet fro PostBack error.

As to why posting back on month change - it wasn't something that I planned to do - it just "happens" (admittedly, most likely because I have done something wrong).
PB-Error.jpg
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 24059560
the postback happens because you have a event attached to selectedindexchanged
OnSelectionChanged="calEvents_SelectionChanged"

if you do not want any processing to be done when the date/month/year etc is changed, remove that piece of code

As far as the error is concerned, do what it suggests. one of the option mentioned is to add enableEventvalidation attribute to the page. So in the aspx page at the top i.e "<%@page" section, add
<%@page enableEventValidation="false" .....
OR use the other option mentioned, which is  to register your event ..


Rejo
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:allanmark
ID: 24060924
I'm happy with what you're saying.

What confuses me is why when I "<--" to go back to the page, it isn't displaying the data as it was before the hyperlink was clicked. If I click teh <F5> , it  then goes back into the Page_Load, is not a PostBack and follows that code route. It then diisplays the data as it shoudl be and I can happily go to any other month.

Also, if after goin to the pevious month and selecting, I then hit <F5> and therafter do the Hyperlink thingy and "<---" thingy, then it all works fine.

Obviously, this is telling me something, but I'm not sure what?
0
 
LVL 26

Accepted Solution

by:
Rejojohny earned 375 total points
ID: 24063258
>>What confuses me is why when I "<--" to go back to the page, it isn't displaying the data as it was before the hyperlink was clicked
Because you have everytihng witihin updatepanels (AJAX), the partial postbacks of the page is not cached by the browser. So when you go back to a page which had a partial postback earlier, the changed values will not be seen because the browser cache just has the content that was stored when the page was first shown

Rejo
0
 

Author Closing Comment

by:allanmark
ID: 31565685
Now I understand!! Thanks!! NAd thanks for your patience!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

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

15 Experts available now in Live!

Get 1:1 Help Now