Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ASP.NET: Creating an Application Configuration Class : Page 4

Most of you are probably aware that the web.config file in an ASP.NET project controls the behavior of your Web site. If you make a change to one of the built-in settings in this file, ASP.NET automatically detects those changes and applies them immediately. Wouldn't it be nice if you could have your own settings in this file applied immediately as well? In this article you will learn how to do just that. You will also learn the difference between the Application object and creating your own Configuration class.


advertisement
Testing the Application
Now that you have created your class and set key values into your web.config file, it is time to test the application. Follow the steps below to create a user interface to display the values from the web.config file.

  1. Display Webform1.aspx in design mode.
  2. Set the pageLayout property to FlowLayout.
  3. Select Table, Insert, Table from the Visual Studio .NET menu.
  4. Create a table with 2 rows and 2 columns.
  5. Click OK.
  6. Click into the first cell of the first row in the table and double-click on the Label control in the toolbox.
  7. Set the Text property of this control to Site Name.
  8. Click into the second cell of the first row in the table and double-click on the Label control in the toolbox.
  9. Set the Name property to lblSiteName.
  10. Set the Text property of this control to an empty value.
  11. Click into the first cell of the second row in the table and double-click on the Label control in the toolbox.
  12. Set the Text property of this control to Connect String.
  13. Click into the second cell of the second row in the table and double-click on the Label control in the toolbox.
  14. Set the Name property to lblConnect.
  15. Set the Text property of this control to an empty value.
  16. Double click the form to display the Page_Load event procedure.
  17. Add the following code to the page.
You can test your AppConfig class by retrieving the properties and placing them into labels.

Private Sub Page_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load lblSiteName.Text = AppConfig.SiteName lblConnect.Text = AppConfig.ConnectString End Sub

Run the application and you should see the values you typed into the web.config file appear in the labels on the form.

With the application still running, modify the values in the web.config file and click the Refresh button on your browser. After a couple of refreshes the values should automatically refresh with the new values.

Creating a configuration class has a lot of benefits when compared to just using the Application object by itself. The ability to have an IntelliSense listing of properties, automatic reload of values when the web.config file changes and having typed properties are just some of the advantages. While using this method requires a little more setup work, the payoff is well worth the effort.


Paul D. Sheriff is the President of PDSA, Inc., a Microsoft Managed Partner in Southern California. Paul is the MSDN Regional Director for Southern California, and has an upcoming Addison-Wesley book on ASP.NET, co-written with Ken Getz. Visit the PDSA, Inc. Web site at www.pdsa.com for more information. PSheriff@pdsa.com.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date