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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null Controls 1

Status
Not open for further replies.

JurkMonkey

Programmer
Nov 23, 2004
1,731
CA
I am adding custom controls to a placeholder at runtime. Once I create the custom control I am trying to set some values of the object inside the control but they are null.

Does anyone know why this might be?

Inside my Custom Control
Code:
protected void Page_Load(object sender, EventArgs e)
{
    //btnCustomer is null at this point
    btnCustomer.Text = customer.Name;
    btnCustomer.PostBackUrl = "[URL unfurl="true"]http://www.google.ca";[/URL]
}

 
Can you post the code that creates the button, adds it to the placeholder and anything else the created to btnCustomer reference?

You posted the code that displays the symptom, but not the code that contains the problem.

Senior Software Developer
 
Lets try that post again. LOL!

Can you post the code that creates the button, adds it to the placeholder and anything else that created the btnCustomer reference?


Senior Software Developer
 
This is the page that gets the customers and loads the placeholder

Code:
 protected void Page_Load(object sender, EventArgs e)
        {
            PopulateResults();
        }

        private void PopulateResults()
        {
            List<CustomerBean> customers = GetAllCustomers();

            foreach (CustomerBean customer in customers)
            {
                CustomerSearchResult result = new CustomerSearchResult();
                placeHolder1.Controls.Add(result);
                result.Customer = customer;
            }
        }

 
Confused....

That looks right to me. You are just looping throught a list and creating a new custom control for each one, adding them to the placeholder and setting a property... right?

... and you are saying that "result" is null? When you put a breakpoint on "result.Customer = customer;" and do a quick watch on result and customer, what happens? Obviously result.Customer would not have been set until the you move past that break.

Senior Software Developer
 
The "result" is created. I can then pass in the customer class. Once inside the usercontrol I try to set the values of a textbox and a linkbutton but visual studio is saying that my link button and textbox are null.

I thought that on the page_load of the custom control, the controls contained on the page already exist. Am I wrong?

 
dynamic controls must be instiantiated in the page.Init event. the data can be populated in either the page.init or page.load event.

you also need to use LoadControl() to initialize the controls. calling [tt]new CustomerSearchResult();[/tt] doesn't work.
Code:
private IList<CustomerSearchResult> results = new List<CustomerSearchResult>();

page_init(object sender, eventargs e)
{
   for(int i = 0; i < customers.count; i++)
   {
      CustomerSearchResult result = (CustomerSearchResult)this.LoadControl("~\CustomeSearchResult");
      results.add(result);
      placeHolder1.Controls.Add(result);
   }
}

page_load(object sender, eventargs e)
{
   for(int i = 0; i < customers.count; i++)
   {
      results[i].Customer = customers[i];
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
In addition, if you want the controls to maintain their state correctly (during LoadViewState()), then you'll want to set the control's ID property to the same value every time.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
bolderbum said:
if you want the controls to maintain their state correctly (during LoadViewState()), then you'll want to set the control's ID property
does that mean this code would persist through postbacks?
Code:
private IList<CustomerSearchResult> results = new List<CustomerSearchResult>();

page_init(object sender, eventargs e)
{
   for(int i = 0; i < customers.count; i++)
   {
      CustomerSearchResult result = (CustomerSearchResult)this.LoadControl("~\CustomeSearchResult");
      results.ID = "control" + i.tostring();
      results.add(result);
      placeHolder1.Controls.Add(result);
   }
}

page_load(object sender, eventargs e)
{
   if (!this.ispostback)
   {
      for(int i = 0; i < customers.count; i++)
      {
         results[i].Customer = customers[i];
      }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
this.LoadControl("~\CustomeSearchResult.ascx");

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
lol, web apps are a necessary evil.
i don't find it so bad, but then it's all i do, and I don't care about pretty apps:)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
does that mean this code would persist through postbacks?

No, you still have to create the controls every time, but they will be able to maintain their state predictably.

Otherwise there's a risk that the state won't match the intended control and it could potentially cause other errors (as in event raising when the control tree changes). I forget the exact mechanics, but I just remember that it's "good practice" to explicitly set the ID property of dynamically created controls.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top