首页 > 开发 > 综合 > 正文

Form code generator V1.1 by Steve Schofield[bate2

2024-07-21 02:28:00
字体:
来源:转载
供稿:网友

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 in speeding this process up for me.   there still is some manual parts to finish up the form code but this takes care of remembering what columns are in the database table.    in future releases, we'll provide more functionality to further automate this but this is a big first step in my opinion!  the following four steps listed below can be followed and this will generate the asp.net code. a big thanks to dave w. webmaster of  for saving me on many things!!

define what database you want to connect to in the config.web.  this is stored in the connection string
<configuration>
    <appsettings>
        <add key="dsn" value="server=localhost;uid=sa;pwd=;database=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 be on the form
copy and paste into your code..
here is a screen shot of the file after following the above steps.



here 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 sqlserver as string

sub page_load(sender as object, e as eventargs)

          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(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 the validator name
          mysb.replace(":control:","at" & myname) 'add the 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 = syscolumns.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(13) & 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(13) & createvalidator(_item.cells(1).text ) )
                              end if

                              sb.append ("</td>" & chr(13)) '
                              sb.append ( " </tr>" & chr(13)) ' close out the row
                    end if

next
          sb.append ( " <tr>" & chr(13)) ' close out the row
          sb.append(" <td colspan=""2""><asp:button id=""button1"" text=""validate form"" runat=""server"" /></td>" & chr(13))
          sb.append ( " </tr>" & chr(13)) ' close out the row
          sb.append ( " </table>" & chr(13) )
          sb.append(chr(13) & "</form>")
          stroutput = sb.tostring()
          stroutput = system.web.httputility.htmlencode(stroutput)
          taresults.value = stroutput
          pnltextarea.visible=true
end sub

</script>

<html>
<head>
<title></title>
</head>
<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" 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" 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>
</html


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表