📄 aspnet09-04.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><!-- saved from url=(0076)http://msconline.maconstate.edu/tutorials/aspnet20/ASPNET09/aspnet09-04.aspx --><title>ASP.NET Tutorial</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link href="aspnet09-04_data/stylesheet.css" type="text/css" rel="stylesheet">
<style>TABLE#DEFormView TD {
FONT-SIZE: 11pt; FONT-FAMILY: times new roman
}
TABLE#DEFormView TH {
FONT-SIZE: 11pt; VERTICAL-ALIGN: top; FONT-FAMILY: times new roman; BACKGROUND-COLOR: #e0e0e0; TEXT-ALIGN: left
}
</style>
<meta content="MSHTML 6.00.2900.2180" name="GENERATOR"><style type="text/css">TABLE#AddTable TH {
FONT-SIZE: 11pt; VERTICAL-ALIGN: top; BACKGROUND-COLOR: #e0e0e0; TEXT-ALIGN: left
}
TABLE#AddTable TD {
FONT-SIZE: 11pt; VERTICAL-ALIGN: top
}
TABLE#AddTable TH {
FONT-SIZE: 11pt; VERTICAL-ALIGN: top; BACKGROUND-COLOR: #e0e0e0; TEXT-ALIGN: left
}
.buttons {
BACKGROUND-COLOR: #e0e0e0
}
.errmsg {
WIDTH: 180px
}
</style></head><body onscroll="document.all.PageScroll.value=document.body.scrollTop" onload="document.body.scrollTop=document.all.PageScroll.value">
<form id="ctl00" name="ctl00" action="aspnet09-04.aspx" method="post">
<div><input id="__VIEWSTATE" value="/wEPDwUIMzU2ODk4MzIPZBYCAgEPZBYCAggPPCsACgEADxYEHgtfIURhdGFCb3VuZGceC18hSXRlbUNvdW50ZmQWAmYPZBYGZg8PFgIeB1Zpc2libGVoZGQCAQ9kFgJmD2QWAgIFDxAPFgIfAGdkEBUGCERhdGFiYXNlCEdyYXBoaWNzCEhhcmR3YXJlCFNvZnR3YXJlB1N5c3RlbXMDV2ViFQYIRGF0YWJhc2UIR3JhcGhpY3MISGFyZHdhcmUIU29mdHdhcmUHU3lzdGVtcwNXZWIUKwMGZ2dnZ2dnZGQCAg8PFgIfAmhkZBgCBR5fX0NvbnRyb2xzUmVxdWlyZVBvc3RCYWNrS2V5X18WAQUTREVGb3JtVmlldyRCb29rU2FsZQUKREVGb3JtVmlldw8UKwAHZGQCAmRkFgBkZMnQ3lG+IhToPc1uC6aes0w4bkg/" name="__VIEWSTATE" type="hidden"> </div><input id="PageScroll" style="visibility: hidden; position: absolute;" name="PageScroll">
<div class="body">
<div class="divhead">Data Entry with a FormView</div>
<p>Sometimes it is not necessary to perform full-featured editing; it's just a
matter of performing data entry. This need crops up routinely on Web sites with
forms to fill out for taking orders, conducting surveys, registering for
memberships, and other common types of information gathering. In these cases,
the need is simply to display a data entry form and to write collected
information to a database. Although any of the information editing controls can
perform this task, the FormView is particularly attractive because of its
flexibility in formatting the data collection instrument.</p>
<p>The following FormView demonstrates a data entry form. Here it is used to
perform data entry to add records to the <span class="code">Books</span> table of
the <span class="code">BooksDB.mdb </span>database; however, it can be adapted for
use in any type of data entry situation.</p>
<p><i>Note that making actual changes to the <span class="code">BooksDB.mdb</span>
database is not permitted in these tutorials; however, all other functions work
as expected.</i></p>
<div class="page">
<h3>Book Entry</h3>
<table id="DEFormView" style="border-collapse: collapse;" border="0" cellspacing="0">
<tbody>
<tr>
<td colspan="2">
<table id="AddTable" border="0" cellpadding="1">
<tbody>
<tr>
<th>ID</th>
<td><input id="DEFormView_BookID" style="width: 70px;" maxlength="6" name="DEFormView$BookID"></td>
<td class="errmsg"><span id="DEFormView_ERRBookID" style="color: red;"></span></td></tr>
<tr>
<th>Type</th>
<td><select id="DEFormView_BookType" name="DEFormView$BookType">
<option value="Database" selected="selected">Database</option> <option value="Graphics">Graphics</option> <option value="Hardware">Hardware</option> <option value="Software">Software</option> <option value="Systems">Systems</option> <option value="Web">Web</option></select></td>
<td></td></tr>
<tr>
<th>Title</th>
<td><input id="DEFormView_BookTitle" style="width: 300px;" maxlength="50" name="DEFormView$BookTitle"></td>
<td class="errmsg"><span id="DEFormView_ERRBookTitle" style="color: red;"></span></td></tr>
<tr>
<th>Author</th>
<td><input id="DEFormView_BookAuthor" style="width: 300px;" maxlength="50" name="DEFormView$BookAuthor"></td>
<td class="errmsg"><span id="DEFormView_ERRBookAuthor" style="color: red;"></span></td></tr>
<tr>
<th>Description</th>
<td><textarea id="DEFormView_BookDescription" style="font-size: 9pt; width: 300px; font-family: Arial;" name="DEFormView$BookDescription" rows="5"></textarea></td>
<td class="errmsg"><span id="DEFormView_ERRBookDescription" style="color: red;"></span></td></tr>
<tr>
<th>Price</th>
<td><input id="DEFormView_BookPrice" style="width: 60px; text-align: right;" maxlength="6" name="DEFormView$BookPrice"></td>
<td class="errmsg"><span id="DEFormView_ERRBookPrice" style="color: red;"></span></td></tr>
<tr>
<th>Quantity</th>
<td><input id="DEFormView_BookQty" style="width: 60px; text-align: right;" maxlength="2" name="DEFormView$BookQty"></td>
<td class="errmsg"><span id="DEFormView_ERRBookQty" style="color: red;"></span></td></tr>
<tr>
<th>Sale</th>
<td><input id="DEFormView_BookSale" name="DEFormView$BookSale" type="checkbox"></td></tr>
<tr>
<th></th>
<td class="buttons"><input id="DEFormView_InsertButton" style="font-size: 9pt; width: 50px;" value="Insert" name="DEFormView$InsertButton" type="submit">
<input id="DEFormView_CancelButton" style="font-size: 9pt; width: 50px;" value="Cancel" name="DEFormView$CancelButton" type="submit">
</td></tr></tbody></table></td></tr></tbody></table><span id="AddMSG" style="color: red;"></span><br></div>
<div class="figure"><b>Figure 9-23.</b> Data entry with a FormView.</div>
<p class="head2">Setting the DefaultMode</p>
<p>A FormView normally operates in three modes. Its <span class="code"><ItemTemplate></span> configures the form for display of a data
source record, its <span class="code"><EditItemTemplate> </span>displays
edit boxes for making changes to the record, and its <span class="code"><InsertItemTemplate></span> displays blank edit boxes for
adding a new record. For presenting a data entry form, however, only the <span class="code"><InsertItemTemplate></span> is of interest. At the same time,
there is only need to display "Insert" and "Cancel" buttons to activate data
entry.</p>
<p>Code for the FormView to display a data entry form is shown below along with
its AccessDataSource controls.</p><pre class="divcode"><style type="text/css">
table#AddTable th {font-size:11pt; text-align:left; vertical-align:top;
background-color:#E0E0E0}
table#AddTable td {font-size:11pt; vertical-align:top}
table#AddTable th {font-size:11pt; text-align:left; vertical-align:top;
background-color:#E0E0E0}
.buttons {background-color:#E0E0E0}
.errmsg {width:180px}
</style>
<asp:AccessDataSource id="BookSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
InsertCommand="INSERT INTO Books (BookID, BookType, BookTitle,
BookAuthor,BookDescription, BookPrice, BookQty,
BookSale) VALUES (@BookID, @BookType, @BookTitle,
@BookAuthor, @BookDescription, @BookPrice, @BookQty,
@BookSale)"
/>
<asp:AccessDataSource id="TypeSource" Runat="Server"
DataFile="../Databases/BooksDB.mdb"
SelectCommand="SELECT DISTINCT BookType FROM Books ORDER BY BookType"/>
<h3>Book Entry</h3>
<asp:FormView id="DEFormView" DataSourceID="BookSource" Runat="Server"
<b>DefaultMode="Insert"
OnItemInserting="Validate_Insert_Data"</b>>
<InsertItemTemplate>
<table id="AddTable" border="0" cellpadding="1">
<tr>
<th>ID</th>
<td><asp:TextBox id="BookID" Text='<span class="script"><%# Bind("BookID") %></span>' Runat="Server"
Width="70" MaxLength="6"/></td>
<td class="errmsg">
<asp:Label id="ERRBookID" ForeColor="#FF0000" Runat="Server"
EnableViewState="False"/></td>
</tr>
<tr>
<th>Type</th>
<td><asp:DropDownList id="BookType" Runat="Server"
DataSourceID="TypeSource"
DataTextField="BookType"
DataValueField="BookType"
SelectedValue='<span class="script"><%# Bind("BookType") %></span>'/></td>
<td></td>
</tr>
<tr>
<th>Title</th>
<td><asp:TextBox id="BookTitle" Runat="Server"
Text='<span class="script"><%# Bind("BookTitle") %></span>'
Width="300" MaxLength="50"/></td>
<td class="errmsg">
<asp:Label id="ERRBookTitle" ForeColor="#FF0000" Runat="Server"
EnableViewState="False"/></td>
</tr>
<tr>
<th>Author</th>
<td><asp:TextBox id="BookAuthor" Runat="Server"
Text='<span class="script"><%# Bind("BookAuthor") %></span>'
Width="300" MaxLength="50"/></td>
<td class="errmsg">
<asp:Label id="ERRBookAuthor" ForeColor="#FF0000" Runat="Server"
EnableViewState="False"/></td>
</tr>
<tr>
<th>Description</th>
<td><asp:TextBox id="BookDescription" Runat="Server"
Text='<span class="script"><%# Bind("BookDescription") %></span>'
TextMode="MultiLine" Rows="5" Width="300px"
Font-Name="Arial" Font-Size="9pt"/></td>
<td class="errmsg">
<asp:Label id="ERRBookDescription" ForeColor="#FF0000" Runat="Server"
EnableViewState="False"/></td>
</tr>
<tr>
<th>Price</th>
<td><asp:TextBox id="BookPrice" Runat="Server"
Text='<span class="script"><%# Bind("BookPrice") %></span>'
Width="60" MaxLength="6" Style="text-align:right"/></td>
<td class="errmsg">
<asp:Label id="ERRBookPrice" ForeColor="#FF0000" Runat="Server"
EnableViewState="False"/></td>
</tr>
<tr>
<th>Quantity</th>
<td><asp:TextBox id="BookQty" Runat="Server"
Text='<span class="script"><%# Bind("BookQty") %></span>'
Width="60" MaxLength="2" Style="text-align:right"/></td>
<td class="errmsg">
<asp:Label id="ERRBookQty" ForeColor="#FF0000" Runat="Server"
EnableViewState="False"/></td>
</tr>
<tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -