Dynamically Creating controls

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

Guest

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
 
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 said:
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
 
Thanks Peter for the help

Peter Laan said:
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
 
Back
Top