473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Dynamically adding controls at run time

Joe
I have an xml file which looks similar to this:

<data>
Please enter the value:
<dynamicControl id="myDC", type="Textbox", MaxLength="100"/>
</data>

I am trying to devise a way to have a web page load this file in it's Load
event and render the text and controls at run-time, so that I can dynamicall
change the content that a user sees and the controls that a user can use by
altering the xml file.

Does anyone know how to do this? How do I dynamically create a control in
my code based on string parameter values?

TIA,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
Nov 19 '05 #1
4 2083
Well, there's always the good old

Control ctrl = null;
switch (type)
{
case "Textbox":
ctrl = new TextBox();
break;
case ...
case ...
case ...
}
if (ctrl != null)
{
plc.Controls.Add(ctrl);
}
but that doesn't sound fun, does it? :)

You can load types like this:

Type t = Type.GetType("Textbox")
if (t != null)
{
Control c = (Control)Activator.CreateInstance(t);
}
there are 2 problems with this. (a) types stored in the GAC (which all the
built-in ones are), require the fully quantified STRONG name. So you'd need
to have "System.Web.UI.WebControls.TextBox, System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

Don't fret, if you go to c:\windows\assembly and view the properties of
System.Web you'll get everything you need (the above information is correct
actually ;) )

The other problem is that this will give you Control object, so you can't do

Control c =
(Control)Activator.CreateInstance("System.Web.UI.W ebControls.TextBox,
System.Web, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a");
c.MaxLength = x.Attributes["maxLength"].value;

since control doesn't define MaxLength.

and you can't declare it a textbox, 'cuz if you knew it was gonna be a
textbox we wouldn't have this mess to begin with ;)

I guess what I'd do is copy all the attributes of you xml element which
aren't "id", "type" or any other standard WEBControl property and copy them
into the control's attribute property:

Type t = Type.GetType("Textbox")
if (t != null)
{
WebControl c = (WebControl)Activator.CreateInstance(t);
foreach (XmlAttribute attribute in element)
{
if (attribute.Name != "type" && attribute.Name != "id")
{
c.Attributes.Add(attribute.Name, attribute.Value);
}
}
}
anyways, all this code is off the top of my head, but hope it helps..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:AA**********************************@microsof t.com...
I have an xml file which looks similar to this:

<data>
Please enter the value:
<dynamicControl id="myDC", type="Textbox", MaxLength="100"/>
</data>

I am trying to devise a way to have a web page load this file in it's Load
event and render the text and controls at run-time, so that I can
dynamicall
change the content that a user sees and the controls that a user can use
by
altering the xml file.

Does anyone know how to do this? How do I dynamically create a control in
my code based on string parameter values?

TIA,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation

Nov 19 '05 #2
Joe
Thanks. This helps. May I ask, what is "plc?"
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Karl Seguin" wrote:
Well, there's always the good old

Control ctrl = null;
switch (type)
{
case "Textbox":
ctrl = new TextBox();
break;
case ...
case ...
case ...
}
if (ctrl != null)
{
plc.Controls.Add(ctrl);
}
but that doesn't sound fun, does it? :)

You can load types like this:

Type t = Type.GetType("Textbox")
if (t != null)
{
Control c = (Control)Activator.CreateInstance(t);
}
there are 2 problems with this. (a) types stored in the GAC (which all the
built-in ones are), require the fully quantified STRONG name. So you'd need
to have "System.Web.UI.WebControls.TextBox, System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

Don't fret, if you go to c:\windows\assembly and view the properties of
System.Web you'll get everything you need (the above information is correct
actually ;) )

The other problem is that this will give you Control object, so you can't do

Control c =
(Control)Activator.CreateInstance("System.Web.UI.W ebControls.TextBox,
System.Web, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a");
c.MaxLength = x.Attributes["maxLength"].value;

since control doesn't define MaxLength.

and you can't declare it a textbox, 'cuz if you knew it was gonna be a
textbox we wouldn't have this mess to begin with ;)

I guess what I'd do is copy all the attributes of you xml element which
aren't "id", "type" or any other standard WEBControl property and copy them
into the control's attribute property:

Type t = Type.GetType("Textbox")
if (t != null)
{
WebControl c = (WebControl)Activator.CreateInstance(t);
foreach (XmlAttribute attribute in element)
{
if (attribute.Name != "type" && attribute.Name != "id")
{
c.Attributes.Add(attribute.Name, attribute.Value);
}
}
}
anyways, all this code is off the top of my head, but hope it helps..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:AA**********************************@microsof t.com...
I have an xml file which looks similar to this:

<data>
Please enter the value:
<dynamicControl id="myDC", type="Textbox", MaxLength="100"/>
</data>

I am trying to devise a way to have a web page load this file in it's Load
event and render the text and controls at run-time, so that I can
dynamicall
change the content that a user sees and the controls that a user can use
by
altering the xml file.

Does anyone know how to do this? How do I dynamically create a control in
my code based on string parameter values?

TIA,
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation


Nov 19 '05 #3
it's just a placeholder control, whatever/wherever you want to load the
control

<asp:placeholder id="plc" runat="server" />

protected PlaceHolder plc;

it's static on the page.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:6C**********************************@microsof t.com...
Thanks. This helps. May I ask, what is "plc?"
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Karl Seguin" wrote:
Well, there's always the good old

Control ctrl = null;
switch (type)
{
case "Textbox":
ctrl = new TextBox();
break;
case ...
case ...
case ...
}
if (ctrl != null)
{
plc.Controls.Add(ctrl);
}
but that doesn't sound fun, does it? :)

You can load types like this:

Type t = Type.GetType("Textbox")
if (t != null)
{
Control c = (Control)Activator.CreateInstance(t);
}
there are 2 problems with this. (a) types stored in the GAC (which all
the
built-in ones are), require the fully quantified STRONG name. So you'd
need
to have "System.Web.UI.WebControls.TextBox, System.Web,
Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

Don't fret, if you go to c:\windows\assembly and view the properties of
System.Web you'll get everything you need (the above information is
correct
actually ;) )

The other problem is that this will give you Control object, so you can't
do

Control c =
(Control)Activator.CreateInstance("System.Web.UI.W ebControls.TextBox,
System.Web, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a");
c.MaxLength = x.Attributes["maxLength"].value;

since control doesn't define MaxLength.

and you can't declare it a textbox, 'cuz if you knew it was gonna be a
textbox we wouldn't have this mess to begin with ;)

I guess what I'd do is copy all the attributes of you xml element which
aren't "id", "type" or any other standard WEBControl property and copy
them
into the control's attribute property:

Type t = Type.GetType("Textbox")
if (t != null)
{
WebControl c = (WebControl)Activator.CreateInstance(t);
foreach (XmlAttribute attribute in element)
{
if (attribute.Name != "type" && attribute.Name != "id")
{
c.Attributes.Add(attribute.Name, attribute.Value);
}
}
}
anyways, all this code is off the top of my head, but hope it helps..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:AA**********************************@microsof t.com...
>I have an xml file which looks similar to this:
>
> <data>
> Please enter the value:
> <dynamicControl id="myDC", type="Textbox", MaxLength="100"/>
> </data>
>
> I am trying to devise a way to have a web page load this file in it's
> Load
> event and render the text and controls at run-time, so that I can
> dynamicall
> change the content that a user sees and the controls that a user can
> use
> by
> altering the xml file.
>
> Does anyone know how to do this? How do I dynamically create a control
> in
> my code based on string parameter values?
>
> TIA,
> --
> Joe
>
> VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation


Nov 19 '05 #4
Joe
Thanks Karl. This does the trick!
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Karl Seguin" wrote:
it's just a placeholder control, whatever/wherever you want to load the
control

<asp:placeholder id="plc" runat="server" />

protected PlaceHolder plc;

it's static on the page.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:6C**********************************@microsof t.com...
Thanks. This helps. May I ask, what is "plc?"
--
Joe

VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation
"Karl Seguin" wrote:
Well, there's always the good old

Control ctrl = null;
switch (type)
{
case "Textbox":
ctrl = new TextBox();
break;
case ...
case ...
case ...
}
if (ctrl != null)
{
plc.Controls.Add(ctrl);
}
but that doesn't sound fun, does it? :)

You can load types like this:

Type t = Type.GetType("Textbox")
if (t != null)
{
Control c = (Control)Activator.CreateInstance(t);
}
there are 2 problems with this. (a) types stored in the GAC (which all
the
built-in ones are), require the fully quantified STRONG name. So you'd
need
to have "System.Web.UI.WebControls.TextBox, System.Web,
Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

Don't fret, if you go to c:\windows\assembly and view the properties of
System.Web you'll get everything you need (the above information is
correct
actually ;) )

The other problem is that this will give you Control object, so you can't
do

Control c =
(Control)Activator.CreateInstance("System.Web.UI.W ebControls.TextBox,
System.Web, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a");
c.MaxLength = x.Attributes["maxLength"].value;

since control doesn't define MaxLength.

and you can't declare it a textbox, 'cuz if you knew it was gonna be a
textbox we wouldn't have this mess to begin with ;)

I guess what I'd do is copy all the attributes of you xml element which
aren't "id", "type" or any other standard WEBControl property and copy
them
into the control's attribute property:

Type t = Type.GetType("Textbox")
if (t != null)
{
WebControl c = (WebControl)Activator.CreateInstance(t);
foreach (XmlAttribute attribute in element)
{
if (attribute.Name != "type" && attribute.Name != "id")
{
c.Attributes.Add(attribute.Name, attribute.Value);
}
}
}
anyways, all this code is off the top of my head, but hope it helps..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Joe" <jo******@donotspam.yahoo.com> wrote in message
news:AA**********************************@microsof t.com...
>I have an xml file which looks similar to this:
>
> <data>
> Please enter the value:
> <dynamicControl id="myDC", type="Textbox", MaxLength="100"/>
> </data>
>
> I am trying to devise a way to have a web page load this file in it's
> Load
> event and render the text and controls at run-time, so that I can
> dynamicall
> change the content that a user sees and the controls that a user can
> use
> by
> altering the xml file.
>
> Does anyone know how to do this? How do I dynamically create a control
> in
> my code based on string parameter values?
>
> TIA,
> --
> Joe
>
> VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA Automation


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
0
by: Sinisa Ruzin | last post by:
Hi all, I had problem with dynamically adding/removing controls;ascx, Controls.Add(Page.LoadControl... in the same page of the IBuySpy portal. ASP.NET, C#. I added buttons to the main ASCX loaded...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
7
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'm adding subheadings to a gridview. Each sub head has a few link buttons. I'm adding the controls in the rowdatabound event code follows: sorry about the length here. I have to be missing...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.