Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Please help with user controls/param passing!

Am new to ASP.NET, my need is simple.

1. 1 main page with a header, footer and center area to the left. The page is divided using a simple HTML table, with a ASP.NET placeholder control each for the header, footer and center
2. I have created user controls for header, footer and specific book_details user control to show in center
3. In header, I have a textbox where someone can enter a book name
4.When book name is entered, I want the containing page (the main page holding the control) to display more details on the book - this has to be done by loading the book_details to the center placeholder.

So what are we looking at here? in the simplest level, the data from the header user control must be passed to the main page, and then the main page must set some property of the book_details control and load it. That's it.

What have I done so far? well my user controls work fine as far as loading is concerned, I do it in the main page load by doing
<code>

Control hdr = this.LoadControl("bkheader.ascx");
Control ftr = this.LoadControl("bkfooter.ascx");
                                
this.HeaderCtrl.Controls.Add(hdr);
this.FooterCtrl.Controls.Add(ftr);

</code>

I even figured out a clumsy way (may be that is the only way in ASP.NET, i'm not sure) where I do a server.transfer to the main page when someone enters text in the header text box. But once I get it in the main page, I want to set a BookName attribute of the book_details user control (like, say, bdetails.BookName="babylon".

My biggest issue is I CAN'T SEE THE DAMN parameters when I try setting it in the main page. It is  declared as public in there. I tried a (bkheader)this.LoadControl typecast too-- no luck :(
I'm out of clues and too new to figure out more. PLEASE HELP! :) (If I can get past this, I may be able to declare a delegate in the user control and then assign a method in the main page to the user control so that the header can directly call a main page method instead of doing a server.transfer.. but this is holding it all up!)

Your help is greatly appreciated.

n
Friday, March 04, 2005

I think that I need more information about how you've got it all set up, but let me take a stab at it.

You have a main page. In your main page you have a TextBox and a submit button. You've registered the Click event of the submit button to a handler, and you need that handler to pass the information the user entered to a user control that you are already using in the page. Does that sound right?

Have new properties of your user control is fairly easy - just create it in the code-behind as a public property. So if you have a user control BookInformationDisplayer, do the following

public class BookInformationDisplayer : System.Web.UI.UserControl
{
  private string bookSearchString = String.Empty;

  public string BookSearchString
  {
      get{return bookSearchString;}
      set{bookSearchString = value;}
  }
}

so from your main page you can do something like

public class MainPage : System.Web.UI.Page
{
  protected Button cmdSearch;
  protected TextBox searchString;
  protected PlaceHolder PlaceHolder1;
  private BookInformationDisplayer bookDisplayer;

  private void Page_Load(object sender, EventArgs e)
  {
      cmdSearch.Click += new EventHandler(cmdSearch_Click);

      bookDisplayer =
          Page.LoadControl("bookdisplay.aspx");

      PlaceHolder1.Controls.Add(bookDisplayer);
  }

  private void cmdSearch_Click(object sender, EventArgs e)
  {
      bookDisplayer.BookSearchString = searchString.Text;
      bookDisplayer.Search(); //or DataBind()     
  }
}

I did all that on the fly because I don't have any of the C# stuff here to test it with, so if that doesn't work, and you are still stuck, let me know and I'll take a look at it on Monday.

Cory

Cory Foy
Saturday, March 05, 2005

Also another alternative.

after you load the control then pass a value to one of it's members or to one of it's functions.

pseudo:

mycontrol = loadcontrol();
mycontrol.bookvalue = "babylon";
controls.add(mycontrol);

Steve
Monday, March 07, 2005

*  Recent Topics

*  Fog Creek Home