Kshitij’s Blog
.NET/ CSLA.NET / ASP.NET / VB.NET / C# / SQL Server / MySql ..
-
May28
Dynamically create controls at runtime
Filed under: ASP.NET;4 CommentsHi,
I was searching an article about creating controls at runtime based on a master table. But I couldn’t find one. So I decided to write my own code and here is the result. I hope this post will help other guys who are looking to do same thing.So let’s first create a master table which will store details about fields need to appear on our page.
Let’s call this table tblFieldMaster.
Now Insert some data in it. 
Now add a new webForm to your website project ( i assume you know how to create a website project with visual studio 2005 or 2008). Let’s Name it createControl.aspx.
Add following code to newly added webform between tag.<body> <form id="frmMain" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <table width="550px" cellspacing="1" cellpadding="1" class="outerBG" align="center"> <tr> <td align="center"> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Panel ID="Panel1" runat="server" CssClass="innerTabBg"> </asp:Panel> </ContentTemplate> </asp:UpdatePanel> </td></tr></table> </div> </form> </body>
As you review this code i have added a scriptmanager so this form supports AJAX feature. Then i added an updatepanel and a Normal Panel inside this update panel. I will now add code to my code behind file which in this case will be createcontrol.vb.
Imports System.Data Imports System.Data.OleDb Partial Class createControl Inherits System.Web.UI.Page Private WithEvents cmdBtn As System.Web.UI.WebControls.Button Private req As Web.UI.WebControls.RequiredFieldValidator Private tbl As System.Web.UI.WebControls.Table Private styl As System.Web.UI.WebControls.TableStyle Private tr As System.Web.UI.WebControls.TableRow Private td As System.Web.UI.WebControls.TableCell Private txt As System.Web.UI.WebControls.TextBox Private lbl As System.Web.UI.WebControls.Label Private conn As OleDbConnection Private da As OleDbDataAdapter Private cmd As New OleDbCommand Private ds As DataSet Private dbPath As String = Server.MapPath('App_Data/regdb.mdb') Private connString As String = 'Provider=Microsoft.Jet.OLEDB.4.0; Data source=' & dbPath & ';' Private strSQL As String Private Sub RenderForm() conn = New OleDbConnection(connString) conn.Open() strSQL = 'Select * from tbl_IVS_Noida' cmd.Connection = conn cmd.CommandText = strSQL cmd.CommandType = CommandType.Text Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader tbl = New Table tbl.ID = 'tblForm' tbl.Width = 550 'tbl.CssClass = ' Me.Panel1.Controls.Add(tbl) While (dr.Read) Select Case dr('Field_Type') Case 'Character', 'Number' tr = New TableRow tbl.Controls.Add(tr) td = New TableCell td.Width = 230 lbl = New Label lbl.Text = dr('Field_Label') td.Controls.Add(lbl) tr.Controls.Add(td) td = New TableCell td.Width = 320 txt = New TextBox txt.ID = dr('Field_Name') td.Controls.Add(txt) If dr('Required') Then req = New RequiredFieldValidator req.ControlToValidate = txt.ID req.ErrorMessage = txt.ID & ' Is Required' td.Controls.Add(req) End If tr.Controls.Add(td) Case 'Date' End Select End While 'Now create Save Button tr = New TableRow td = New TableCell td.ColumnSpan = 2 tr.Controls.Add(td) cmdBtn = New Button cmdBtn.ID = 'btnSave' cmdBtn.Text = 'Save!' td.Controls.Add(cmdBtn) tbl.Controls.Add(tr) End Sub Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init RenderForm() End Sub Private Sub btn_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdBtn.Click End Sub End ClassI think the code is quite self explanatory. Please ask away if you don’t get any point of this code.I hope this will help others who are looking to do same. 
4 Responses to “Dynamically create controls at runtime”
-
Hi,
Thanks for nice article.
This is exactly what i was looking for.
Thanks again.
Keep up good work!John Butler.
-
Thanks John.
-
Agreecycrycle July 12th, 2009 at 12:41 pm
Good Article.
Please write more about this.
Thanks
-
Just looking for this.
Good job.
Leave a Reply


Recent Comments