Imports System
Imports System.Text
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Imports Microsoft.VisualBasic

Public Class createformcode: Inherits System.Web.UI.Page

    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
    Protected WithEvents taResults As System.Web.UI.HtmlControls.HtmlTextArea
    Protected WithEvents pnlTextarea As System.Web.UI.WebControls.Panel
    Protected WithEvents myPanel As System.Web.UI.WebControls.Panel
    Protected WithEvents tblList As System.Web.UI.WebControls.DropDownList
    Protected WithEvents GetTable As System.Web.UI.WebControls.Button
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button    

    Dim sqlText As String
    Dim ds As New DataSet()
    Dim dbComm As New SqlDataAdapter()
    Dim conn As SqlConnection
    Dim sqlServer As String

    Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        sqlServer = GetSqlConn()
        conn = New SqlConnection(sqlServer)
        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(ByVal 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) 
		'增加表单验证控件的名字
        mySB.Replace(":control:", "at" & myName) 
		'增加控件的名称
        mySB.Replace(":errMsg:", myName & " is required")

        Return mySB.ToString()

    End Function

    Function GetSqlConn() As String
	'从web.config文件中读出与SQL SERVER的链接字符串
        Dim DSN As String = ConfigurationSettings.AppSettings("DSN")
        Return DSN
    End Function

    Sub GetTable_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim sqlText As String
        sqlText = "SELECT syscolumns.name, syscolumns.isnullable FROM sysobjects INNER JOIN syscolumns ON sysobjects.id=syscolumns.id where sysobjects.name = '" & tblList.SelectedItem.Text & "' ORDER BY syscolumns.colid"

	'连接到SQL SERVER数据库
        dbComm = New SqlDataAdapter(sqlText, conn)

	'数据填充
        dbComm.Fill(ds, "TestData")
        MyDataGrid.DataSource = ds.Tables("TestData").DefaultView
	'数据绑定
        MyDataGrid.DataBind()


'显示结果
        myPanel.Visible = True

    End Sub

    Public Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim i As Integer
        Dim _item As DataGridItem
        Dim dr As DataRow
        Dim sb As StringBuilder = New StringBuilder()
        Dim strOutput As String

        '自动生成表格
        sb.Append("<form runat=""server"" id=""form2"" name=""form2"">" & chr(13) & chr(10))
        sb.Append(" <table border=1>")


        For i = 0 To MyDataGrid.Items.Count - 1
         '自动生成选择控件
            _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"" />")

                '生成表单验证代码
                If validCheckBox.Checked Then
                    sb.Append(" " & vbCrLf & CreateValidator(_item.Cells(1).Text))
                End If

                sb.Append("</td>" & chr(13))  '
                sb.Append("    </tr>" & chr(13))   
'关闭这一行
            End If

        Next
        sb.Append("    <tr>" & chr(13))   
'关闭这一行
        sb.Append("        <td colspan=""2""><asp:button id=""button1"" Text=""Validate Form"" runat=""Server"" /></td>" & vbCrLf)
        sb.Append("    </tr>" & chr(13))   
'关闭这一行
        sb.Append(" </table>" & chr(13))
        sb.Append(vbCrLf & "</form>")
        strOutput = sb.ToString()
        strOutput = System.Web.HttpUtility.HtmlEncode(strOutput)
'输出生成的代码到文本区
        taResults.Value = strOutput
'显示文本区域
        pnlTextarea.Visible = True
	
    End Sub
'初始化组件
    Private Sub InitializeComponent()

    End Sub

End Class
