⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspnet09-04.htm

📁 tutorialssss.... for everybody!!!!!!!!!!!!!!!!!!!
💻 HTM
📖 第 1 页 / 共 2 页
字号:
    <th>Sale</th>
    &lt;td&gt;&lt;asp:CheckBox id="BookSale" Checked='<span class="script">&lt;%# Bind("BookSale") %&gt;</span>' 
          Runat="Server"/&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;th&gt;&lt;/th&gt;
    &lt;td class="buttons"&gt;
      &lt;asp:Button id="InsertButton" CommandName="Insert" Text="Insert" 
        Font-Size="9pt" Width="50" Runat="Server"/&gt;
      &lt;asp:Button id="CancelButton" CommandName="Cancel" Text="Cancel" 
        Font-Size="9pt" Width="50" Runat="Server"/&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/InsertItemTemplate&gt;
  
&lt;/asp:FormView&gt;

&lt;asp:Label id="AddMSG" ForeColor="#FF0000" EnableViewState="False" 
Runat="Server"/&gt;
</pre>
<div class="listing"><b>Listing 9-33.</b> Coding a FormView for data entry.</div>
<p>Normally, a FormView opens in <span class="code"><b>ReadOnly</b></span> mode to 
display the first of the selected records from its AccessDataSource in its <span class="code">&lt;ItemTemplate&gt;</span>. In this case, however, there is no set 
of selected records for display since the AccessDataSource does not have a <span class="code">SelectCommand</span> property. Neither does the FormView have an 
<span class="code">&lt;ItemTemplate&gt;</span>. With no data to bind to the form 
and with no way to display it, the FormView simply would not appear on the 
page.</p>
<p>The need is to open the FormView in its <span class="code"><b>Insert</b></span> 
mode rather than its default "ReadOnly" mode. This is accomplished by adding the 
<span class="code"><b>DefaultMode</b>="Insert"</span> property to the FormView as 
is done in the above example. Now, the form is opened as a set of input areas as 
it would appear when clicking the standard FormView's "New" button and switching 
to insert mode. Also, if necessary, you can open the FormView in <span class="code"><b>Edit</b></span> mode by setting its <span class="code">DefaultMode="Edit"</span> property, assuming you also have a <span class="code">SelectCommand</span> statement that retrieves an initial record.</p>
<p>Since the FormView opens in <span class="code">Insert</span> mode, it is 
necessary to provide appropriate command buttons to activate the form. Buttons 
with <span class="code">CommandName="Insert"</span> and <span class="code">CommandName="Cancel"</span> are provided. An <span class="code">OnItemInserting</span> event handler calls a subprogram when the 
ItemInserting event occur to validate input values.</p>
<p>Notice that all input areas have <span class="code">&lt;#% Bind(<i>field</i>) 
%&gt;</span> binding expressions to link with the parameters of the <span class="code">InsertCommand</span>. These expressions do not bind "forward" from a 
data source to an insert box for display; they bind "backwards" from the input 
areas to the <span class="code">INSERT</span> statement for inserting entered 
values.</p>
<p>Each input area has an associated message display Label to its right. These 
separate message areas make it possible to display separate data entry errors. 
Also, there is a message Label at the end of the FormView to report success or 
failure in adding a record to the database.</p>
<p class="head2">Scripting the FormView</p>
<p>Most of the scripting effort is to validate entered data to ensure its 
correctness and reasonableness prior to writing the record to the database. Much 
of the code is adapted from previous scripts.</p><pre class="divscript">&lt;%@ Import Namespace="System.Data.OleDb" %&gt;

&lt;SCRIPT Runat="Server"&gt;

Sub Validate_Insert_Data (Src As Object, Args As FormViewInsertEventArgs)
  
  Dim MSGLabel As Label
  
  If Args.Values("BookID") = "" Then
    Args.Cancel = True
    MSGLabel = DEFormView.FindControl("ERRBookID")
    MSGLabel.Text = "&amp;bull; Missing BookID"
  End If
  
  Dim DBConnection As OleDbConnection
  Dim DBCommand As OleDbCommand
  Dim SQLString As String
  
  DBConnection = New OleDbConnection( _
    "Provider=Microsoft.Jet.OLEDB.4.0;" &amp; _
    "Data Source=" &amp; Server.MapPath("../Databases/BooksDB.mdb"))
  DBConnection.Open()
  SQLString = "SELECT Count(*) FROM Books " &amp; _
              "WHERE BookID = '" &amp; Args.Values("BookID") &amp; "'"
  DBCommand = New OleDbCommand(SQLString, DBConnection)
  If DBCommand.ExecuteScalar() &lt;&gt; 0 Then
    MSGLabel = DEFormView.FindControl("ERRBookID")
    MSGLabel.Text = "&amp;bull; Duplicate BookID"
    Args.Cancel = True
  End If
  DBConnection.Close()
  
  If Args.Values("BookTitle") = "" Then
    Args.Cancel = True
    MSGLabel = DEFormView.FindControl("ERRBookTitle")
    MSGLabel.Text = "&amp;bull; Missing title"
  End If
  
  If Args.Values("BookAuthor") = "" Then
    Args.Cancel = True
    MSGLabel = DEFormView.FindControl("ERRBookAuthor")
    MSGLabel.Text = "&amp;bull; Missing author"
  End If
  
  If Args.Values("BookDescription") = "" Then
    Args.Cancel = True
    MSGLabel = DEFormView.FindControl("ERRBookDescription")
    MSGLabel.Text = "&amp;bull; Missing description"
  End If	
  
  If Not IsNumeric(Args.Values("BookPrice")) Then
    Args.Cancel = True
    MSGLabel = DEFormView.FindControl("ERRBookPrice")
    MSGLabel.Text = "&amp;bull; Price not numeric"
  Else
    If Args.Values("BookPrice") &lt; 0 _
    OR Args.Values("BookPrice") &gt; 200 Then
      Args.Cancel = True
      MSGLabel = DEFormView.FindControl("ERRBookPrice")
      MSGLabel.Text = "&amp;bull; Price out of range"
    End If
  End If
  
  If Not IsNumeric(Args.Values("BookQty")) Then
    Args.Cancel = True
    MSGLabel = DEFormView.FindControl("ERRBookQty")
    MSGLabel.Text = "&amp;bull; Qty not numeric"
  Else
    If Args.Values("BookQty") &lt; 0 Then
      Args.Cancel = True
      MSGLabel = DEFormView.FindControl("ERRBookQty")
      MSGLabel.Text = "&amp;bull; Qty out of range"
    End If
  End If
  
  If Args.Cancel = True Then
    AddMSG.Text = "Record " &amp; Args.Values("BookID") &amp; " Not Added"
  Else
    AddMSG.Text = "Record " &amp; Args.Values("BookID") &amp; " Added"
  End If
  
End Sub

&lt;/SCRIPT&gt;
</pre>
<div class="listing"><b>Listing 9-34.</b> Scripting a FormView used for data 
validation.</div>
<p>When the "Insert" button is clicked and an Inserting event occurs, the <span class="code">Validate_Insert_Data</span> subprogram is called. This subprogram 
validates submitted data for all except the DropDownList and CheckBox fields. A 
typical check is shown below for the <span class="code">BookID</span> TextBox.</p><pre class="divscript">Dim MSGLabel As Label

If Args.Values("BookID") = "" Then
  Args.Cancel = True
  MSGLabel = DEFormView.FindControl("ERRBookID")
  MSGLabel.Text = "-- Missing BookID"
End If

...
</pre>
<div class="listing"><b>Listing 9-35.</b> Scripting a FormView to validate a data 
entry field.</div>
<p>In this case, the <span class="code">BookID</span> textbox is tested for an 
entry. If it is missing, then the inserting event is cancelled (<span class="code">Args.Cancel="True"</span>) and an error message is displayed in the 
associated Label.</p>
<p>Since message labels appear inside the FormView, they must be "found" prior 
to their use. Here, the Label with <span class="code">id="ERRBookID"</span> is 
located with <span class="code">DEFormView.FindControl("ERRBookID")</span> and 
assigned to a script-generated Label (<span class="code">MSGLabel</span>) for 
assignment of the <span class="code">"Missing BookID" </span>message.</p>
<p>Since there are six different message areas, there is the need to find all 
six and to assign them to Labels. It is not necessary, however, to create six 
different scripted Labels. All <span class="code">FindControl()</span> methods can 
assign to the same shared control as is done here by declaring <span class="code">MSGLabel</span> as the shared global Label.</p>
<p>It should be meantioned that the FormView's <span class="code">FindControl()</span> method cannot be applied in this same format to 
GridView or DetailsView controls. The reason is that the FormView has only one 
"row" to search for a control. These other controls have multiple rows whose 
index numbers must be used to identify on which row to look for a control. These 
<span class="code">FindControl()</span> differences are pointed out in later 
tutorials.</p>
<p>A test of made of the <span class="code">BookID</span> entry to avoid writing a 
duplicate record to the database. This test involves connecting to the database 
and returning a <span class="code">Count(*)</span> of the number of records with a 
<span class="code">BookID</span> matching the entered value. If the count is <span class="code">0</span>, then there are no matching records and the new record can 
be added. If the count is not <span class="code">0</span>, then a matching record 
was found and a duplicate-record message is displayed. </p><pre class="divscript">DBConnection = New OleDbConnection( _
  "Provider=Microsoft.Jet.OLEDB.4.0;" &amp; _
  "Data Source=" &amp; Server.MapPath("../Databases/BooksDB.mdb"))
DBConnection.Open()
SQLString = "SELECT Count(*) FROM Books " &amp; _
            "WHERE BookID = '" &amp; Args.Values("BookID") &amp; "'"
DBCommand = New OleDbCommand(SQLString, DBConnection)
If DBCommand.ExecuteScalar() &lt;&gt; 0 Then
  MSGLabel = DEFormView.FindControl("ERRBookID")
  MSGLabel.Text = "&amp;bull; Duplicate BookID"
  Args.Cancel = True
End If
DBConnection.Close()
</pre>
<div class="listing"><b>Listing 9-36.</b> Testing for a duplicate <span class="code">BookID</span>.</div>
<p>Whether or not the entered information is added to the database, a message is 
displayed indicating the results. This message area is located outside the 
FormView, so it can be referenced directly to display the message.</p><pre class="divscript">If Args.Cancel = True Then
  AddMSG.Text = "Record " &amp; Args.Values("BookID") &amp; " Not Added"
Else
  AddMSG.Text = "Record " &amp; Args.Values("BookID") &amp; " Added"
End If
</pre>
<div class="listing"><b>Listing 9-37.</b> Displaying a record-addition results 
message.</div><br><br></div>
<div><input id="__EVENTVALIDATION" value="/wEWEQL3lJaQBgLbhM+CAgKex86fBwLwmYrvBwLUr56oDwKi36nYBwKbxJkVAqGFup0GAoSZkeIGApCRpb4NApTih/IGApixpucGAt6w8M4CAs7HysMBApXe8MYEAqjmsIMEAs/X140GXvnUVOcK3caZEPVeCULGZToJY1Y=" name="__EVENTVALIDATION" type="hidden"> </div></form></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -