為了修改一條已經存在的數據,我們必須設置DataForm模式為UpdateRecord。然后,我們必須確定修改那一條數據,我們通過DataKeyField和DataKeyValue唯一確定一條數據,DataKeyField是數據表主鍵;DataKeyValue是一條數據的主鍵的值。
以下代碼修改數據表中第三條記錄:
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.DataForm" %>
<html>
<head><title>DataFormUpdateRecord.aspx</title></head>
<body>
<super:SqlDataForm
TableName="CustomerSurvey"
ConnectionString="Server=Localhost;UID=sa;PWD=secret;Database=Pubs"
DataKeyField="Customer_ID"
DataKeyValue="3"
Mode="UpdateRecord"
runat="Server" />
</body>
</html>
具體效果如下:
http://www.superexpertcontrols.com/dataform/samples/sample2.aspx
以上代碼設置Mode為UpdateRecord,設置DataKeyField為Customer_ID,設置DataKeyValue為3。
如果我們需要修改數據表中的所有數據,可以將DataForm模式設置為UpdateTable。在設置為修改整個表以后,會在數據表單上方生成一個導航條,通過這個導航條,我們可以瀏覽數據表中的所有數據。
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.DataForm" %>
<html>
<head><title>DataFormUpdateTable.aspx</title></head>
<body>
<super:SqlDataForm
TableName="CustomerSurvey"
ConnectionString="Server=Localhost;UID=sa;PWD=secret;Database=Pubs"
DataKeyField="Customer_ID"
Mode="UpdateTable"
runat="Server" />
</body>
</html>
具體效果如下:
http://www.superexpertcontrols.com/dataform/samples/sample3.aspx
如果我們將模式設置為Custom,我們就可以設置提交表單以后的動作。比如,以下代碼實現提交表單以后自動轉到ThankYou.aspx頁面。
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.DataForm" %>
<Script runat="Server">
Sub Form_Submit( s As Object, e As EventArgs )
myForm.Save()
Response.Redirect( "ThankYou.aspx" )
End Sub
</Script>
<html>
<head><title>DataFormCustom.aspx</title></head>
<body>
<super:SqlDataForm
id="myForm"
TableName="CustomerSurvey"
ConnectionString="Server=Localhost;UID=sa;PWD=secret;Database=Pubs"
Mode="Custom"
OnSubmit="Form_Submit"
runat="Server" />
</body>
</html>
具體效果如下:
http://www.superexpertcontrols.com/dataform/samples/sample4.aspx
。、設置DataForm樣式
DataForm有四種樣式我們可以修改:
1、HeaderStyle:定義數據表單的Header樣式;
2、LabelStyle:定義數據表單的Label樣式;
3、FieldStyle:定義數據表單的Field樣式;
4、FooterStyle:定義數據表單的Footer樣式;
DataForm還支持以下屬性設置:
GridLines:定義數據表單的線格式,包括:None、Both、Horizontal和Vertical。
Height:數據表單控件高度;
Width:數據表單控件寬度;
BoderStyle:數據表單邊框格式;
BorderWidth:數據表單邊框寬度;
BorderColor:數據表單邊框顏色;
CellPadding:數據表單控件大;
CellSpacing:數據表單控件間間距;
我們現在看一個舉例:
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.DataForm" %>
<html>
<head><title>DataFormStyle.aspx</title></head>
<body>
<super:SqlDataForm
HeaderStyle-backColor="Salmon"
FieldStyle-backColor="yellow"
LabelStyle-Font-Name="Script"
LabelStyle-Font-Size="28pt"
FooterStyle-backColor="blue"
Cellpadding="10"
Cellspacing="0"
GridLines="Both"
BorderStyle="Dashed"
BorderColor="red"
BorerWidth="10px"
LabelStyle-BackColor="lightgreen"
TableName="CustomerSurvey"
ConnectionString="Server=Localhost;UID=sa;PWD=secret;Database=Pubs"
runat="Server" />
</body>
</html>
具體效果如下:
http://www.superexpertcontrols.com/dataform/samples/sample5.aspx
。、自定義布局的DataForm
我們也可以自己增加控件設計數據表單布局,可以增加的控件如下:
● DataTextBox
● DataDropDownList
● DataRadioButton
● DataCheckbox
● DataListBox
● DataRadioButtonList
● DataLabel
每一個控件都擴展了標準控件的功能。這些控件都有兩個屬性:DataField和DataType。DataField讓控件和數據庫的具體字段聯系起來,DataType定義輸入數據的類型。以下是一個增加數據的舉例:
<%@ Register TagPrefix="Super" Namespace="Superexpert.Data"
Assembly="Superexpert.DataForm" %>
<Script runat="Server">
Sub Form_Submit( s As Object, e As EventArgs )
myForm.Save()
Response.Redirect( "ThankYou.aspx" )
End Sub
</Script>
<html>
<head><title>DataFormCustomLayout.aspx</title></head>
<body>
<super:SqlDataForm
id="myForm"
TableName="CustomerSurvey"
ConnectionString="Server=Localhost;UID=sa;PWD=secret;Database=Pubs"
runat="Server">
Customer Name:
<br>
<super:DataTextBox
DataField="Customer"
Runat="Server" />
<p>
Age:
<br>
<super:DataTextBox
DataField="Age"
Runat="Server" />
<p>
Birthdate:
<br>
<super:DataTextBox
DataField="Birthdate"
Runat="Server" />
<p>
<asp:Button
Text="Add New Customer!"
OnClick="Form_Submit"
Runat="Server" />
<p>
</super:SqlDataForm>
</body>
</html>
具體效果請看:
http://www.superexpertcontrols.com/dataform/samples/sample6.aspx
文章來源于領測軟件測試網 http://www.kjueaiud.com/