| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Finally, you can manage your Google Docs, uploads, and email attachments (plus Dropbox and Slack files) in one convenient place. Claim a free account, and in less than 2 minutes, Dokkio (from the makers of PBworks) can automatically organize your content for you.

View
 

Guestbook Code from Deitel (Before Editing)

Page history last edited by PBworks 9 years, 11 months ago

Guestbook ASP Code

 

<%-- Fig. 25.37 Guestbook.aspx --%>
<%-- Guestbook Web application with a form for users to submit --%>
<%-- guestbook entries and a GridView to view existing entries. --%>
<%@ Page Language="VB" AutoEventWireup="false" 
   CodeFile="Guestbook.aspx.vb" Inherits="Guestbook" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Guestbook</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h2>
          Please leave a message in our guestbook:</h2>
       <table>
          <tr>
             <td style="width: 130px; height: 21px" valign="top">
                Your name:<br />
             </td>
             <td style="width: 300px; height: 21px" valign="top">
                <asp:textbox ID="nameTextBox" runat="server" 
                   Width="300px"></asp:textbox>
             </td>
          </tr>
          <tr>
             <td style="width: 130px" valign="top">
                Your e-mail address:<br />
             </td>
             <td style="width: 300px" valign="top">
                <asp:textbox ID="emailTextBox" runat="server" 
                   Width="300px"></asp:textbox>
             </td>
          </tr>
          <tr>
             <td style="width: 130px" valign="top">
                Tell the world:<br />
             </td>
             <td style="width: 300px" valign="top">
                <asp:textbox ID="messageTextBox" runat="server" 
                   Height="100px" Rows="8" Width="300px">
                  </asp:textbox>
             </td>
          </tr>
          <tr>
             <td style="width: 130px" valign="top">
             </td>
             <td style="width: 300px" valign="top">
                <asp:button ID="submitButton" runat="server" 
                   Text="Submit" />
                <asp:button ID="clearButton" runat="server" 
                   Text="Clear" />
             </td>
          </tr>
       </table>
       <br />
       <asp:gridview ID="messagesGridView" runat="server" 
          AutoGenerateColumns="False" CellPadding="4"
          DataKeyNames="MessageID" DataSourceID="messagesSqlDataSource" 
          ForeColor="#333333" GridLines="None" Width="600px">
          <FooterStyle BackColor="#1C5E55" Font-Bold="True" 
             ForeColor="White" />
          <Columns>
             <asp:BoundField DataField="Date" HeaderText="Date" 
                SortExpression="Date" />
             <asp:BoundField DataField="Name" HeaderText="Name" 
                SortExpression="Name" />
             <asp:BoundField DataField="Email" HeaderText="Email" 
                SortExpression="Email" />
             <asp:BoundField DataField="Message" HeaderText="Message" 
                SortExpression="Message" />
          </Columns>
          <RowStyle BackColor="#E3EAEB" />
          <EditRowStyle BackColor="#7C6F57" />
          <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" 
             ForeColor="#333333" />
          <PagerStyle BackColor="#666666" ForeColor="White" 
             HorizontalAlign="Center" />
          <HeaderStyle BackColor="#1C5E55" Font-Bold="True" 
             ForeColor="White" />
          <AlternatingRowStyle BackColor="White" />
       </asp:gridview>
       <asp:sqldatasource ID="messagesSqlDataSource" runat="server" 
          ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
          DeleteCommand="DELETE FROM [Messages] WHERE [MessageID] = 
             @MessageID" InsertCommand="INSERT INTO [Messages] 
             ([Date], [Name], [Email], [Message]) 
             VALUES (@Date, @Name, @Email, @Message)"
          SelectCommand="SELECT * FROM [Messages]" UpdateCommand=
             "UPDATE [Messages] SET [Date] = @Date, [Name] = @Name, 
             [Email] = @Email, [Message] = @Message 
             WHERE [MessageID] = @MessageID">
          <DeleteParameters>
             <asp:Parameter Name="MessageID" Type="Int32" />
          </DeleteParameters>
          <UpdateParameters>
             <asp:Parameter Name="Date" Type="String" />
             <asp:Parameter Name="Name" Type="String" />
             <asp:Parameter Name="Email" Type="String" />
             <asp:Parameter Name="Message" Type="String" />
             <asp:Parameter Name="MessageID" Type="Int32" />
          </UpdateParameters>
          <InsertParameters>
             <asp:Parameter Name="Date" Type="String" />
             <asp:ControlParameter ControlID="nameTextBox" Name="Name" 
                PropertyName="Text" Type="String" />
             <asp:ControlParameter ControlID="emailTextBox" Name="Email" 
                PropertyName="Text" Type="String" />
             <asp:ControlParameter ControlID="messageTextBox" 
                Name="Message" PropertyName="Text" Type="String" />
          </InsertParameters>
       </asp:sqldatasource>
    </div>
    </form>
</body>
</html>

Guestbook Code-behind

 

 

' Fig. 25.38: Guestbook.aspx.vb
' Code-behind file that defines event handlers for the guestbook.
Partial Class Guestbook
    Inherits System.Web.UI.Page

    ' Submit Button adds a new guestbook entry to the database,
    ' clears the form and displays the updated list of guestbook entries
    Protected Sub submitButton_Click(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles submitButton.Click
        ' create a date parameter to store the current date
        Dim currentDate As New System.Web.UI.WebControls.Parameter( _
           "Date", TypeCode.String, DateTime.Now.ToShortDateString())

        ' set the @Date parameter to the date parameter
        messagesSqlDataSource.InsertParameters.RemoveAt(0)
        messagesSqlDataSource.InsertParameters.Add(currentDate)

        ' execute an INSERT SQL statement to add a new row to the 
        ' Messages table in the Guestbook database that contains the
        ' current date and the user's name, e-mail address and message
        messagesSqlDataSource.Insert()

        ' clear the TextBoxes
        nameTextBox.Text = ""
        emailTextBox.Text = ""
        messageTextBox.Text = ""

        ' update the GridView with the new database table contents
        messagesGridView.DataBind()
    End Sub ' submitButton_Click

    ' Clear Button clears the Web Form's TextBoxes
    Protected Sub clearButton_Click(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles clearButton.Click
        nameTextBox.Text = ""
        emailTextBox.Text = ""
        messageTextBox.Text = ""
    End Sub ' clearButton_Click
End Class ' Guestbook


Comments (0)

You don't have permission to comment on this page.