Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Modifying the Code-Behind File for the Guestbook Application - ASP.NET

Chapter: Internet & World Wide Web HOW TO PROGRAM - Rich Internet Application Server Technologies - ASP.NET 2.0 and ASP.NET Ajax

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Modifying the Code-Behind File for the Guestbook Application - ASP.NET

After building the Web Form and configuring the data controls used in this example, dou-ble click the Submit and Clear buttons in Design view to create their corresponding Click event handlers in the Guestbook.aspx.vb code-behind file.

Modifying the Code-Behind File for the Guestbook Application

 

After building the Web Form and configuring the data controls used in this example, dou-ble click the Submit and Clear buttons in Design view to create their corresponding Click event handlers in the Guestbook.aspx.vb code-behind file (Fig. 25.38). The IDE generates empty event handlers, so we must add the appropriate code to make these but

14' Fig. 25.38: Guestbook.aspx.vb

 

15' Code-behind file that defines event handlers for the guestbook.

 

16Partial Class Guestbook

 

17       Inherits System.Web.UI.Page

5

1          ' Submit Button adds a new guestbook entry to the database,

 

2          ' clears the form and displays the updated list of guestbook entries

 

3          Protected Sub submitButton_Click(ByVal sender As Object, _

 

4                 ByVal e As System.EventArgs) Handles submitButton.Click

 

5                   ' create a date parameter to store the current date

 

6                   Dim currentDate As New System.Web.UI.WebControls.Parameter( _

 

7                         "Date", TypeCode.String, DateTime.Now.ToShortDateString())

13

1                   ' set the @Date parameter to the date parameter

 

2                   messagesSqlDataSource.InsertParameters.RemoveAt(0)

 

3                   messagesSqlDataSource.InsertParameters.Add(currentDate)

4              

9                   ' execute an INSERT SQL statement to add a new row to the

 

10                ' Messages table in the Guestbook database that contains the

 

11                ' current date and the user's name, e-mail address and message

 

12                messagesSqlDataSource.Insert()

22

15                ' clear the TextBoxes

 

16                nameTextBox.Text = ""

 

17                emailTextBox.Text = ""

 

18                messageTextBox.Text = ""

19          

23                ' update the GridView with the new database table contents

 

24                messagesGridView.DataBind()

 

25         End Sub ' submitButton_Click

31

1             ' Clear Button clears the Web Form's TextBoxes

 

2             Protected Sub clearButton_Click(ByVal sender As Object, _

 

3                   ByVal e As System.EventArgs) Handles clearButton.Click

 

4                   nameTextBox.Text = ""

 

5                   emailTextBox.Text = ""

 

6                   messageTextBox.Text = ""

 

7             End Sub ' clearButton_Click

 

8      End Class ' Guestbook

 

Fig. 25.38 | Code-behind file for the guestbook application.

 

tons work properly. The event handler for clearButton (lines 33–38) clears each TextBox by setting its Text property to an empty string. This resets the form for a new guestbook submission.

 

Lines 8–30 contain the event-handling code for submitButton, which adds the user’s information to the Messages table of the Guestbook database. Recall that we configured messagesSqlDataSource’s INSERT command to use the values of the TextBoxes on the Web Form as the parameter values inserted into the database. We have not yet specified the date value to be inserted, though. Lines 11–12 assign a String representation of the current date (e.g., "3/27/06") to a new object of type Parameter. This Parameter object is identified as "Date" and is given the current date as a default value. The SqlData Source’s InsertParameters collection contains an item named Date (at position 0), which we Remove in line 15 and replace in line 16 by Adding our currentDate parameter. Invoking SqlDataSource method Insert in line 21 executes the INSERT command against the database, thus adding a row to the Messages table. After the data is inserted into the database, lines 24–26 clear the TextBoxes, and line 29 invokes messagesGridView’s Data-Bind method to refresh the data that the GridView displays. This causes messagesSql-DataSource (the data source of the GridView) to execute its SELECT command to obtain the Messages table’s newly updated data.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.