| | |
Dynamicall adding user controls problems!!
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Im trying to dynamically add controls such as text boxes, radio buttons, check boxes, etc to a user control(ascx page). Im basically trying to build a very simple faceted search. Here is the scenario the user defines a string that specifies what is to appear on the page, some thing like this:
searchParams = "Name:TextBox, City:Radio{a;b;c}"
So the Name field will be a text box, and the City field will be a group of the three radio buttons "a,b,c". Im generating the user controls in a select case statement as follows:
Select Case source(1)
Case "TextBox"%>
<%=source(0)%><br />
<asp:TextBox runat=server></asp:TextBox><br />
<% Case "DropDown"
Case "Radio"%>
<%=source(0)%><br />
<%
For Each opt As String In options
Dim radio As RadioButton
radio = New RadioButton
radio.Text = opt
radio.GroupName = "radio1"
Me.Controls.Add(radio)
%>
<%Next
%>
<%End Select
But im getting this error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
when executing this line: Me.Controls.Add(radio)
So I researched this and I read places that I should try and add things to a placeholder instead of the page itself, I tried that but that didnt work. I also tryed using binding syntax <%#blah%> instead of <%%> and that didnt work either. Im running out of ideas and I dont know what the problem is? I need to be able to dynamically generate controls onto the page according to that user defined string. Any ideas?
searchParams = "Name:TextBox, City:Radio{a;b;c}"
So the Name field will be a text box, and the City field will be a group of the three radio buttons "a,b,c". Im generating the user controls in a select case statement as follows:
Select Case source(1)
Case "TextBox"%>
<%=source(0)%><br />
<asp:TextBox runat=server></asp:TextBox><br />
<% Case "DropDown"
Case "Radio"%>
<%=source(0)%><br />
<%
For Each opt As String In options
Dim radio As RadioButton
radio = New RadioButton
radio.Text = opt
radio.GroupName = "radio1"
Me.Controls.Add(radio)
%>
<%Next
%>
<%End Select
But im getting this error: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
when executing this line: Me.Controls.Add(radio)
So I researched this and I read places that I should try and add things to a placeholder instead of the page itself, I tried that but that didnt work. I also tryed using binding syntax <%#blah%> instead of <%%> and that didnt work either. Im running out of ideas and I dont know what the problem is? I need to be able to dynamically generate controls onto the page according to that user defined string. Any ideas?
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: Updating text boxes using drop down lists
- Next Thread: if statement problems
| Thread Tools | Search this Thread |
"crystal .net .net2005 2008 access add advanced application array assignment basic beginner box button buttons center click code combo convert cpu cuesent data database datagrid datagridview designer dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees excel exists firewall forms html image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity open pan pdf picturebox picturebox2 port print printpreview record regex reports" reuse right-to-left save savedialog search serial socket sorting sqldatbase sqlserver storedprocedure string temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vba vbnet vista visual visualbasic visualbasic.net visualstudio.net web wpf wrapingcode xml





