code_junkie

What's the best way to load highly re-used data in a .net web application

Nov 10th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public class Global : System.Web.HttpApplication
  2. {
  3. // The key to use in the rest of the web site to retrieve the list
  4. public const string ListItemKey = "MyListItemKey";
  5. // a class to hold your actual values. This can be use with databinding
  6. public class NameValuePair
  7. {
  8. public string Name{get;set;}
  9. public string Value{get;set;}
  10. public NameValuePair(string Name, string Value)
  11. {
  12. this.Name = Name;
  13. this.Value = Value;
  14. }
  15. }
  16.  
  17. protected void Application_Start(object sender, EventArgs e)
  18. {
  19. InitializeApplicationVariables();
  20. }
  21.  
  22.  
  23. protected void InitializeApplicationVariables()
  24. {
  25. List<NameValuePair> listItems = new List<NameValuePair>();
  26. // replace the following code with your data access code and fill in the collection
  27. listItems.Add( new NameValuePair("Item1", "1"));
  28. listItems.Add( new NameValuePair("Item2", "2"));
  29. listItems.Add( new NameValuePair("Item3", "3"));
  30. // load it in the application object
  31. Application[ListItemKey] = listItems;
  32. }
  33. }
  34.  
  35. <asp:DropDownList runat="server" ID="ddList" DataTextField="Name" DataValueField="Value"></asp:DropDownList>
  36.  
  37. protected override void OnPreInit(EventArgs e)
  38. {
  39. ddList.DataSource = Application[Global.ListItemKey];
  40. ddList.DataBind();
  41. base.OnPreInit(e);
  42. }
  43.  
  44. // Create and load the profile object
  45. x_siteprofile thisprofile = new x_siteprofile(Server.MapPath(String.Concat(config.Path, "templates/")));
  46. Application.Add("SiteProfileX", thisprofile);
Add Comment
Please, Sign In to add comment