|
-
Dec 31st, 2002, 02:30 AM
#1
Thread Starter
Junior Member
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
-
Jan 2nd, 2003, 05:01 PM
#2
Hyperactive Member
well, here's the general concept:
VB Code:
Dim ctl As New TextBox()
ctl.ID = "MyNewTextBox"
Page.FindControl("Form1").Controls.Add(ctl)
You didn't post any code so I'm not sure what you tried that didn't work.
-
Jan 3rd, 2003, 12:25 AM
#3
Thread Starter
Junior Member
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.
-
Jan 3rd, 2003, 09:36 AM
#4
Hyperactive Member
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:
<html>
<body>
<form id="myForm" method="post" action="DynamicControls.aspx">
<% Dim i As Integer %>
<% For i = 1 To 10 %>
<input type="text" name="mytext<%= i %>" /><br />
<% Next %>
</form>
</body>
</html>
Here's one of several "ASP.NET" ways of doin that too:
VB Code:
<script language="vb" runat="server">
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Page.IsPostBack Then
DrawControls()
End If
End Sub
Sub DrawControls()
Dim i As Integer
Dim myNewTextBox As TextBox
For i = 1 To 10
myNewTextBox = New TextBox()
myNewTextBox.ID = "myTextBox" & i.ToString()
Page.FindControl("myForm").Controls.Add(myNewTextBox)
Next
End Sub
</script>
<html>
<body>
<form id="myForm" runat="server">
</form>
</body>
</html>
-
Jan 3rd, 2003, 10:55 PM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|