Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Creating dynamic controls using ASP.NET 2.0 and VB .NET
Creating dynamic controls using ASP.NET 2.0 and VB .NET

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Controls Tutorial

This tutorial will show you how to create dynamic, persistent, web controls on the fly using ASP.NET 2.0 and VB.NET

In some development situations you may not know how many controls you need ahead of time. In these cases it is easier to dynamically create web controls in a container control (such as a Panel) on your form while your application is running. To do this we do not need to be importing any special namespaces, however one implication of creating dynamic controls is that the dynamic controls you create are not preserved after postbacks so you will need some mechanism to remember the controls they've created dynamically. One way to do is using a simple array, and this is what we'll use in the following example. First in our _Default class we will declare a static Button array member called btn_arr, and a static int called btn_count. btn_arr will hold a copy of the controls we create on-the-fly and btn_count will keep track of how many new controls we create.

Partial Class _Default
Inherits System.Web.UI.Page

Private Shared btn_arr As Button() = New Button(19) {}
Private Shared btn_count As Integer

...

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

In the Page_Load event, we will read our button array and re-create all of the dynamic controls we stored in it. In this example, we read each array item into a Button object and use the Controls.Add() method of our container control (pnlMain) to add them to that controls collection.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If TypeOf btn_arr(0) Is Button Then
'for each button saved in our array, recreate it
For Each button As Button In btn_arr
add_button(button)
Next button
End If
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub

Protected Sub add_button(ByVal button As Button)
Try
'add button to button array
btn_arr(btn_count) = button
btn_count = btn_count + 1
'add to a container on the page
pnlMain.Controls.Add(button)
'add a spacer after the control
pnlMain.Controls.Add(New LiteralControl("<br>"))
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

When we actually click on our btnSubmit button the real work happens. We instantiate a new Button type and assign it some initial values taken from the UI. It is then stored in our btn_arr and passed off to our add_button() function which adds it to the Panel control's collection.

On the front end we have a couple of text boxes that ask for the control ID, Text, and ForeColor. We also have a submit button and a label. The front end .aspx page looks something like this:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" rowspan="2" align="right" bgcolor="#eeeeee" class="header1"> Web
Control added Dynamically:</td>
<td align="center" bgcolor="#FFFFFF"> <br />
<asp:Panel ID="pnlMain" runat="server" Height="300px" Width="500px">
</asp:Panel>
<br /> &namp;bsp;
</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<asp:button ID="btnSubmit" runat="server" Text="Create Button" OnClick="btnSubmit_Click" />Button
ID:
<asp:TextBox ID="txtID" runat="server" Width="64px"></asp:TextBox>
Button Color:<asp:TextBox ID="txtForeColor" runat="server" Width="64px"></asp:TextBox> <br />
Button Text:<asp:TextBox ID="txtText" runat="server" Width="64px"></asp:TextBox> <br />
<asp:label ID="lblStatus" runat="server"></asp:label> </td>
</tr>
</table>

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The flow for the code behind page is as follows.

Partial Class _Default Inherits System.Web.UI.Page

Private Shared btn_arr As Button() = New Button(19) {}
Private Shared btn_count As Integer

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If TypeOf btn_arr(0) Is Button Then
'for each button saved in our array, recreate it
For Each button As Button In btn_arr
add_button(button)
Next button
End If
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
'create a new instance of the control
Dim new_button As Button = New Button()
new_button.ID = txtID.Text
new_button.ForeColor = System.Drawing.Color.FromName(txtForeColor.Text)
new_button.Text = txtText.Text
'add button to button array
btn_arr(btn_count) = new_button
btn_count = btn_count + 1
'call our add function
add_button(new_button)
lblStatus.Text &= "Created button " & new_button.ID & " and of color " & new_button.ForeColor.ToString()
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub

Protected Sub add_button(ByVal button As Button)
Try
'add button to button array
btn_arr(btn_count) = button
btn_count = btn_count + 1
'add to a container on the page
pnlMain.Controls.Add(button)
'add a spacer after the control
pnlMain.Controls.Add(New LiteralControl("<br>"))
Catch ex As Exception
lblStatus.Text += ex.Message.ToString()
End Try
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks