Thanks Adam,
doing as you suggested enables a control placed in a placeholder to work
from within the page propertly as i need,
But now I have a new problem. I still can't do what I am trying to achieve
because the control I want to load into the placeholder is determined by
what a user selects from a dropdown in the [hosting] page.
I put the code loading the control into a Page_Init sub as suggested, it
works when i statically Load a control by hard coding the control name in
the LoadControl("test.ascx"), but i need it to load the control dynamically
here is the sub:
private void Page_Init(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
string controlName = DropDownList1.SelectedItem.Text; //the text for
dropdown items is the control to load
Control myControl = LoadControl(controlName+".ascx");
PlaceHolder1.Controls.Add(myControl);
}
}
the page is posted back when a user selects form the dropdown and clicks a
button, here is the sub that handles the button click:
private void btnCategory_Click(object sender, System.EventArgs e)
{
selectecdCategory.Text = DropDownList1.SelectedItem.Text.Replace("
","").Replace("/","").Replace("&","");
}
when the user selects a categroy and clicks the button, the page posts back,
I get this error
I get the following error:
"Object reference not set to an instance of an object. "
for this line:
string controlName = DropDownList1.SelectedItem.Text;
So the DropDownList1 object is not initialised before the Page_Init sub
fires.
Can you see what i am trying to achieve? any further suggestions
appreciated.
Thanks to everyone for their input so far.
Tim..
[quoted text, click to view] "Adam Carden" <adam.carden@futurejester.co.uk> wrote in message
news:e1RIRMznDHA.2432@TK2MSFTNGP10.phx.gbl...
>
>
> I had the same problem however the answer is simple, control binding /
> view state preperation starts before page_load and any event.
>
> If you place your control loading logic in page_init sub all will work
> properly.
>
> *** Sent via Developersdex
http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!