Click here to Skip to main content
15,868,292 members
Articles / Programming Languages / C#
Article

Cross Page Postback Without Circular Reference Errors

Rate me:
Please Sign up or sign in to vote.
4.11/5 (4 votes)
5 Jun 2008CPOL3 min read 41.5K   158   20   10
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. C#
    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. C#
    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. C#
    #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.NET
    <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. C#
    protected void Button1_Click(object sender, EventArgs e)
    {
      iItemID = 22;
      iClientUploadID = 23;
    }

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

    C#
    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)


Written By
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 Pin
Venu200910-May-10 1:00
Venu200910-May-10 1:00 
GeneralRe: Cross page Postback in Masterpage Pin
Yankee Imperialist Dog!10-May-10 5:06
Yankee Imperialist Dog!10-May-10 5:06 
GeneralYour Followup Article on Remote IIS Debugging Pin
Abhijit Jana14-Jul-09 11:33
professionalAbhijit Jana14-Jul-09 11:33 
GeneralInterface issues in vb.net Pin
Member 386979218-Jun-09 21:24
Member 386979218-Jun-09 21:24 
Questioncan someone help me to conver to vb.net Pin
kss11-Dec-08 10:13
kss11-Dec-08 10:13 
AnswerRe: can someone help me to conver to vb.net Pin
Yankee Imperialist Dog!17-Dec-08 4:34
Yankee Imperialist Dog!17-Dec-08 4:34 
GeneralRe: can someone help me to conver to vb.net Pin
kss17-Dec-08 4:38
kss17-Dec-08 4:38 
GeneralThanks!! Pin
peaceoutside.org28-Oct-08 6:32
peaceoutside.org28-Oct-08 6:32 
This is exactly what I was looking for, thanks!
Generaladditional option that deserves note Pin
Yankee Imperialist Dog!17-Aug-08 11:02
Yankee Imperialist Dog!17-Aug-08 11:02 
GeneralThanks! Pin
Marc Leger10-Jun-08 5:56
Marc Leger10-Jun-08 5:56 

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.