1. Trang chủ
  2. » Công Nghệ Thông Tin

ASP.NET 2.0 Instant Results phần 10 pot

56 251 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

‘By calling the “Throw” statement, you are raising the error to the ‘global.asax file, which will use the default error handling page to ‘process/display the custom error to the user Throw End Try Return mSender End Function 4. In the App_Code folder within the Bll subfolder, open the EmailContent.vb class file. Add a new function to this class, named GetEmailSenderForResource. This function will also have the integer parameter of resourceID, and return a string value for the sender’s e-mail address. ❑ The following is the contents of GetEmailSenderForResource: ‘’’ <summary> ‘’’ Gets the email address for the sender of a file from the database ‘’’ </summary> Public Shared Function GetEmailSenderForResource(ByVal resourceID As Integer) As String Return EmailContentDB.GetEmailSenderForResource(resourceID) End Function 5. Open the Download.aspx page and press F7 to open the code-behind file, Download.aspx.cs. You can also right click the Download.aspx page and select the View Code option from the menu. Within this page, add some logic, displayed below, to send the e-mail to the original sender. This logic must occur within the Try-Catch block of the DisplayDownloadDialog function. This functionality will capture the resourceID querystring variable, and use it to query the database and extract the sender’s e-mail address. Dim resourceID As Integer = Request.QueryString(“resourceID”) Dim senderEmail As String = EmailContent.GetEmailSenderForResource( _ resourceID) Dim emailBody As String = “Your file was downloaded at “ & _ System.DateTime.Today.ToLongDateString Utilities.SendEmail(senderEmail, Config.EmailFrom, _ “Your file was downloaded”, emailBody) Now that you have implemented a simple enhancement, your understanding of the application’s archi- tecture is probably much stronger. You have seen and developed within the layered approach to the design. This will allow you to move quickly to add more functionality to the application, or write one of your own. 5 Modifying the Wrox File Share 749516 bc02.qxp 2/9/06 11:19 AM Page 5 749516 bc02.qxp 2/9/06 11:19 AM Page 6 3 Modifying the Wrox Chat Server Some possible enhancements to the Wrox Chat Server could include: ❑ Management Capabilities: The ability to see which chat sessions are used and how often they are used ❑ Automated Responses: Automatic messages to respond to a user if no administrative personnel are available to enter their chat requests. ❑ Chat Content Filters: The ability to track and alert administrative personnel when certain vulgarities or unacceptable terminology is used within chat sessions. The following steps would be required to implement automated responses: 1. Open the Web.config file, and scroll down to the appSettings section. Add two entries for the hour in the morning that the site opens and the hour that it closes, (using military hours from 0 to 24). You also will need to add an entry to specify the administrator’s email address, and the message to send out to the chat user during off-hours. So if you want to open at 8am and close at 6pm, the following four entries would be added (using military time, 8am is the number 8, and 6pm is the number 18): <configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0”> <appSettings> <add key=”HourOpen” value=”8”/> <add key=”HourClose” value=”18”/> <add key=”AdminEmail” value=”Admin@MyDomain.com”/> <add key=”ClosedMessage” value=”We are sorry, but nobody is here to assist you right now. Please try again between 8 am and 6 pm PST. For online assistance, visit our Help page.”/> 2. Expand the ContentFiles folder, then open the ChatRoom.aspx.vb WebForm’s code- behind page to view its contents. Within this file is the RaiseCallbackEvent event, where you will add a conditional message based on the time of the chatted message. The text of this function is below: 749516 bc03.qxp 2/9/06 11:19 AM Page 7 ‘’’ <summary> ‘’’ the RaiseCallbackEvent captures the content from the ‘’’ chat message from the window ‘’’ </summary> Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _ Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent If eventArgument.Length > 0 Then ChatRoom.SaveMessage(Request.QueryString(“chatRoomID”), _ eventArgument, Session(“Email”)) End If End Sub ❑ Enter the following code just below the SaveMessage method call, and before the end of the IF statement: Dim mHour As Integer = System.DateTime.Now.Hour If Config.HourOpen <= mHour And Config.HourClose >= mHour Then ChatRoom.SaveMessage(Request.QueryString(“chatRoomID”), _ Config.ClosedMessage, Config.AdminEmail) End If ❑ The entire function would be: ‘’’ <summary> ‘’’ the RaiseCallbackEvent captures the content from the ‘’’ chat message from the window ‘’’ </summary> Public Sub RaiseCallbackEvent(ByVal eventArgument As String) _ Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent If eventArgument.Length > 0 Then ChatRoom.SaveMessage(Request.QueryString(“chatRoomID”), _ eventArgument, Session(“Email”)) Dim mHour As Integer = System.DateTime.Now.Hour If Config.HourOpen <= mHour And Config.HourClose >= mHour Then ChatRoom.SaveMessage(Request.QueryString(“chatRoomID”), _ Config.ClosedMessage, Config.AdminEmail) End If End If End Sub 3. In the App_Code folder, open the config.vb class file. Add the four entries needed for the HourOpen and HourClose variables to be available to the rest of the application logic. Since the properties are public shared, they can be accessed from the class directly via dot-notation. Add the following code to this file to expose these four variable values from the Web.config file: ‘’’ <summary> ‘’’ The hour to open in the morning ‘’’ </summary> Public Shared ReadOnly Property HourOpen() As String Get Return ConfigurationManager.AppSettings(“HourOpen”).ToString() End Get 8 Chapter 3 749516 bc03.qxp 2/9/06 11:19 AM Page 8 End Property ‘’’ <summary> ‘’’ The hour to close in the evening ‘’’ </summary> Public Shared ReadOnly Property HourClose() As String Get Return ConfigurationManager.AppSettings(“HourClose”).ToString() End Get End Property ‘’’ <summary> ‘’’ The administrator’s email address ‘’’ </summary> Public Shared ReadOnly Property AdminEmail() As String Get Return ConfigurationManager.AppSettings(“AdminEmail”).ToString() End Get End Property ‘’’ <summary> ‘’’ The message to be sent back to the user ‘’’ </summary> Public Shared ReadOnly Property ClosedMessage() As String Get Return ConfigurationManager.AppSettings(“ClosedMessage”).ToString() End Get End Property 4. That’s it! Press F5 to run the application and test it out. If a chat message comes into the system that is outside of the stated work hours of 8am to 6pm, then the preconfigured chat textual message will be posted immediately following the user’s message. Now that you have constructed a basic enhancement, you should have a much better grasp of the appli- cation’s simplistic file structure and layered approach, and you can move quicker to add more functionality to the application, or write a new feature of your own. 9 Modifying the Wrox Chat Server 749516 bc03.qxp 2/9/06 11:19 AM Page 9 749516 bc03.qxp 2/9/06 11:19 AM Page 10 4 Modifying the Wrox Survey Engine Although the Wrox Survey Engine is a great starter application for utilizing ASP.NET 2.0, it likely does not contain all the features you might want to see. Some possible enhancements to the Wrox Survey Engine could include: ❑ Survey Reviews: Let users make comments on what they think about the survey. ❑ Number Counts: The ability to view the number of responses to each of the surveys from the Administration section. ❑ Charting: The ability to view a pie chart or a bar chart next to the percentages of the survey results pages. ❑ Email Invitations: An e-mail message-generation tool for sending an e-mail invitation to take the survey. ❑ Final Report: A request area to view final survey results once a specified number of responses are received. ❑ Reporting: Some prepared reports about surveys, their responses, and percentages for each response. If you want to implement number counts, the following steps would be required: 1. In Visual Web Developer or Visual Studio 2005, open the website from its extracted location at C:\inetpub\wwwroot\SurveyEngine. Open the Database Explorer by selecting the View➪Database Explorer menu selection. Find your database under the Database Connections node of the tree view. If your database is not listed, right click the Data Connections node in the tree view, select the Add Connection option, select the Microsoft SQL Server File option, and browse to the PhotoDB.mdf file in the App_Data folder of the site. This should bring up your connection within the Database Explorer window. Now that you have a valid database tree, expand it to see the Views node, and drill down to the viewResponseCountBySurvey view. Right click the view and select the Open View Definition option. 749516 bc04.qxp 2/9/06 11:44 AM Page 11 2. The query used for the view should be the following: SELECT TOP (100) PERCENT dbo.viewResponseCountBySurvey.SurveyID, dbo.viewResponseCountBySurvey.NumberResponses / dbo.viewQuestionCountBySurvey.NumQuestions AS Responses FROM dbo.viewQuestionCountBySurvey INNER JOIN dbo.viewResponseCountBySurvey ON dbo.viewQuestionCountBySurvey.SurveyID = dbo.viewResponseCountBySurvey.SurveyID ORDER BY dbo.viewResponseCountBySurvey.SurveyID ❑ The table design area, which corresponds to the SQL query, should appear similar to Figure 4-1. Figure 4-1 ❑ In this view, the count for the survey responses is returned with the SurveyID. To obtain the survey records and the count of responses, you have to add the Survey table to the view designer, joining on the SurveyID. So in chronological steps, the query can be modified to provide all of the fields you need for the survey grid, along with its number of user responses. ❑ You can modify this query by right clicking near the tables in the design area, and selecting the Add Table option. 3. On the Tables tab of the pop-up window, select the Survey table. Click OK to see the Survey table added to the designer window. 4. Then, click the ID column in the Survey table, and drag to the viewQuestionCountBySurvey table and release over the SurveyID field. The relationship of inner join between the surveyID and ID fields. Then check the box next to all of the column names (ID, Name, Description, Date, and IsCurrentSurvey). Next, uncheck the SurveyID column from the viewResponseCountBySurvey view within the same designer window. The results of this query will return the fields of the Survey table, and count the number of responses that the survey generated. Figure 4-2 displays the view design with the added Survey table. Figure 4-2 ❑ The new Transact-SQL code for the query is below: 12 Chapter 4 749516 bc04.qxp 2/9/06 11:44 AM Page 12 SELECT TOP (100) PERCENT dbo.viewResponseCountBySurvey.NumberResponses / dbo.viewQuestionCountBySurvey.NumQuestions AS Responses, dbo.Survey.Name, dbo.Survey.Description, dbo.Survey.Date, dbo.Survey.IsCurrentSurvey, dbo.Survey.ID FROM dbo.viewQuestionCountBySurvey INNER JOIN dbo.viewResponseCountBySurvey ON dbo.viewQuestionCountBySurvey.SurveyID = dbo.viewResponseCountBySurvey.SurveyID INNER JOIN dbo.Survey ON dbo.viewQuestionCountBySurvey.SurveyID = dbo.Survey.ID ORDER BY dbo.viewResponseCountBySurvey.SurveyID ❑ Running the SQL query produces the results shown in Figure 4-3. Figure 4-3 5. The resulting data provide a better view of a survey, and now you can provide this view to the user through modifying the stored procedure that is used to get the records for the user. Do this by opening the stored procedure entitled sprocSurveySelectList: ALTER PROCEDURE dbo.sprocSurveySelectList /* ‘=============================================================== ‘ NAME: sprocSurveySelectList ‘ DATE CREATED: October 5, 2005 ‘ CREATED BY: Shawn Livermore (shawnlivermore.blogspot.com) ‘ CREATED FOR: ASP.NET 2.0 - Instant Results ‘ FUNCTION: Returns a list of surveys from the database. ‘=============================================================== */ as select * from Survey ❑ By modifying this stored procedure to select from the modified view, rather than the Survey table directly, you can obtain the fields needed to display the number of responses for the surveys. 6. Change the select statement to read, Select * from viewNumberResponsesBySurvey and click Save. ❑ The new stored procedure should look exactly like the code excerpt below: ALTER PROCEDURE dbo.sprocSurveySelectList /* ‘=============================================================== ‘ NAME: sprocSurveySelectList ‘ DATE CREATED: October 5, 2005 ‘ CREATED BY: Shawn Livermore (shawnlivermore.blogspot.com) ‘ CREATED FOR: ASP.NET 2.0 - Instant Results ‘ FUNCTION: Returns a list of surveys from the database. ‘=============================================================== */ as select * from viewNumberResponsesBySurvey 13 Modifying the Wrox Survey Engine 749516 bc04.qxp 2/9/06 11:44 AM Page 13 7. Next, modify the user interface to provide the visibility to the new information in the GridView. Double-click the webform Admin.aspx to open the form in Design View. Then, right click the GridView control and select Edit Columns. In the Edit Columns screen, add a bound column, with the HeaderText value of # Columns and the DataField property of Responses. Click OK, and save the form. 8. Run the project, log in to the site, and view the Administration grid. You should see the # Responses column with the number of responses listed for each survey, as displayed in Figure 4-4. Figure 4-4 Now that you have walked through an example enhancement, there are plenty more ideas that may flow from this direction in conducting surveys online. 14 Chapter 4 749516 bc04.qxp 2/9/06 11:44 AM Page 14 [...]... 10 Now there are two SqlDataSource controls on the page The first data source control, named SqlDataSource1, is for displaying the existing image and description, and so on And the other control, named SqlDataSource2, is for displaying comments made on this particular image Scroll towards the middle of the page, after the end tag of the DataList ASP.NET data control, and add... provider that supports the Profile feature To do so, choose Website ASP.NET Configuration in VWD In the Web Site Administration Tool, click Provider Configuration and then click Select a Single Provider for all Site Management Data Make sure AspNetSqlProvider is selected and click the Test link to ensure the provider works correctly 10 Return to the Web.config file and scroll all the way to the end... support department, requesting more information or making a suggestion about a product To make it easier for returning users to fill in the form, the Contact page saves the user’s details using the new ASP.NET 2.0 Profile feature This information is then used to fill the text boxes when the Contact page is loaded again on a subsequent visit With the Customer Support Site you have seen most of the features... updated, and the author that created the article 2 Content rating Let your visitors rate the item to show their opinion about it You could collect the user’s rating with a simple user control and display the results (with a bar graph for example) with another user control 3 User feedback A common extension for a CMS-driven site like the one presented in this chapter is to allow visitors to respond on the content;... Web Developer offers to create a few required tables and procedures Click Yes to have those objects created 7 8 In the Add Table dialog, add the Content table and the PageView table you just created 9 10 Drag the Id column of the Content table onto the ContentId column of the PageView table A dialog appears that allows you to define the behavior of the relations The defaults are fine, so click OK twice... node in the Database Explorer, and select the Add New Stored Procedure option Then, enter the following for the stored procedure: CREATE PROCEDURE dbo.add_comment ( @photoID int, @description varchar (100 0), @email varchar(300) ) AS INSERT INTO comment (photoID, description, email) VALUES (@photoID, @description, @email) 9 In the Solution Explorer, navigate to the root of the site Open up the Viewphoto.aspx . bc03.qxp 2/ 9 /06 11:19 AM Page 9 749516 bc03.qxp 2/ 9 /06 11:19 AM Page 10 4 Modifying the Wrox Survey Engine Although the Wrox Survey Engine is a great starter application for utilizing ASP. NET. sprocSurveySelectList ‘ DATE CREATED: October 5, 20 05 ‘ CREATED BY: Shawn Livermore (shawnlivermore.blogspot.com) ‘ CREATED FOR: ASP. NET 2. 0 - Instant Results ‘ FUNCTION: Returns a list of surveys. sprocSurveySelectList ‘ DATE CREATED: October 5, 20 05 ‘ CREATED BY: Shawn Livermore (shawnlivermore.blogspot.com) ‘ CREATED FOR: ASP. NET 2. 0 - Instant Results ‘ FUNCTION: Returns a list of surveys

Ngày đăng: 12/08/2014, 08:22

Xem thêm: ASP.NET 2.0 Instant Results phần 10 pot

TỪ KHÓA LIÊN QUAN