Click here to Skip to main content
13,400,969 members (41,150 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 3 May 2008

Cross Page Postback Without Circular Reference Errors

Rate this:
Please Sign up or sign in to vote.
Cross posting with strong typing.


This whole thing started out simple. All I wanted to do was pass a single variable back and forth between two pages in an admin section of a website using cross page postback. Were it not for my use of a master page, I would not be writing about this. Without a master, all you need to use is find the control. However, with a master page, things get more complicated. I’ll not go into it more than to say you need to find the control via the clientID, and you need to somehow get that ID in ways that I consider prone to error. Therefore, I opted for strong typing of the previous page. This nailed me with another issue. It works great one way, but not two ways. From x.aspx to y.aspx - no problem, but from y.aspx back to x.aspx - error! Well, it took me a while, but I figured out how to do it cleanly; in one word: Interface.

Let’s start by reviewing the three methods for cross page postback. I have two variables I need to pass, iItemID and iClientID, between two pages, x.aspx and y.aspx.

  1. PreviousPage.FindControl("lbl_iItemID") where lbl_iItemID is a Label on the previous page with the value you want to get. This gave me problems with a master page, necessitating some wordy and sloppy code. Sloppy in at least the way I was doing it!
  2. Strong typing of the previous page. Here you add a <%@ PreviousPageType VirtualPath="sourcepage.aspx" %> in the receiving page. This is a reference to the previous page, so you can just use a PreviousPage.lbl_iItemID.Text to get the value. The problem here is when you try to go back to the first page, you get an error noting a circular reference. In other words, x to y is on, but y to x is error.
  3. This one is not common, but it should be! Use an interface. From here forward, this is what I will be using for all cross page post backs. It allows you to create a discrete number of strongly typed arguments that can be referenced by any page you decide that needs to receive or pass variables.

The interface method

I will outline how I consider it should be done:

  1. Create the interface. This one is simple, with only two items.
  2. using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.ComponentModel; 
    public interface ICommonPostback 
      int IItemID 
      { get; set; } 
      int IClientUploadID 
      { get; set; } 

    Save as ICommonPostback.cs.

  3. Reference the interface in your two pages like this, by adding ICommonPostBack:
  4. public partial class Admin_y : System.Web.UI.Page, ICommonPostback //(for y)
    public partial class Admin_x : System.Web.UI.Page, ICommonPostback //(for x)
  5. Create properties.
  6. #region Cross page postback var
    private int iItemID = -1;
    private int iClientUploadID = -1;
    public int IItemID
      get { return iItemID; }
      set { iItemID = value; }
    public int IClientUploadID
      get { return iClientUploadID; }
      set { iClientUploadID = value; }

    This may not be necessary, but I consider it worth the effort to get cleaner code for all pages involved in the posting cycle.

  7. Create a button on each page with the post back URL set to the other page.
  8. <asp:Button ID="Button1" runat="server" Text="Button" 
        onclick="Button1_Click" PostBackUrl="~/Admin/y.aspx" />
  9. Create a Button_Click event with the following (given that this is x.aspx). You set the values here to be referenced in the receiving page.
  10. protected void Button1_Click(object sender, EventArgs e)
      iItemID = 22;
      iClientUploadID = 23;

    On the receiving page (given that this is y.aspx).

    protected void Page_Load(object sender, EventArgs e)
      if (PreviousPage != null)
        ICommonPostback frm = PreviousPage as ICommonPostback;
        Label1.Text = frm.IItemID.ToString();
  11. Do the same on the other page, but change the variables to show that posting has occurred. See the attached code (uses ASP.NET and C# 3.5).
  12. Note, for master pages, it will work the same, except you will need to reference the PreviousPage.Master, or if nested, the master page's PreviousPage.Master.Master.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yankee Imperialist Dog!
Web Developer
United States United States
I specialize in stock charting systems built upon MS Sqlserver and Cold Fusion (DotNet 2.0 Versions next: to use SQL2005 and the CLR for data integration/de-normalization). My current project is an entire smart system based upon Point & Figure charting. This system includes Point & Figure Charts, Relative Strength Charts Ten-Week Charts, Bullish Percent Charts, Hi-Lo Charts. Reports include all standard summary information from all charts and additional “AI” reports revealing market opportunities. You want to see it? Email me.

I have a BS in Computer Science from SCSU
I’m a 10 year Cold Fusion Programmer, 10 year SQL Programmer, and a 1+ year DotNet Programmer. I have also written using C++, VB, Access, RPGII/III/400 (native code). I’m planning to recertify on sql2005, and get a 2.0 cert in

And yes I do love dotnet….

You may also be interested in...


Comments and Discussions

GeneralCross page Postback in Masterpage Pin
Venu200910-May-10 2:00
memberVenu200910-May-10 2:00 
GeneralRe: Cross page Postback in Masterpage Pin
Yankee Imperialist Dog!10-May-10 6:06
memberYankee Imperialist Dog!10-May-10 6:06 
GeneralYour Followup Article on Remote IIS Debugging Pin
Abhijit Jana14-Jul-09 12:33
mvpAbhijit Jana14-Jul-09 12:33 
GeneralInterface issues in Pin
Member 386979218-Jun-09 22:24
memberMember 386979218-Jun-09 22:24 
Questioncan someone help me to conver to Pin
kss11-Dec-08 11:13
memberkss11-Dec-08 11:13 
AnswerRe: can someone help me to conver to Pin
webbuilder17-Dec-08 5:34
memberwebbuilder17-Dec-08 5:34 
GeneralRe: can someone help me to conver to Pin
kss17-Dec-08 5:38
memberkss17-Dec-08 5:38 
GeneralThanks!! Pin
peaceoutside.org28-Oct-08 7:32
memberpeaceoutside.org28-Oct-08 7:32 
Generaladditional option that deserves note Pin
webbuilder17-Aug-08 12:02
memberwebbuilder17-Aug-08 12:02 
GeneralThanks! Pin
Marc Leger10-Jun-08 6:56
memberMarc Leger10-Jun-08 6:56 
Simple and elegant solution! Was beating my head against the wall.

- M

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180218.2 | Last Updated 5 Jun 2008
Article Copyright 2008 by Yankee Imperialist Dog!
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid