PlaceHolder Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,
How can I retrieve the Control (wucState.ascx) from the Placeholder at the
even: btn_Click:
//declaration at page
protected wucState WucState1; -- my control
....
private void Page_Load(object sender, System.EventArgs e)
{
....
WucState1 = Page.LoadControl("wucState.ascx") as wucState;
plhState.Controls.Add(WucState1);
....
}
....
private void btn_Click(object sender, System.EventArgs e)
{
WucState1 = (wucState)plhState.Controls[0]; // ERROR !!!!!! CANNOT FIND
THE CONTROL
....
}

any idea ? please with code sample.

thanx,
Oren
 
It isn't clear from your code whether or not you are adding the control to
your placeholder during postback. Ie, if you have the Page.LoadControl and
plhState.Controls.Add lines below in a if (!Page.IsPostBack) then things
won't work.

Also, since you've scoped WucState1 as a field (seems like it should be
private, not public, but that doesn't mater), why not just use WucState1 in
yoru click?

public class Index: Page
{
private wucState _wucState1;
private void Page_Load(object sender, System.EventArgs e)
{
_wucState1 = (wucState)Page.LoadControl("wucState.ascx");
plhState.Controls.Add(_wucState1);
if (!Page.IsPostBack)
{
//do stuff
}
}
private void btn_Click(object sender, System.EventArgs e)
{
_wucState1.Whatever //shouldn't be null, because it was recreated
in page_load on postback
}
}

Karl
 
Karl,
the "Page.Controls.Add" is inside "if (!Page.IsPostBack)". On the Click
event I need to retreive some data that was changed in the control.

public class Index: Page
{
private wucState _wucState1;
private System.Web.UI.WebControls.PlaceHolder plhOrder;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
_wucState1 = (wucState)Page.LoadControl("wucState.ascx");
plhState.Controls.Add(_wucState1);
}
}
private void btn_Click(object sender, System.EventArgs e)
{
_wucState1 = (wucState)plhState.Controls[0]; // ERROR
}
}


Karl Seguin said:
It isn't clear from your code whether or not you are adding the control to
your placeholder during postback. Ie, if you have the Page.LoadControl and
plhState.Controls.Add lines below in a if (!Page.IsPostBack) then things
won't work.

Also, since you've scoped WucState1 as a field (seems like it should be
private, not public, but that doesn't mater), why not just use WucState1 in
yoru click?

public class Index: Page
{
private wucState _wucState1;
private void Page_Load(object sender, System.EventArgs e)
{
_wucState1 = (wucState)Page.LoadControl("wucState.ascx");
plhState.Controls.Add(_wucState1);
if (!Page.IsPostBack)
{
//do stuff
}
}
private void btn_Click(object sender, System.EventArgs e)
{
_wucState1.Whatever //shouldn't be null, because it was recreated
in page_load on postback
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



Oren said:
hi,
How can I retrieve the Control (wucState.ascx) from the Placeholder at the
even: btn_Click:
//declaration at page
protected wucState WucState1; -- my control
...
private void Page_Load(object sender, System.EventArgs e)
{
...
WucState1 = Page.LoadControl("wucState.ascx") as wucState;
plhState.Controls.Add(WucState1);
...
}
...
private void btn_Click(object sender, System.EventArgs e)
{
WucState1 = (wucState)plhState.Controls[0]; // ERROR !!!!!! CANNOT FIND
THE CONTROL
...
}

any idea ? please with code sample.

thanx,
Oren
 
As i said (and my example showed), it must be OUTSIDE the
!Page.IsPostback....you can't find the control because on postback, it was
never recreated and added to the placeholder..

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



Oren said:
Karl,
the "Page.Controls.Add" is inside "if (!Page.IsPostBack)". On the Click
event I need to retreive some data that was changed in the control.

public class Index: Page
{
private wucState _wucState1;
private System.Web.UI.WebControls.PlaceHolder plhOrder;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
_wucState1 = (wucState)Page.LoadControl("wucState.ascx");
plhState.Controls.Add(_wucState1);
}
}
private void btn_Click(object sender, System.EventArgs e)
{
_wucState1 = (wucState)plhState.Controls[0]; // ERROR
}
}


Karl Seguin said:
It isn't clear from your code whether or not you are adding the control
to
your placeholder during postback. Ie, if you have the Page.LoadControl
and
plhState.Controls.Add lines below in a if (!Page.IsPostBack) then
things
won't work.

Also, since you've scoped WucState1 as a field (seems like it should be
private, not public, but that doesn't mater), why not just use WucState1
in
yoru click?

public class Index: Page
{
private wucState _wucState1;
private void Page_Load(object sender, System.EventArgs e)
{
_wucState1 = (wucState)Page.LoadControl("wucState.ascx");
plhState.Controls.Add(_wucState1);
if (!Page.IsPostBack)
{
//do stuff
}
}
private void btn_Click(object sender, System.EventArgs e)
{
_wucState1.Whatever //shouldn't be null, because it was
recreated
in page_load on postback
}
}

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



Oren said:
hi,
How can I retrieve the Control (wucState.ascx) from the Placeholder at
the
even: btn_Click:
//declaration at page
protected wucState WucState1; -- my control
...
private void Page_Load(object sender, System.EventArgs e)
{
...
WucState1 = Page.LoadControl("wucState.ascx") as wucState;
plhState.Controls.Add(WucState1);
...
}
...
private void btn_Click(object sender, System.EventArgs e)
{
WucState1 = (wucState)plhState.Controls[0]; // ERROR !!!!!! CANNOT
FIND
THE CONTROL
...
}

any idea ? please with code sample.

thanx,
Oren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top