In this article I have covered a few topics like,
- Intializing the properties from.
i). web.config file
ii).session variables
- Readonly properties.
- Readonly & Write properties.
In this example we have two properties.
[Readonly properties]ResultsForPage - it is a readonly property. This means we can take the value from the property, but we can't re-asign the value to the property, we have intialized this property by using session variable.
[Readonly & Write properties]PriceMode - it is a read and write property. we have intialized this property through web.config file.
web.Config Code...
In order to access the values from web.config file. we should intialize the values to any variable in <appSettings> tag.
- <?xml version="1.0"?>
- <!--
-
- For more information on how to configure your ASP.NET application, please visit
-
- http:
-
- -->
- <configuration>
- <system.web>
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" /> </system.web>
- <appSettings>
- <add key="Constr" value="server=10101010101;UID=sa;PWD=mypassword;DATABASE=EMPLOYEE;" />
- <add key="PriceMode" value="INR" />
- <add key="isValidCustomer" value="false" /> </appSettings>
- </configuration>
webform.aspx.cs Code...
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class _Default: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- Session["PageCount"] = 30;
- int resultcount = StaticData.ResultsForPage;
-
-
- string price_mode = StaticData.PriceMode;
- StaticData.PriceMode = "USD";
- }
- }
C# Class file Code...
- using Microsoft.VisualBasic;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Diagnostics;
- using System.Web.Configuration;
- using System.Web.UI;
- using System.Web;
- public class StaticData {
- public static string Price_Mode = "";
- private readonly static int ResultsFor_Page = Convert.ToInt32(HttpContext.Current.Session["PageCount"]);
- public static int ResultsForPage {
- get {
- return ResultsFor_Page;
- }
- }
-
-
-
-
-
- private static string Price_mode = System.Web.Configuration.WebConfigurationManager.AppSettings["PriceMode"].ToString();
- public static string PriceMode {
- get {
- return Price_mode;
- }
- set {
- Price_mode = value;
- }
- }
- }