<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TechTips &#187; ASP.NET</title>
	<atom:link href="http://kshitijsharma.net/category/netaspnetvbnetcsql-server-mysql/net/aspnet-net-netaspnetvbnetcsql-server-mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://kshitijsharma.net</link>
	<description>.NET/ CSLA.NET / ASP.NET / VB.NET / C# / SQL Server / MySql ..</description>
	<lastBuildDate>Tue, 24 Aug 2010 15:37:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Dynamically create controls at runtime</title>
		<link>http://kshitijsharma.net/2009/05/28/dynamically-create-controls-at-runtime/</link>
		<comments>http://kshitijsharma.net/2009/05/28/dynamically-create-controls-at-runtime/#comments</comments>
		<pubDate>Fri, 29 May 2009 04:56:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://kshitijsharma.net/?p=3</guid>
		<description><![CDATA[<table border="0">
<tbody>
<tr>
<td>Hi,
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.</td>
</tr>
<tr>
<td><img class="alignleft size-medium wp-image-13" title="tblFieldMaster" src="http://kshitijsharma.net/wp-content/uploads/2009/05/tabledesign-300x121.gif" alt="tblFieldMaster" width="300" height="121" /></td>
</tr>
<tr>
<td>Now Insert some data in it.</td>
</tr>
<tr>
<td><img class="alignleft size-full wp-image-12" title="screenhunter_02-may-30-1017" src="http://kshitijsharma.net/wp-content/uploads/2009/05/screenhunter_02-may-30-1017.gif" alt="screenhunter_02-may-30-1017" width="400" height="129" /></td>
</tr>
<tr>
<td>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.</td>
</tr></table>]]></description>
			<content:encoded><![CDATA[<p><!-- You will NOT be able to see the ad on your site! This unit is hidden on your page, and will only display to your search engine traffic (from US and CA). To preview, paste the code up on your site, then add #chitikatest=mortgage to the end of your URL in your browser's address bar.  Example:  www.yourwebsite.com#chitikatest=mortgage. This will show you what the ad would look like to a user who is interested in "mortgages." -->
<script type="text/javascript"><!--
ch_client = "kshitijsharma";
ch_type = "mpu";
ch_width = 500;
ch_height = 250;
ch_noborders=0;
ch_non_contextual = 4;
ch_vertical ="premium";
ch_sid = "Chitika Premium";
var ch_queries = new Array( );
var ch_selected=Math.floor((Math.random()*ch_queries.length));
if ( ch_selected < ch_queries.length ) {
ch_query = ch_queries[ch_selected];
}
//--></script>
<script  src="http://scripts.chitika.net/eminimalls/amm.js" type="text/javascript">
</script></p>
<table border="0">
<tbody>
<tr>
<td>Hi,<br />
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&#8217;s first create a master table which will store details about fields need to appear on our page.<br />
Let&#8217;s call this table tblFieldMaster.</td>
</tr>
<tr>
<td><img class="alignleft size-medium wp-image-13" title="tblFieldMaster" src="http://kshitijsharma.net/wp-content/uploads/2009/05/tabledesign-300x121.gif" alt="tblFieldMaster" width="300" height="121" /></td>
</tr>
<tr>
<td>Now Insert some data in it.</td>
</tr>
<tr>
<td><img class="alignleft size-full wp-image-12" title="screenhunter_02-may-30-1017" src="http://kshitijsharma.net/wp-content/uploads/2009/05/screenhunter_02-may-30-1017.gif" alt="screenhunter_02-may-30-1017" width="400" height="129" /></td>
</tr>
<tr>
<td>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&#8217;s Name it createControl.aspx.<br />
Add following code to newly added webform between tag.</td>
</tr>
<tr>
<td><!--Session data--></p>
<pre class="brush: xml;">
&lt;body&gt;
&lt;form id=&quot;frmMain&quot; runat=&quot;server&quot;&gt;
&lt;div&gt;
&lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot;&gt;
&lt;/asp:ScriptManager&gt;
&lt;table width=&quot;550px&quot; cellspacing=&quot;1&quot; cellpadding=&quot;1&quot; class=&quot;outerBG&quot; align=&quot;center&quot;&gt;
&lt;tr&gt;
&lt;td align=&quot;center&quot;&gt;
&lt;asp:UpdatePanel ID=&quot;UpdatePanel1&quot; runat=&quot;server&quot;&gt;
&lt;ContentTemplate&gt;
&lt;asp:Panel ID=&quot;Panel1&quot; runat=&quot;server&quot; CssClass=&quot;innerTabBg&quot;&gt;
&lt;/asp:Panel&gt;
&lt;/ContentTemplate&gt;
&lt;/asp:UpdatePanel&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
</pre>
<p>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.</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: 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=' &amp; dbPath &amp; ';'
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 &amp; ' 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 Class
</pre>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td>I think the code is quite self explanatory. Please ask away if you don&#8217;t get any point of this code.I hope this will help others who are looking to do same.</td>
</tr>
<tr>
<td>
<input id="gwProxy" type="hidden" />
<p><!--Session data--></p>
<input id="jsProxy" onclick="jsCall();" type="hidden" />
<input id="gwProxy" type="hidden" />
</td>
</tr>
</tbody>
</table>
<div id="st0000000001" class="st-taf"><script src="http://taf.socialtwist.com:80/taf/js/shoppr.core.js?id=0000000001"></script><img style="border:0;margin:0;padding:0;" src="http://tellafriend.socialtwist.com:80/wizard/images/tafbutton_blue16.png" onmouseout="hideHoverMap(this)" onmouseover="showHoverMap(this, '0000000001', 'http%3A%2F%2Fkshitijsharma.net%2F2009%2F05%2F28%2Fdynamically-create-controls-at-runtime%2F', 'Dynamically+create+controls+at+runtime')" onclick="cw(this, {id:'0000000001',link: 'http%3A%2F%2Fkshitijsharma.net%2F2009%2F05%2F28%2Fdynamically-create-controls-at-runtime%2F', title: '+Dynamically+create+controls+at+runtime+' })"/></div>]]></content:encoded>
			<wfw:commentRss>http://kshitijsharma.net/2009/05/28/dynamically-create-controls-at-runtime/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
