473,385 Members | 1,912 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,385 software developers and data experts.

Dynamically Creating controls

Hello EveryBody,
I have a dropdownlist with some values as .net server controls & a button ,
on clicking a button i need to generate the choosen contorl dynamically.

I am giving a small glimpse of my code for your understanding
the aspx page has dropdwon list with values as below.

<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Label">Label</asp:ListItem>
<asp:ListItem Value="DropDownList">DropDownList</asp:ListItem>
<asp:ListItem Value="DataGrid">DataGrid</asp:ListItem>
<asp:ListItem Value="DataList">DataList</asp:ListItem>
<asp:ListItem Value="CheckBox">CheckBox</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="Table">Table</asp:ListItem>
<asp:ListItem Value="TextBox">TextBox</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

now the code that gets executed at the click of the button is as follows

i am using the reflection name space

String strCtrlName = DropDownList1.SelectedItem.Value ;
Response.Write(strCtrlName);

Type tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName ,true) ;
Object obj = Activator.CreateInstance(tp0;
PlaceHolder.Controls.Add(new obj );
but i get error at the line " Type
tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName ,true) ; "

error below :

System.SystemException: {"Could not load type
System.Web.UI.WebControls.DataGrid from assembly TestingDynamicCtrl,
Version=1.0.1950.28617, Culture=neutral, PublicKeyToken=null."}
AssemblyName: "TestingDynamicCtrl, Version=1.0.1950.28617, Culture=neutral,
PublicKeyToken=null"
ClassName: "System.Web.UI.WebControls.DataGrid"
Message: "Could not load type System.Web.UI.WebControls.DataGrid from
assembly TestingDynamicCtrl, Version=1.0.1950.28617, Culture=neutral,
PublicKeyToken=null."
MessageArg: null
ResourceId: -2146233054
TypeName: "System.Web.UI.WebControls.DataGrid"

so could you pls help me out with your expert advices on solving the problem
and generating the control dynamically.

your expert advice would be highly appreciated.

Rgds
Shiju

Nov 19 '05 #1
4 1660
When getting the type of a class, you need to specify the assembly, ala:

"System.Web.UI.WebControls.DropDownList, System.Web"

Since the "System.Web" assembly is in the GAC, you must also specify the
version, publickeytoken and culture, ala:

"System.Web.UI.WebControls.Label, System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

You can get the information by checking out c:\windows\assembly though
it'll be the same for you..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Hello EveryBody,
I have a dropdownlist with some values as .net server controls & a button
,
on clicking a button i need to generate the choosen contorl dynamically.

I am giving a small glimpse of my code for your understanding
the aspx page has dropdwon list with values as below.

<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Label">Label</asp:ListItem>
<asp:ListItem Value="DropDownList">DropDownList</asp:ListItem>
<asp:ListItem Value="DataGrid">DataGrid</asp:ListItem>
<asp:ListItem Value="DataList">DataList</asp:ListItem>
<asp:ListItem Value="CheckBox">CheckBox</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="Table">Table</asp:ListItem>
<asp:ListItem Value="TextBox">TextBox</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

now the code that gets executed at the click of the button is as follows

i am using the reflection name space

String strCtrlName = DropDownList1.SelectedItem.Value ;
Response.Write(strCtrlName);

Type tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName
,true) ;
Object obj = Activator.CreateInstance(tp0;
PlaceHolder.Controls.Add(new obj );
but i get error at the line " Type
tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName ,true) ;
"

error below :

System.SystemException: {"Could not load type
System.Web.UI.WebControls.DataGrid from assembly TestingDynamicCtrl,
Version=1.0.1950.28617, Culture=neutral, PublicKeyToken=null."}
AssemblyName: "TestingDynamicCtrl, Version=1.0.1950.28617,
Culture=neutral,
PublicKeyToken=null"
ClassName: "System.Web.UI.WebControls.DataGrid"
Message: "Could not load type System.Web.UI.WebControls.DataGrid from
assembly TestingDynamicCtrl, Version=1.0.1950.28617, Culture=neutral,
PublicKeyToken=null."
MessageArg: null
ResourceId: -2146233054
TypeName: "System.Web.UI.WebControls.DataGrid"

so could you pls help me out with your expert advices on solving the
problem
and generating the control dynamically.

your expert advice would be highly appreciated.

Rgds
Shiju

Nov 19 '05 #2
"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Hello EveryBody,
I have a dropdownlist with some values as .net server controls & a button
,
on clicking a button i need to generate the choosen contorl dynamically.

I am giving a small glimpse of my code for your understanding
the aspx page has dropdwon list with values as below.

<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Label">Label</asp:ListItem>
<asp:ListItem Value="DropDownList">DropDownList</asp:ListItem>
<asp:ListItem Value="DataGrid">DataGrid</asp:ListItem>
<asp:ListItem Value="DataList">DataList</asp:ListItem>
<asp:ListItem Value="CheckBox">CheckBox</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="Table">Table</asp:ListItem>
<asp:ListItem Value="TextBox">TextBox</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

now the code that gets executed at the click of the button is as follows

i am using the reflection name space

String strCtrlName = DropDownList1.SelectedItem.Value ;
Response.Write(strCtrlName);

Type tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName
,true) ;
Object obj = Activator.CreateInstance(tp0;
PlaceHolder.Controls.Add(new obj );
but i get error at the line " Type
tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName ,true) ;
"


Try adding the full assembly name to the string.
"System.Web.UI.WebControls.TextBox, System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

You can use

TextBox tb = new TextBox();

string tmp = tb.GetType().AssemblyQualifiedName;

to find out what it is.

Peter


Nov 19 '05 #3
Thanks Peter for the help

"Peter Laan" wrote:
"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Hello EveryBody,
I have a dropdownlist with some values as .net server controls & a button
,
on clicking a button i need to generate the choosen contorl dynamically.

I am giving a small glimpse of my code for your understanding
the aspx page has dropdwon list with values as below.

<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Label">Label</asp:ListItem>
<asp:ListItem Value="DropDownList">DropDownList</asp:ListItem>
<asp:ListItem Value="DataGrid">DataGrid</asp:ListItem>
<asp:ListItem Value="DataList">DataList</asp:ListItem>
<asp:ListItem Value="CheckBox">CheckBox</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="Table">Table</asp:ListItem>
<asp:ListItem Value="TextBox">TextBox</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

now the code that gets executed at the click of the button is as follows

i am using the reflection name space

String strCtrlName = DropDownList1.SelectedItem.Value ;
Response.Write(strCtrlName);

Type tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName
,true) ;
Object obj = Activator.CreateInstance(tp0;
PlaceHolder.Controls.Add(new obj );
but i get error at the line " Type
tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName ,true) ;
"


Try adding the full assembly name to the string.
"System.Web.UI.WebControls.TextBox, System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

You can use

TextBox tb = new TextBox();

string tmp = tb.GetType().AssemblyQualifiedName;

to find out what it is.

Peter


Nov 19 '05 #4
Thanks Karl for the speedy reply, it worked for me.

Rgds
Shiju

"Karl Seguin" wrote:
When getting the type of a class, you need to specify the assembly, ala:

"System.Web.UI.WebControls.DropDownList, System.Web"

Since the "System.Web" assembly is in the GAC, you must also specify the
version, publickeytoken and culture, ala:

"System.Web.UI.WebControls.Label, System.Web, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

You can get the information by checking out c:\windows\assembly though
it'll be the same for you..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Shiju Poyilil" <Sh**********@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
Hello EveryBody,
I have a dropdownlist with some values as .net server controls & a button
,
on clicking a button i need to generate the choosen contorl dynamically.

I am giving a small glimpse of my code for your understanding
the aspx page has dropdwon list with values as below.

<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="Label">Label</asp:ListItem>
<asp:ListItem Value="DropDownList">DropDownList</asp:ListItem>
<asp:ListItem Value="DataGrid">DataGrid</asp:ListItem>
<asp:ListItem Value="DataList">DataList</asp:ListItem>
<asp:ListItem Value="CheckBox">CheckBox</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="RadioButton">RadioButton</asp:ListItem>
<asp:ListItem Value="Table">Table</asp:ListItem>
<asp:ListItem Value="TextBox">TextBox</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>

now the code that gets executed at the click of the button is as follows

i am using the reflection name space

String strCtrlName = DropDownList1.SelectedItem.Value ;
Response.Write(strCtrlName);

Type tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName
,true) ;
Object obj = Activator.CreateInstance(tp0;
PlaceHolder.Controls.Add(new obj );
but i get error at the line " Type
tp=System.Type.GetType("System.Web.UI.WebControls. "+strCtrlName ,true) ;
"

error below :

System.SystemException: {"Could not load type
System.Web.UI.WebControls.DataGrid from assembly TestingDynamicCtrl,
Version=1.0.1950.28617, Culture=neutral, PublicKeyToken=null."}
AssemblyName: "TestingDynamicCtrl, Version=1.0.1950.28617,
Culture=neutral,
PublicKeyToken=null"
ClassName: "System.Web.UI.WebControls.DataGrid"
Message: "Could not load type System.Web.UI.WebControls.DataGrid from
assembly TestingDynamicCtrl, Version=1.0.1950.28617, Culture=neutral,
PublicKeyToken=null."
MessageArg: null
ResourceId: -2146233054
TypeName: "System.Web.UI.WebControls.DataGrid"

so could you pls help me out with your expert advices on solving the
problem
and generating the control dynamically.

your expert advice would be highly appreciated.

Rgds
Shiju


Nov 19 '05 #5

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

Similar topics

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...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
3
by: Dotnet Gruven | last post by:
I've built a WebForm with a Table added dynamically in Page_Load when IsPostBack is false. The table includes a couple of TextBoxes, RadioButtonLists and CheckboxLists. On postback, those...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
6
by: Bjorn Sagbakken | last post by:
Hello In VS2005: I am adding buttons and textboxes dynamically into a table, that also dynamically expands. So far, so good, actually very nice. But I am having trouble starting the desired...
5
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
8
by: BillE | last post by:
When I create a control dynamically and it grows according to the content, the control Height property still returns the original default control height instead of the height after expanding. ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.