Form code generator V1.1 by Steve Schofield[bate2 Grade: Intermediate] (reprint: Aspfree)

Source: Internet
Author: User
Tags chr dsn generator tostring
This article revolves around being plain lazy. When it comes to creating Form code based on some database table, I hate it!   This code sample goes along way into speeding this process up to me. There still is some manual parts to finish up the form code but this takes care of remembering, what columns are in the DAT    Abase table.  In future releases, we'll provide more functionality to further automate this but this is the big I-I opinion! The following four steps listed below can be followed and this'll generate the ASP.net code. A big to Dave W. Webmaster's for saving me on many things!!

Define What database you want to connect to the Config.web. This is stored in the connection string
<configuration>
<appsettings>
<add key= "DSN" value= "server=localhost;uid=sa;pwd=;d atabase=aspfree"/>
</appsettings>
</configuration>
Load the ASPX page in your browser, select the table to create the Form code from
Select the Checkboxs of which fields to is on the form
Copy and paste into your code.
This is a screen shot of the File after following the above steps.



This is the code:

<%@ Page language= "VB" enablesessionstate= "false" enableviewstate= "true" Trace= "false" debug= "false" strict= "True" %>
<%@ Import namespace= "System.Text"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script language= "VB" runat= "Server" >
Dim SQLText as String
Dim DS as New DataSet
Dim Dbcomm as New SqlDataAdapter
Dim Conn as SqlConnection
Dim SQL Server as String

Sub Page_Load (sender as Object, E as EventArgs)

SQL Server = Getsqlconn ()
conn = New SqlConnection (SQL Server)


If not IsPostBack Then
SQLText = "SELECT ID, name from sysobjects where xtype= ' U ' ORDER by name"
Dbcomm = New SqlDataAdapter (sqltext,conn)
Dbcomm.fill (ds, "AllTables")
Tbllist.datasource = ds. Tables ("AllTables"). DefaultView
Tbllist.datatextfield = "Name"
Tbllist.datavaluefield = "Name"
Tbllist.databind ()
End If


End Sub

Function Createvalidator (myname As String) as String
Dim MYSB as StringBuilder = New StringBuilder ()

REM--use: <some Text>: as placeholders
Mysb.append ("<asp:requiredfieldvalidator runat=" "Server" "Id=" ": Name:" "controltovalidate=": Control: "" Errormessage= "": ErrMsg: "" display= "" Static "" >this Required field!</asp:requiredfieldvalidator> ")

Mysb.replace (": Name:", "VLD" & myname) ' Add ' validator Name
Mysb.replace (": Control:", "at" & myname) ' Add ' control name
Mysb.replace (": ErrMsg:", MyName & "is required")

Return mysb.tostring ()

End Function


Function Getsqlconn () as String
Dim DSN as String = ConfigurationSettings.AppSettings ("DSN")
Return DSN
End Function


Sub Gettable_click (sender as Object, E as EventArgs)
Dim SQLText as String
SQLText = "Select Syscolumns.name, syscolumns.isnullable from sysobjects INNER JOIN syscolumns on sysobjects.id = Syscolum ns.id where sysobjects.name = ' "& TblList.SelectedItem.Text & ' ORDER by Syscolumns.colid '

REM--Connect to SQL
Dbcomm = New SqlDataAdapter (sqltext,conn)

REM--Fill DataSet
Dbcomm.fill (ds, "TestData")
Mydatagrid.datasource = ds. Tables ("TestData"). DefaultView
Mydatagrid.databind ()


REM-Show the results
Mypanel.visible= True

End Sub



Sub Button1_Click (sender as Object, E as EventArgs)

Dim I as Integer
Dim _item as DataGridItem
Dim Dr as DataRow
Dim sb as StringBuilder = New StringBuilder ()
Dim Stroutput as String

REM--Auto Generate the Form
Sb. Append ("<form runat=" "Server" "id=" "Form2" "Name=" "Form2" ">" & Chr () & Chr (10))
Sb. Append ("<table border=1>" & Chr (13))


For i = 0 to Mydatagrid.items.count-1
REM--Get the checkbox
_item = Mydatagrid.items (i)
Dim AddCheckBox as CheckBox = Ctype (_item. FindControl ("Chkadd"), CheckBox)
Dim Validcheckbox as CheckBox = Ctype (_item. FindControl ("Chkvalid"), CheckBox)

If Addcheckbox.checked Then
Sb. Append ("<tr>" & Chr (13))
Sb. Append ("<td>" & _item. Cells (1). Text & "</td>" & Chr (13))
Sb. Append ("<td>")
Sb. Append ("<asp:textbox id=" "at" & _item. Cells (1). Text & "" runat= "" Server ""/> ")

' Create a Validator control
If Validcheckbox.checked Then
Sb. Append ("" & Chr () & Createvalidator (_item. Cells (1). Text))
End If

Sb. Append ("</td>" & Chr (13)) '
Sb. Append ("</tr>" & Chr ()) ' Close out ' row
End If

Next
Sb. Append ("<tr>" & Chr ()) ' Close out ' row
Sb. Append ("<td colspan=" "2" "><asp:button id=" "Button1" "text=" "Validate Form" "runat=" "Server" "/></td > "& Chr (13))
Sb. Append ("</tr>" & Chr ()) ' Close out ' row
Sb. Append ("</table>" & Chr (13))
Sb. Append (CHR & "</form>")
Stroutput = sb. ToString ()
Stroutput = System.Web.HttpUtility.HTMLEncode (stroutput)
Taresults.value = Stroutput
Pnltextarea.visible=true
End Sub

</script>

<title></title>
<body bgcolor= "#FFFFFF" >
<form runat= "Server" id=form1>
Select a tablename to create a. NET form for:
<asp:dropdownlist id= "tbllist" runat= "Server"/>
<asp:button id=gettable text= "Get Table" onclick= "Gettable_click" runat= "Server"/>

<asp:panel id=mypanel runat= "Server" visible= "false" >
<br>
Select the Columns used for generating the form.

<asp:datagrid id=mydatagrid runat= "Server"
Bordercolor= "BLACK"
Borderwidth= "1"
Cellpadding= "3"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
Autogeneratecolumns= "False"
>
<Columns>
<asp:templatecolumn headertext= "Add?" >
<ItemTemplate>
<center>
<asp:checkbox id=chkadd runat= "Server"/>
</center>
</Itemtemplate>
</asp:TemplateColumn>

<asp:boundcolumn headertext= "name" datafield= "name"/>

<asp:templatecolumn headertext= "Create Validator?" >
<ItemTemplate>
<center>
<asp:checkbox id=chkvalid runat= "Server"/>
</center>
</Itemtemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

<asp:button id=button1 text= "Create Form" onclick= "Button1_Click" runat= "Server"/>
</asp:panel>

<asp:panel id= "Pnltextarea" visible= "false" runat= "Server" >
<p>copy This code into a new ASP.net page</p>
<textarea id=taresults cols=90 rows=40 runat= "Server"/>
</asp:panel>

</form>
</body>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.