Click here to Skip to main content
Click here to Skip to main content

Cross Page Postback Without Circular Reference Errors

, 5 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Cross posting with strong typing.

Introduction

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; }
    }
    #endregion

    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.

License

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

Share

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 asp.net.
 
And yes I do love dotnet….

Comments and Discussions

 
GeneralCross page Postback in Masterpage PinmemberVenu200910-May-10 1:00 
GeneralRe: Cross page Postback in Masterpage PinmemberYankee Imperialist Dog!10-May-10 5:06 
GeneralYour Followup Article on Remote IIS Debugging PinmvpAbhijit Jana14-Jul-09 11:33 
GeneralInterface issues in vb.net PinmemberMember 386979218-Jun-09 21:24 
Questioncan someone help me to conver to vb.net Pinmemberkss11-Dec-08 10:13 
I am using vb.net for my code behind and I am having issues with my interface. Cuz in vb iItemID and IItemID are the same.
 
Thanks,
AnswerRe: can someone help me to conver to vb.net Pinmemberwebbuilder17-Dec-08 4:34 
GeneralRe: can someone help me to conver to vb.net Pinmemberkss17-Dec-08 4:38 
GeneralThanks!! Pinmemberpeaceoutside.org28-Oct-08 6:32 
Generaladditional option that deserves note Pinmemberwebbuilder17-Aug-08 11:02 
GeneralThanks! PinmemberMarc Leger10-Jun-08 5:56 

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

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

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