Results 1 to 5 of 5

Thread: Generating dynamic Web Controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Location
    India
    Posts
    27

    Question Generating dynamic Web Controls

    Hi Everyone,

    I am not able to figure out how to generate dynamic web controls (textbox, dropdownlist etc) at runtime and assigning different names to each of web controls at runtime using ASP.net. This thing was easily done in ASP but could not know how to do ASP.net. Pls help me out in doing it.

    Thanks in advance

    Jasvinder

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    well, here's the general concept:
    VB Code:
    1. Dim ctl As New TextBox()
    2. ctl.ID = "MyNewTextBox"
    3. Page.FindControl("Form1").Controls.Add(ctl)
    You didn't post any code so I'm not sure what you tried that didn't work.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Location
    India
    Posts
    27
    Thanks pvb for your reply.

    I tried your code but it didn't work. I want to create dynamic web controls in the same way as we used to create in ASP. Here is the code for creating dynamic/multiple textboxes in ASP

    <%for i=1 to 10%>
    <input type=text name=mytext<%=i%>>
    <%next%>

    In the same way i want to create in ASP.Net without using datagrid, datalist etc..

    Pls help me out

    Thanks in advance.

  4. #4
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Keep in mind that the concept of Variants no longer exists in ASP.NET(or anywhere else in .NET for that matter). So to get your code to work how you want you need to declare i:
    VB Code:
    1. <html>
    2.     <body>
    3.         <form id="myForm" method="post" action="DynamicControls.aspx">
    4.             <% Dim i As Integer %>
    5.             <% For i = 1 To 10 %>
    6.             <input type="text" name="mytext<%= i %>" /><br />
    7.             <% Next %>
    8.         </form>
    9.     </body>
    10. </html>

    Here's one of several "ASP.NET" ways of doin that too:
    VB Code:
    1. <script language="vb" runat="server">
    2. Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    3.     If Not Page.IsPostBack Then
    4.         DrawControls()
    5.     End If
    6. End Sub
    7. Sub DrawControls()
    8.     Dim i As Integer
    9.     Dim myNewTextBox As TextBox
    10.     For i = 1 To 10
    11.         myNewTextBox = New TextBox()
    12.         myNewTextBox.ID = "myTextBox" & i.ToString()
    13.         Page.FindControl("myForm").Controls.Add(myNewTextBox)
    14.     Next
    15. End Sub
    16. </script>
    17. <html>
    18.     <body>
    19.         <form id="myForm" runat="server">
    20.         </form>
    21.     </body>
    22. </html>

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Location
    India
    Posts
    27
    hi pvb,
    Thanks dude. This code is working fine.

    But another problem is how can i control them, where to place the controls on the page. Can i integrated it with HTML (in speicfic <tr><td>). I tried to integrate it with HTML but it shwing me an error. Is it possible to integrate dynamic web control with HTML.

    Thanks
    Jasvinder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width