I’m having quite a few issues with my code formatting getting trashed by bloglines, so I’m testing using several different options from the CopySourceAsHtml Add-in for Visual Studio 2005, copying and pasting into and out of Word, and using online converters such as CodeHTMLer, and Manoli.net. Unless this kind of stuff fascinates you, you can safely ignore. (If anybody is fascinated and has suggestions, I’m open).
Test 1: CopySourceAsHtml
Options:
Wrap Words: Y
Strip Line Breaks: N
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Web.Configuration;
11
12 /// <summary>
13 /// Switches to a runtime master page defined either via
14 /// the web.config or via a page-level property for applicable pages.
15 /// </summary>
16 public class DefaultMasterPageModule : IHttpModule
17 {
18 public DefaultMasterPageModule()
19 {
20 }
21
22
23 public void Dispose()
24 {
25 }
26
27
28 public void Init(HttpApplication context)
29 {
30 context.PreRequestHandlerExecute
31 += new EventHandler(context_PreRequestHandlerExecute);
32 }
33
34
35 private void context_PreRequestHandlerExecute(object sender, EventArgs e)
36 {
37 HttpApplication application = sender as HttpApplication;
38 if (application == null) return;
39
40 System.Web.HttpContext context = application.Context;
41 if (context == null) return;
42
43 System.Web.UI.Page p = context.Handler as System.Web.UI.Page;
44 if (p == null) return;
45
46 p.PreInit += new EventHandler(Page_PreInit);
47 }
48
49
50 /// <summary>
51 /// Sets the MasterPageFile property for applicable
52 /// pages at runtime.
53 /// </summary>
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 void Page_PreInit(object sender, EventArgs e)
57 {
58 Page page = sender as Page;
59 if (page == null) return;
60
61 if (page is ISupportRuntimeMasterSwitching)
62 {
63 string runtimeMasterFile =
64 ((ISupportRuntimeMasterSwitching)page).RuntimeMasterPageFile;
65
66 if (!string.IsNullOrEmpty(runtimeMasterFile))
67 {
68 page.MasterPageFile = runtimeMasterFile;
69 return;
70 }
71 }
72
73 if (string.IsNullOrEmpty(page.MasterPageFile)
74 && MasterCanBeApplied(page))
75 {
76 PagesSection pages =
77 (PagesSection)ConfigurationManager.GetSection("system.web/pages");
78
79 if (pages.MasterPageFile != string.Empty)
80 {
81 page.MasterPageFile = pages.MasterPageFile;
82 }
83 }
84 }
85
86
87 /// <summary>
88 /// Attempts to determine whether the page is a content
89 /// page (i.e. expecting a master page template) or not.
90 /// </summary>
91 /// <param name="page"></param>
92 /// <returns></returns>
93 private bool MasterCanBeApplied(Page page)
94 {
95 if (page.HasControls())
96 {
97 if (page.Controls.IsReadOnly) return false;
98
99 foreach (Control control in page.Controls)
100 {
101 LiteralControl literal = control as LiteralControl;
102 if (literal == null ||
103 FirstNonWhiteSpaceIndex(literal.Text) >= 0)
104 {
105 return false;
106 }
107 }
108 return true;
109 }
110 return false;
111 }
112
113
114 private static int FirstNonWhiteSpaceIndex(string s)
115 {
116 for (int i=0; i < s.Length; i++)
117 {
118 if (!char.IsWhiteSpace(s[i]))
119 {
120 return i;
121 }
122 }
123 return -1;
124 }
125 }
Test 2: CopySourceAsHtml
Options:
Wrap Words: N
Strip Line Breaks: N
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Web.Configuration;
11
12 /// <summary>
13 /// Switches to a runtime master page defined either via
14 /// the web.config or via a page-level property for applicable pages.
15 /// </summary>
16 public class DefaultMasterPageModule : IHttpModule
17 { 18 public DefaultMasterPageModule()
19 { 20 }
21
22
23 public void Dispose()
24 { 25 }
26
27
28 public void Init(HttpApplication context)
29 { 30 context.PreRequestHandlerExecute
31 += new EventHandler(context_PreRequestHandlerExecute);
32 }
33
34
35 private void context_PreRequestHandlerExecute(object sender, EventArgs e)
36 { 37 HttpApplication application = sender as HttpApplication;
38 if (application == null) return;
39
40 System.Web.HttpContext context = application.Context;
41 if (context == null) return;
42
43 System.Web.UI.Page p = context.Handler as System.Web.UI.Page;
44 if (p == null) return;
45
46 p.PreInit += new EventHandler(Page_PreInit);
47 }
48
49
50 /// <summary>
51 /// Sets the MasterPageFile property for applicable
52 /// pages at runtime.
53 /// </summary>
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 void Page_PreInit(object sender, EventArgs e)
57 { 58 Page page = sender as Page;
59 if (page == null) return;
60
61 if (page is ISupportRuntimeMasterSwitching)
62 { 63 string runtimeMasterFile =
64 ((ISupportRuntimeMasterSwitching)page).RuntimeMasterPageFile;
65
66 if (!string.IsNullOrEmpty(runtimeMasterFile))
67 { 68 page.MasterPageFile = runtimeMasterFile;
69 return;
70 }
71 }
72
73 if (string.IsNullOrEmpty(page.MasterPageFile)
74 && MasterCanBeApplied(page))
75 { 76 PagesSection pages =
77 (PagesSection)ConfigurationManager.GetSection("system.web/pages"); 78
79 if (pages.MasterPageFile != string.Empty)
80 { 81 page.MasterPageFile = pages.MasterPageFile;
82 }
83 }
84 }
85
86
87 /// <summary>
88 /// Attempts to determine whether the page is a content
89 /// page (i.e. expecting a master page template) or not.
90 /// </summary>
91 /// <param name="page"></param>
92 /// <returns></returns>
93 private bool MasterCanBeApplied(Page page)
94 { 95 if (page.HasControls())
96 { 97 if (page.Controls.IsReadOnly) return false;
98
99 foreach (Control control in page.Controls)
100 { 101 LiteralControl literal = control as LiteralControl;
102 if (literal == null ||
103 FirstNonWhiteSpaceIndex(literal.Text) >= 0)
104 { 105 return false;
106 }
107 }
108 return true;
109 }
110 return false;
111 }
112
113
114 private static int FirstNonWhiteSpaceIndex(string s)
115 { 116 for (int i=0; i < s.Length; i++)
117 { 118 if (!char.IsWhiteSpace(s[i]))
119 { 120 return i;
121 }
122 }
123 return -1;
124 }
125 }
Test 3: CopySourceAsHtml
Options:
Wrap Words: N
Strip Line Breaks: Y
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Web.Configuration;
11
12 /// <summary>
13 /// Switches to a runtime master page defined either via
14 /// the web.config or via a page-level property for applicable pages.
15 /// </summary>
16 public class DefaultMasterPageModule : IHttpModule
17 { 18 public DefaultMasterPageModule()
19 { 20 }
21
22
23 public void Dispose()
24 { 25 }
26
27
28 public void Init(HttpApplication context)
29 { 30 context.PreRequestHandlerExecute
31 += new EventHandler(context_PreRequestHandlerExecute);
32 }
33
34
35 private void context_PreRequestHandlerExecute(object sender, EventArgs e)
36 { 37 HttpApplication application = sender as HttpApplication;
38 if (application == null) return;
39
40 System.Web.HttpContext context = application.Context;
41 if (context == null) return;
42
43 System.Web.UI.Page p = context.Handler as System.Web.UI.Page;
44 if (p == null) return;
45
46 p.PreInit += new EventHandler(Page_PreInit);
47 }
48
49
50 /// <summary>
51 /// Sets the MasterPageFile property for applicable
52 /// pages at runtime.
53 /// </summary>
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 void Page_PreInit(object sender, EventArgs e)
57 { 58 Page page = sender as Page;
59 if (page == null) return;
60
61 if (page is ISupportRuntimeMasterSwitching)
62 { 63 string runtimeMasterFile =
64 ((ISupportRuntimeMasterSwitching)page).RuntimeMasterPageFile;
65
66 if (!string.IsNullOrEmpty(runtimeMasterFile))
67 { 68 page.MasterPageFile = runtimeMasterFile;
69 return;
70 }
71 }
72
73 if (string.IsNullOrEmpty(page.MasterPageFile)
74 && MasterCanBeApplied(page))
75 { 76 PagesSection pages =
77 (PagesSection)ConfigurationManager.GetSection("system.web/pages"); 78
79 if (pages.MasterPageFile != string.Empty)
80 { 81 page.MasterPageFile = pages.MasterPageFile;
82 }
83 }
84 }
85
86
87 /// <summary>
88 /// Attempts to determine whether the page is a content
89 /// page (i.e. expecting a master page template) or not.
90 /// </summary>
91 /// <param name="page"></param>
92 /// <returns></returns>
93 private bool MasterCanBeApplied(Page page)
94 { 95 if (page.HasControls())
96 { 97 if (page.Controls.IsReadOnly) return false;
98
99 foreach (Control control in page.Controls)
100 { 101 LiteralControl literal = control as LiteralControl;
102 if (literal == null ||
103 FirstNonWhiteSpaceIndex(literal.Text) >= 0)
104 { 105 return false;
106 }
107 }
108 return true;
109 }
110 return false;
111 }
112
113
114 private static int FirstNonWhiteSpaceIndex(string s)
115 { 116 for (int i=0; i < s.Length; i++)
117 { 118 if (!char.IsWhiteSpace(s[i]))
119 { 120 return i;
121 }
122 }
123 return -1;
124 }
125 }
Test 4: CopySourceAsHtml
Options:
Wrap Words: Y
Strip Line Breaks: Y
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Web.Configuration;
11
12 /// <summary>
13 /// Switches to a runtime master page defined either via
14 /// the web.config or via a page-level property for applicable pages.
15 /// </summary>
16 public class DefaultMasterPageModule : IHttpModule
17 {
18 public DefaultMasterPageModule()
19 {
20 }
21
22
23 public void Dispose()
24 {
25 }
26
27
28 public void Init(HttpApplication context)
29 {
30 context.PreRequestHandlerExecute
31 += new EventHandler(context_PreRequestHandlerExecute);
32 }
33
34
35 private void context_PreRequestHandlerExecute(object sender, EventArgs e)
36 {
37 HttpApplication application = sender as HttpApplication;
38 if (application == null) return;
39
40 System.Web.HttpContext context = application.Context;
41 if (context == null) return;
42
43 System.Web.UI.Page p = context.Handler as System.Web.UI.Page;
44 if (p == null) return;
45
46 p.PreInit += new EventHandler(Page_PreInit);
47 }
48
49
50 /// <summary>
51 /// Sets the MasterPageFile property for applicable
52 /// pages at runtime.
53 /// </summary>
54 /// <param name="sender"></param>
55 /// <param name="e"></param>
56 void Page_PreInit(object sender, EventArgs e)
57 {
58 Page page = sender as Page;
59 if (page == null) return;
60
61 if (page is ISupportRuntimeMasterSwitching)
62 {
63 string runtimeMasterFile =
64 ((ISupportRuntimeMasterSwitching)page).RuntimeMasterPageFile;
65
66 if (!string.IsNullOrEmpty(runtimeMasterFile))
67 {
68 page.MasterPageFile = runtimeMasterFile;
69 return;
70 }
71 }
72
73 if (string.IsNullOrEmpty(page.MasterPageFile)
74 && MasterCanBeApplied(page))
75 {
76 PagesSection pages =
77 (PagesSection)ConfigurationManager.GetSection("system.web/pages");
78
79 if (pages.MasterPageFile != string.Empty)
80 {
81 page.MasterPageFile = pages.MasterPageFile;
82 }
83 }
84 }
85
86
87 /// <summary>
88 /// Attempts to determine whether the page is a content
89 /// page (i.e. expecting a master page template) or not.
90 /// </summary>
91 /// <param name="page"></param>
92 /// <returns></returns>
93 private bool MasterCanBeApplied(Page page)
94 {
95 if (page.HasControls())
96 {
97 if (page.Controls.IsReadOnly) return false;
98
99 foreach (Control control in page.Controls)
100 {
101 LiteralControl literal = control as LiteralControl;
102 if (literal == null ||
103 FirstNonWhiteSpaceIndex(literal.Text) >= 0)
104 {
105 return false;
106 }
107 }
108 return true;
109 }
110 return false;
111 }
112
113
114 private static int FirstNonWhiteSpaceIndex(string s)
115 {
116 for (int i=0; i < s.Length; i++)
117 {
118 if (!char.IsWhiteSpace(s[i]))
119 {
120 return i;
121 }
122 }
123 return -1;
124 }
125 }
Test 5: Word
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
/// <summary>
/// Switches to a runtime master page defined either via
/// the web.config or via a page-level property for applicable pages.
/// </summary>
public class DefaultMasterPageModule : IHttpModule
{
public DefaultMasterPageModule()
{
}