|
 |
aspx_beginners thread: include custom control dynamically
Message #1 by "Jason Pluis" <Jason.Pluis@a...> on Fri, 08 Nov 2002 12:48:28 +1100
|
|
Does anyone know how to include a custom control dynamically?
I have the following control that I have inserted into the aspx body:
<html>
<form runat="server">
<mine:myCustomControl id="obj01" runat="server" height="40%"
Text="<p>content</p>"></mine:myCustomControl>
</form>
...
But rather than calling it from the aspx body I need to be able to call
it from a function, so i can create several instances of the control if
I need to, each one with different id and Text attribute values.
So I would like something like:
public void displayEditor() {
//for each chunk of content create an id and get the text, then
Response.Write(<mine:myCustomControl id=\"" + theId + "\"
runat=\"server\" height=\"40%\" Text=\"" + theContent +
"\"></mine:myCustomControl>);
}
then in the html:
<html>
<form runat="server">
<% displayEditor(); %>
</form>
...
Any ideas how to write the displayEditor function so it actually
works?
Thanks in advance.
JasonP.
**********************************************************************
WARNING
This email message and any attached files may contain information
that is confidential and subject of legal privilege intended only for
use by the individual or entity to whom they are addressed. If you
are not the intended recipient or the person responsible for
delivering the message to the intended recipient be advised that you
have received this message in error and that any use, copying,
circulation, forwarding, printing or publication of this message or
attached files is strictly forbidden, as is the disclosure of the
information contained therein. If you have received this message in
error, please notify the sender immediately and delete it from your
InBox.
AFP Web site: http://www.afp.gov.au
**********************************************************************
Message #2 by "Ken Schaefer" <ken@a...> on Fri, 8 Nov 2002 15:27:16 +1100
|
|
You can use LoadControl() which takes the path to the ascx file. Something
like:
<script>
Dim ctlMyControl as Control
ctlMyControl = LoadControl("myControl.ascx")
plhMyPlaceholder.Controls.Add(ctlMyControl)
</script>
<asp:placeholder id="plhMyPlaceholder" runat="server" />
-or-
Look in the IBuySpy sample (www.ibuyspy.com), which uses controls
extensively
Cheers
Ken
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Jason Pluis" <Jason.Pluis@a...>
Subject: [aspx_beginners] include custom control dynamically
: Does anyone know how to include a custom control dynamically?
:
: I have the following control that I have inserted into the aspx body:
:
: <html>
: <form runat="server">
: <mine:myCustomControl id="obj01" runat="server" height="40%"
: Text="<p>content</p>"></mine:myCustomControl>
: </form>
: ...
:
: But rather than calling it from the aspx body I need to be able to call
: it from a function, so i can create several instances of the control if
: I need to, each one with different id and Text attribute values.
:
: So I would like something like:
:
: public void displayEditor() {
: //for each chunk of content create an id and get the text, then
: Response.Write(<mine:myCustomControl id=\"" + theId + "\"
: runat=\"server\" height=\"40%\" Text=\"" + theContent +
: "\"></mine:myCustomControl>);
: }
:
: then in the html:
: <html>
: <form runat="server">
: <% displayEditor(); %>
: </form>
: ...
:
:
: Any ideas how to write the displayEditor function so it actually
: works?
:
: Thanks in advance.
: JasonP.
Message #3 by "Jason Pluis" <jason.pluis@a...> on Fri, 8 Nov 2002 05:35:18
|
|
Ken,
Thanks Ken, that has got the ball rolling. My code is semi-working now :-)
My problem now is thatI need to be able to adjust some of the attributes
of the customControl before adding them: (the Id and the Content)
I get this error:
Compiler Error Message: CS0117: 'System.Web.UI.Control' does not contain a
definition for 'id'
when I run the following code. How do I access the attributes of
dynamically added *user* control??
public void displayEditor( string Id, string Content, string Type, string
Caption) {
//do some stuff here to display the editor...
Control thisControl;
thisControl = LoadControl("editor.ascx");
thisControl.id = Id; <__???
thisControl.Text = Content; <__???
myPlaceHolder.Controls.Add(thisControl);
}
Cheers,
JasonP.
> You can use LoadControl() which takes the path to the ascx file.
Something
like:
<script>
Dim ctlMyControl as Control
ctlMyControl = LoadControl("myControl.ascx")
plhMyPlaceholder.Controls.Add(ctlMyControl)
</script>
<asp:placeholder id="plhMyPlaceholder" runat="server" />
-or-
Look in the IBuySpy sample (www.ibuyspy.com), which uses controls
extensively
Cheers
Ken
Message #4 by "Ken Schaefer" <ken@a...> on Fri, 8 Nov 2002 17:44:48 +1100
|
|
Try:
thisControl.ID = Id;
C# is case sensitive no?
Also, since thisControl is of type Control, there is no .Text property. So,
I think you'll need to do a cast to your user control type to be able to set
the .Text property. (I'm no expert, so this could all be a real horribly
wrong!). In VB.Net you'd do:
<% @Register TagPrefix="Whatever" TagName="MyControlName"
Src="whatever.ascx" %>
<script runat="server">
CType(thisControl, MyControlName).Text = "myText"
</script>
Not sure how you do something similar in C# (let me know though!)
Cheers
Ken
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Jason Pluis" <jason.pluis@a...>
Subject: [aspx_beginners] Re: include custom control dynamically
: Thanks Ken, that has got the ball rolling. My code is semi-working now :-)
: My problem now is thatI need to be able to adjust some of the attributes
: of the customControl before adding them: (the Id and the Content)
:
: I get this error:
: Compiler Error Message: CS0117: 'System.Web.UI.Control' does not contain a
: definition for 'id'
: when I run the following code. How do I access the attributes of
: dynamically added *user* control??
:
: public void displayEditor( string Id, string Content, string Type, string
: Caption) {
: //do some stuff here to display the editor...
: Control thisControl;
: thisControl = LoadControl("editor.ascx");
: thisControl.id = Id; <__???
: thisControl.Text = Content; <__???
: myPlaceHolder.Controls.Add(thisControl);
: }
:
: Cheers,
: JasonP.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
 |