Dynamically Adding User Controls at Runtime

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Nov 2008
Posts: 2
Reputation: patelb is an unknown quantity at this point 
Solved Threads: 0
patelb patelb is offline Offline
Newbie Poster

Dynamically Adding User Controls at Runtime

 
0
  #1
Nov 26th, 2008
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:

  1. Select Case source(1)
  2. Case "TextBox"%>
  3. <%=source(0)%><br />
  4. <asp:TextBox runat=server></asp:TextBox><br />
  5. <% Case "DropDown"
  6. Case "Radio"%>
  7. <%=source(0)%><br />
  8. <%
  9. For Each opt As String In options
  10. Dim radio As RadioButton
  11. radio = New RadioButton
  12. radio.Text = opt
  13. radio.GroupName = "radio1"
  14. Me.Controls.Add(radio)
  15. %>
  16. <%Next
  17. %>
  18.  
  19. <%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?
Last edited by peter_budo; Nov 28th, 2008 at 1:20 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC