View Full Version : Expose a placeholder control in usercontrol
nature
01-11-2004, 06:35 AM
I put a placeholder in an ascx file. I want to expose that placeholder and put a control on it.
Ultimately, what I'm attempting to do is create a generic page layout in an ascx file and then plug in the controls as needed. E.g. I have an ascx file (call it template.ascx) containing a table where the first row is a header, the 2nd row is the body, the third row is a footer. I want to have a place holder control in the body and load in another ascx there.
Perhaps there's a better way to do this. I'd appreciate any advice you can give. Thanks, Carl
steurm
01-11-2004, 11:52 AM
There probably will be a better way to do this, but I use this system all the time ...
To add controls (e.g. "MyControl.ascx") to your placeholder (e.g. "phBody"), you can do this:
[quote]
//puttingthecontrolinavariablebyloadingit.Theloadcon trolmethodneedstherelativepathtothecontrol
MyControlctrl=(MyControl)LoadControl("MyControl.ascx");
//hereyoucansetanypropertiesorsoforthiscontrol
ctrl.MyProperty=true;
//finallyweaddthecontroltotheplaceholder
phBody.Controls.Add(ctrl);
</CODE>
Success !
--
Steurm
www.steurm.net/steurm
nature
01-12-2004, 03:24 AM
thanks for the help but it doesn't quite work. I've tinkered with it to see if I can get it to work but I'm stuck. Any other suggestions?
template.ascx contains:
public sub LoadMyControl( c as System.Web.UI.Control) as System.Web.UI.Control
plhColOne.Controls.Add(c)
end sub
Here's what happens:
Compiler Error Message: BC30456: 'LoadMyControl' is not a member of 'System.Web.UI.Control'.</font id="red">
Source Error:
Line 18:
Line 19: 'use the new method of mytemplate.ascx to load the second control
Line 20: ctlTemplate.LoadMyControl(ctlOther)
Line 21:
Line 22: 'finally, load the mytemplate control on your starting page
Source File: c:\inetpub\wwwroot\newfile.aspx Line: 20
[b]quote:Originally posted by steurm
[b]quote:Actually, if I'm adding a control to a placeholder on my page, this works fine. However, the placeholder is on another control.
E.g. I'm in mypage.aspx that has mytemplate.ascx on it.
I want to add a control to a placeholder on mytemplate.ascx from mypage.aspx.
</blockquote id="quote"></font id="quote">
You can make this work, through codebehind:
First define a method on the mytemplate.ascx, which does the loading of other controls: (MyPH is the name of a PlaceHolder on mytemplate.ascx)
[quote]
publicvoidLoadMyControl(System.Web.UI.Controlc)
{
MyPH.Controls.Add(c);
}
</CODE>
Secondly, you add the mytemplate.ascx to your first page by using codebehind. You load the control, use the method of that control to add a control there, and then add the control:
[quote]
//loadthemytemplate.ascx
mytemplatemt=(mytemplate)LoadControl("mytemplate.ascx");
//loadthesecondcontrol
OtherControloc=(OtherControl)LoadControl("OtherControl.ascx");
//usethenewmethodofmytemplate.ascxtoloadthesecondcon trol
mt.LoadMyControl(oc);
//finally,loadthemytemplatecontrolonyourstartingpage
phStart.Controls.Add(mt);
</CODE>
This should to the job if I'm correct ...
success
--
Steurm
www.steurm.net/steurm
</blockquote id="quote"></font id="quote">
nature
01-12-2004, 07:13 AM
Actually, if I'm adding a control to a placeholder on my page, this works fine. However, the placeholder is on another control.
E.g. I'm in mypage.aspx that has mytemplate.ascx on it.
I want to add a control to a placeholder on mytemplate.ascx from mypage.aspx.
[b]quote:Originally posted by steurm
There probably will be a better way to do this, but I use this system all the time ...
To add controls (e.g. "MyControl.ascx") to your placeholder (e.g. "phBody"), you can do this:
[quote]
//puttingthecontrolinavariablebyloadingit.Theloadcon trolmethodneedstherelativepathtothecontrol
MyControlctrl=(MyControl)LoadControl("MyControl.ascx");
//hereyoucansetanypropertiesorsoforthiscontrol
ctrl.MyProperty=true;
//finallyweaddthecontroltotheplaceholder
phBody.Controls.Add(ctrl);
</CODE>
Success !
--
Steurm
www.steurm.net/steurm
</blockquote id="quote"></font id="quote">
steurm
01-12-2004, 09:24 AM
[b]quote:Actually, if I'm adding a control to a placeholder on my page, this works fine. However, the placeholder is on another control.
E.g. I'm in mypage.aspx that has mytemplate.ascx on it.
I want to add a control to a placeholder on mytemplate.ascx from mypage.aspx.
</blockquote id="quote"></font id="quote">
You can make this work, through codebehind:
First define a method on the mytemplate.ascx, which does the loading of other controls: (MyPH is the name of a PlaceHolder on mytemplate.ascx)
[quote]
publicvoidLoadMyControl(System.Web.UI.Controlc)
{
MyPH.Controls.Add(c);
}
</CODE>
Secondly, you add the mytemplate.ascx to your first page by using codebehind. You load the control, use the method of that control to add a control there, and then add the control:
[quote]
//loadthemytemplate.ascx
mytemplatemt=(mytemplate)LoadControl("mytemplate.ascx");
//loadthesecondcontrol
OtherControloc=(OtherControl)LoadControl("OtherControl.ascx");
//usethenewmethodofmytemplate.ascxtoloadthesecondcon trol
mt.LoadMyControl(oc);
//finally,loadthemytemplatecontrolonyourstartingpage
phStart.Controls.Add(mt);
</CODE>
This should to the job if I'm correct ...
success
--
Steurm
www.steurm.net/steurm
nature
01-13-2004, 01:15 AM
You gave a really good explanation that makes sense to me but it still gives the same error. I really, really appreciate the help though. Unless someone knows what this error is, I'm going to let it rest for now.
Compiler Error Message: BC30456: 'LoadMyControl' is not a member of 'System.Web.UI.Control'.
Source Error:
[b]quote:Originally posted by steurm
[b]quote:thanks for the help but it doesn't quite work. I've tinkered with it to see if I can get it to work but I'm stuck. Any other suggestions?
</blockquote id="quote"></font id="quote">
I'm not familiar with VB.NET, but I tried it out and here is the code that works for me:
<ul> I have 1 webform (StartPage.aspx) and 2 usercontrols (mytemplate.ascx and OtherControl.ascx)
Othercontrol only has some html-content to show mytemplate.ascx has the following method (MyPH is a placeholder control on mytemplate.ascx:
[quote]
PublicSubLoadMyControl(ByValcAsSystem.Web.UI.Contr ol)
MyPH.Controls.Add(c)
EndSub
</CODE> In the Page_Load of StartPage.aspx, I have following code (just like described with C#
[quote]
DimmtAsmytemplate=LoadControl("mytemplate.ascx")
DimotAsOtherControl=LoadControl("OtherControl.ascx")
mt.LoadMyControl(ot)
phStart.Controls.Add(mt)</CODE>
[/list]
And that's it ...
Hopefully you get it to work. Success !
--
Steurm
www.steurm.net/steurm
</blockquote id="quote"></font id="quote">
steurm
01-13-2004, 01:59 AM
[b]quote:thanks for the help but it doesn't quite work. I've tinkered with it to see if I can get it to work but I'm stuck. Any other suggestions?
</blockquote id="quote"></font id="quote">
I'm not familiar with VB.NET, but I tried it out and here is the code that works for me:
<ul> I have 1 webform (StartPage.aspx) and 2 usercontrols (mytemplate.ascx and OtherControl.ascx)
Othercontrol only has some html-content to show mytemplate.ascx has the following method (MyPH is a placeholder control on mytemplate.ascx:
[quote]
PublicSubLoadMyControl(ByValcAsSystem.Web.UI.Contr ol)
MyPH.Controls.Add(c)
EndSub
</CODE> In the Page_Load of StartPage.aspx, I have following code (just like described with C#
[quote]
DimmtAsmytemplate=LoadControl("mytemplate.ascx")
DimotAsOtherControl=LoadControl("OtherControl.ascx")
mt.LoadMyControl(ot)
phStart.Controls.Add(mt)</CODE>
[/list]
And that's it ...
Hopefully you get it to work. Success !
--
Steurm
www.steurm.net/steurm
bruce
01-14-2004, 11:54 AM
LoadMyControl is a subroutine. You have to include that in your code.
[b]quote:Originally posted by nature
You gave a really good explanation that makes sense to me but it still gives the same error. I really, really appreciate the help though. Unless someone knows what this error is, I'm going to let it rest for now.
Compiler Error Message: BC30456: 'LoadMyControl' is not a member of 'System.Web.UI.Control'.
Source Error:
[b]quote:Originally posted by steurm
[b]quote:thanks for the help but it doesn't quite work. I've tinkered with it to see if I can get it to work but I'm stuck. Any other suggestions?
</blockquote id="quote"></font id="quote">
I'm not familiar with VB.NET, but I tried it out and here is the code that works for me:
<ul> I have 1 webform (StartPage.aspx) and 2 usercontrols (mytemplate.ascx and OtherControl.ascx)
Othercontrol only has some html-content to show mytemplate.ascx has the following method (MyPH is a placeholder control on mytemplate.ascx:
[quote]
PublicSubLoadMyControl(ByValcAsSystem.Web.UI.Contr ol)
MyPH.Controls.Add(c)
EndSub
</CODE> In the Page_Load of StartPage.aspx, I have following code (just like described with C#
[quote]
DimmtAsmytemplate=LoadControl("mytemplate.ascx")
DimotAsOtherControl=LoadControl("OtherControl.ascx")
mt.LoadMyControl(ot)
phStart.Controls.Add(mt)</CODE>
[/list]
And that's it ...
Hopefully you get it to work. Success !
--
Steurm
www.steurm.net/steurm
</blockquote id="quote"></font id="quote">
</blockquote id="quote"></font id="quote">
vBulletin® ©Jelsoft Enterprises Ltd.