Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cross Page Postback from ascx file

Status
Not open for further replies.

mechro99

Programmer
Nov 27, 2001
54
US
Hello,

I'm pretty new to ASP.NET C#, and I'm struggling with what I'd imagine is a pretty simple process.

I have a page (default.aspx) that pulls in a user control (quote_generator.ascx). The user control includes a set of text boxes and dropdownlists, and a button. The button's postbackurl is set for a third page (selectcoverage.aspx).

I've read a lot about how to use cross page postbacks, and I'd like to use the strongly typed approach, setting up public properties in the source page. I've successfully created those in the quote_generator.ascx page.

In my selectcoverage.aspx target page, I'm having trouble figuring out how to get at the values from the properties created in quote_generator.ascx. I've got no trouble doing this when the form fields are built into the source page, rather than pulled in from a user control.

Another concern, the user control is used on multiple pages in the site, so I'm not sure how to approach the PreviousPageType directive, since there could be one of several previous pages sending the data.

Any tips or tutorials you could point me to that addresses cross page postbacks involving usercontrols?
 
Thanks for the response, this looks good.

A detail that I neglected to mention is that the user control quote_generator.ascx gets pulled into a masterpage, and not directly into default.aspx.

The masterpage containing the control is then applied to default.aspx, and also to a handful of other pages.

Does this impact where I need to make references? Would I instead make them on the masterpage?

Thanks again,
Dan
 
I would research the concept of encapsulation. it will go a long way in this scenario to reduce the complexity of referencing the values.

right now your hierarchy looks something like this:
Page2 -has a reference to-> Page1 -has a reference to-> Master -has a reference to-> UserControl -has a reference to->TextBox -has a value for->Text

the code would look something like this
Code:
var page1 = GetCrossPagePostBackReference();
var master = (MyInheritedMaster)page1.Master;
var userControl = (MyUserControl)master.FindControl("...");
var input = (TextBox)userControl.FindControl("...");
var text = input.Text;
rather than dig deep into the structure of user control you can encapsulate the objects to hide the details.
Code:
public class Page1 : Page
{
   public string TheText
   {
      return (MyMaster)Master.TheText;
   }
}

public class MyMaster : Master
{
   public string TheText
   {
      return MyUCInstance.TheText;
   }
}

public class MyUserControl : UserControl
{
   public string TheText
   {
      return TheTextBox.Text;
   }
}
then all you have to do is cast the crosss reference page and get TheText
Code:
public class Page2 : Page
{
   public void Page_Load(object sender, EventArg e)
   {
       var page1 = GetCrosssReference();
       var text = page1.TheText;
   }
}
this is encapsulation in it's simplest form.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thanks Jason, I'll dive into the encapsulation concept. I'm learning .NET as a self-taught ASP Classic guy, and I think I spent more time trying to learn how to perform specific tasks than familiarizing myself with overall concepts while learning Classic.

So far .NET is totally owning me. :) Thanks for the tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top