ASP.net/VB.net Generating a data bound form at runtime

DRGDRG

New Member
I'm currently trying to put together code that will dynamically generate a set of HTML form controls at runtime, and then databind those controls. From what I've read, you can't databind form controls like , , etc, however all of the information I can find on this was from a few years ago. Not sure if it's possible to do this with .net 4.5 now or not.In any case, what I'm asking is what the best way is to accomplish what I'm trying to do, whether it's utilizing what I already have, or going at it from a completely different direction.The goal of this dynamically generated form is two-fold:[*]The ability to create/modify/delete news posts on an existing website[*]The ability to reuse this code with as little code change as possibleHere is my current code:\[code\]<%@ Import Namespace="System.Data" %> <%@ Page Language="VB" %><script runat="server"> Protected Sub Page_Load(sender As Object, e As EventArgs) Dim dataSourceInfo As New DataSourceInfo dataSourceInfo.TableName = "Post_Table" dataSourceInfo.DataColumns = {"PostTitle", "PostContent", "PostURL", "PublishDate", "ExpirationDate", "Active"} dataSourceInfo.KeyColumn = "ID" BuildCMSForm(dataSourceInfo) BindCMSForm(dataSourceInfo) End Sub Public Structure DataSourceInfo Dim TableName As String Dim DataColumns() As String Dim KeyColumn As String End Structure Public Sub BuildCMSForm(ByVal dataSourceInfo As DataSourceInfo) Dim dataSource As SqlDataSource = New SqlDataSource() Dim strSelectCommand As String dataSource.ProviderName = "System.Data.SqlClient" dataSource.ConnectionString = "Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=Test_Web;Integrated Security=SSPI" strSelectCommand = "SELECT COLUMN_NAME, DATA_TYPE " & _ "FROM INFORMATION_SCHEMA.COLUMNS " & _ "WHERE TABLE_NAME = '" & dataSourceInfo.TableName & "' " & _ "AND COLUMN_NAME IN (" For Each col As String In dataSourceInfo.DataColumns strSelectCommand = strSelectCommand & "'" & col.ToString & "', " Next strSelectCommand = strSelectCommand.Substring(0, strSelectCommand.Length - 2) & ")" dataSource.SelectCommand = strSelectCommand Dim dv As DataView dv = CType(dataSource.Select(DataSourceSelectArguments.Empty), DataView) For Each row As DataRow In dv.Table.Rows() Dim newControlDiv As HtmlGenericControl = New HtmlGenericControl("div") newControlDiv.Attributes.Add("id", "CMSControlDiv" & row.Item(0).ToString) newControlDiv.Attributes.Add("class", "CMSControlDiv") newControlDiv.InnerHtml = row.Item(0).ToString & ":<br />" Select Case row.Item(1).ToString Case "text" Dim newControl As TextBox = New TextBox newControl.TextMode = TextBoxMode.MultiLine newControl.ID = row.Item(0).ToString newControl.Attributes.Add("class", "CMSControl") newControlDiv.Controls.Add(newControl) Case "varchar" Dim newControl As TextBox = New TextBox newControl.ID = row.Item(0).ToString newControl.Attributes.Add("class", "CMSControl") newControlDiv.Controls.Add(newControl) Case "datetime" Dim newControl As TextBox = New TextBox newControl.ID = row.Item(0).ToString newControl.Attributes.Add("class", "CMSControl") newControlDiv.Controls.Add(newControl) Case "bit" Dim newControl As CheckBox = New CheckBox newControl.ID = row.Item(0).ToString newControl.Attributes.Add("class", "CMSControl") newControlDiv.Controls.Add(newControl) End Select AdminForm.Controls.Add(newControlDiv) Next Dim newSubmit As HtmlInputSubmit = New HtmlInputSubmit newSubmit.Attributes.Add("id", "CMSSubmit") newSubmit.Value = "http://stackoverflow.com/questions/15454320/Submit" AdminForm.Controls.Add(newSubmit) End Sub Public Sub BindCMSForm(ByVal dataSourceInfo As DataSourceInfo) Dim dataSource As SqlDataSource = New SqlDataSource() Dim strSelectCommand As String Dim strColumns As String = "" dataSource.ProviderName = "System.Data.SqlClient" dataSource.ConnectionString = "Data Source=LOCALHOST\SQLEXPRESS;Initial Catalog=Test_Web;Integrated Security=SSPI" strSelectCommand = "SELECT TOP 1 {0} FROM {1} WHERE [PostType] = 'Event'" For Each col As String In dataSourceInfo.DataColumns strColumns = strColumns & "[" & col.ToString & "], " Next strColumns = strColumns.Substring(0, strColumns.Length - 2) dataSource.SelectCommand = String.Format(strSelectCommand, strColumns, dataSourceInfo.TableName) Dim dv As DataView dv = CType(dataSource.Select(DataSourceSelectArguments.Empty), DataView) For Each col As DataColumn In dv.Table.Columns() Next End Sub</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <meta charset="utf-8" /> <title></title> <link href="http://stackoverflow.com/questions/15454320/~/styles/main.css" rel="stylesheet" /> </head> <body> <div id="wrapper" runat="server"> <div id="Content" class="Content" runat="server"> <form id="AdminForm" runat="server"> </form> </div> </div> </body></html>\[/code\]Thanks!
 
Back
Top