|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
归根到底,Java跨平台可以,但是要重新编写代码,否则还分什么J2EE/J2SE/J2ME呢!Thisarticlerevolvesaroundbeingplainlazy.WhenitcomestocreatingFormcodebasedonsomedatabasetable,Ihateit!Thiscodesamplegoesalongwayinspeedingthisprocessupforme.Therestillissomemanualpartstofinishuptheformcodebutthistakescareofrememberingwhatcolumnsareinthedatabasetable.Infuturereleases,wellprovidemorefunctionalitytofurtherautomatethisbutthisisabigfirststepinmyopinion!ThefollowingfourstepslistedbelowcanbefollowedandthiswillgeneratetheASP.NETcode.AbigthankstoDaveW.Webmasterofforsavingmeonmanythings!!
Definewhatdatabaseyouwanttoconnecttointheconfig.web.Thisisstoredintheconnectionstring
<configuration>
<appsettings>
<addkey="dsn"value="server=localhost;uid=sa;pwd=;database=aspfree"/>
</appsettings>
</configuration>
Loadtheaspxpageinyourbrowser,selectthetabletocreatetheFormcodefrom
Selectthecheckboxsofwhichfieldstobeontheform
Copyandpasteintoyourcode..
HereisascreenshotoftheFileafterfollowingtheabovesteps.
Hereisthecode:
<%@PageLanguage="VB"EnableSessionState="False"EnableViewState="True"Trace="False"Debug="False"Strict="True"%>
<%@ImportNamespace="System.Text"%>
<%@ImportNamespace="System.Data"%>
<%@ImportNamespace="System.Data.SqlClient"%>
<scriptlanguage="VB"runat="server">
DimsqlTextasString
DimdsasNewDataSet
DimdbCommASNewSQLDataAdapter
DimconnASSQLConnection
DimsqlServerasString
SubPage_Load(senderAsObject,eAsEventArgs)
SQLserver=GetSqlConn()
conn=NewSQLConnection(SQLserver)
IfNotIsPostBackthen
sqlText="selectid,namefromsysobjectswherextype=Uorderbyname"
dbComm=NewSQLDataAdapter(sqlText,conn)
dbComm.Fill(ds,"AllTables")
tblList.DataSource=ds.Tables("AllTables").DefaultView
tblList.DataTextField="name"
tblList.DataValueField="name"
tblList.DataBind()
Endif
EndSub
FunctionCreateValidator(myNameasstring)asString
DimmySBasStringBuilder=NewStringBuilder()
REM--use:<sometext>:asplaceholders
mySB.Append("<asp:RequiredFieldValidatorrunat=""server""id="":Name:""ControlToValidate="":control:""ErrorMessage="":errMsg:""display=""Static"">ThisRequiredField!</asp:RequiredFieldValidator>")
mySb.Replace(":Name:","vld"&myName)addthevalidatorname
mySb.Replace(":control:","at"&myName)addthecontrolname
mySb.Replace(":errMsg:",myName&"isrequired")
ReturnmySb.ToString()
EndFunction
FunctionGetSqlConn()asString
DimDSNasString=ConfigurationSettings.AppSettings("DSN")
ReturnDSN
EndFunction
SubGetTable_Click(senderAsObject,eAsEventArgs)
DimsqlTextasString
sqlText="SELECTsyscolumns.name,syscolumns.isnullableFROMsysobjectsINNERJOINsyscolumnsONsysobjects.id=syscolumns.idwheresysobjects.name="&tblList.SelectedItem.Text&"ORDERBYsyscolumns.colid"
REM--ConnecttoSQL
dbComm=NewSQLDataAdapter(sqlText,conn)
REM--FillDataSet
dbComm.Fill(ds,"TestData")
MyDataGrid.DataSource=ds.Tables("TestData").DefaultView
MyDataGrid.DataBind()
REM--Showtheresults
myPanel.Visible=True
EndSub
SubButton1_Click(senderAsObject,eAsEventArgs)
DimiAsInteger
Dim_itemAsDataGridItem
DimdrAsDataRow
DimsbasStringBuilder=NewStringBuilder()
DimstrOutputasString
REM--AutoGenerateTheForm
sb.Append("<formrunat=""server""id=""form2""name=""form2"">"&Chr(13)&Chr(10))
sb.Append("<tableborder=1>"&chr(13))
Fori=0ToMyDataGrid.Items.Count-1
REM--Getthecheckbox
_item=MyDataGrid.Items(i)
DimaddCheckBoxAsCheckBox=Ctype(_item.FindControl("chkAdd"),CheckBox)
DimvalidCheckBoxasCheckBox=Ctype(_item.FindControl("chkValid"),CheckBox)
IfaddCheckBox.Checkedthen
sb.Append("<tr>"&chr(13))
sb.Append("<td>"&_item.Cells(1).Text&"</td>"&chr(13))
sb.Append("<td>")
sb.Append("<asp:textboxid=""at"&_item.Cells(1).Text&"""runat=""server""/>")
createavalidatorcontrol
IfvalidCheckBox.Checkedthen
sb.Append(""&chr(13)&CreateValidator(_item.Cells(1).Text))
Endif
sb.Append("</td>"&chr(13))
sb.Append("</tr>"&chr(13))closeouttherow
Endif
Next
sb.Append("<tr>"&chr(13))closeouttherow
sb.Append("<tdcolspan=""2""><asp:buttonid=""button1""Text=""ValidateForm""runat=""Server""/></td>"&chr(13))
sb.Append("</tr>"&chr(13))closeouttherow
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
EndSub
</script>
<html>
<head>
<title></title>
</head>
<bodybgcolor="#FFFFFF">
<formrunat="server"id=form1>
Selectatablenametocreatea.NETformfor:
<asp:dropdownlistid="tblList"runat="server"/>
<asp:Buttonid=GetTableText="GetTable"runat="server"/>
<asp:panelid=myPanelrunat="server"visible="false">
<br>
SelecttheColumnsusedforgeneratingtheform.
<asp:datagridid=MyDataGridrunat="server"
BorderColor="black"
BorderWidth="1"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="False"
>
<Columns>
<asp:TemplateColumnHeaderText="Add?">
<ItemTemplate>
<center>
<asp:CheckBoxid=chkAddrunat="server"/>
</center>
</Itemtemplate>
</asp:TemplateColumn>
<asp:BoundColumnHeaderText="Name"DataField="name"/>
<asp:TemplateColumnHeaderText="CreateValidator?">
<ItemTemplate>
<center>
<asp:CheckBoxid=chkValidrunat="server"/>
</center>
</Itemtemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
<asp:Buttonid=Button1Text="CreateForm"runat="server"/>
</asp:panel>
<asp:panelid="pnlTextarea"visible="false"runat="server">
<p>CopythiscodeintoanewASP.NETpage</p>
<textareaid=taResultscols=90rows=40runat="server"/>
</asp:panel>
</form>
</body>
</html
[img=1border=0style=,1src=]http://www.ckuyun.com/[/img]
据说很厉害,甚至可以把C#也干掉^_^,不过也很复杂,本来C++已经够复杂的。有人甚至还提出把这个东东引进标准,我觉得基本上不可能的。 |
|