Tag Archives: Michael Palermo
Video: Microsoft’s Michael Palermo Reveals Windows 8
In a moment of weakness, Microsoft Developer Evangelist J. Michael Palermo IV (former MVP & RD) shares intimate details about Windows 8, the latest operating system from Microsoft, thanks to a grueling interview by Spike Xavier at Interface's Tech Immersion … Continue Reading
Interface Tech Immersion 2011
Our DEEP DIVE Tech Immersion 2011 was a great success! A highlight was the keynote by Microsoft Distinguished Engineer Jeffrey Snover, the father of PowerShell. Snover cleared up some of the confusion about the cloud, which we'll be hearing more … Continue Reading
Managing Projects in Visual Studio 2005 & 2008 by Michael Palermo – Interface Technical Training
One of the first tasks I tried when I installed Visual Studio 2008 was to open a Visual Studio 2005 project. I was prompted with the following: This concerned me, because I wanted to open the project in Visual Studio 2005 … Continue Reading
How To: Obtain Method Name Programmatically For Tracing – Michael Palermo
I am not a fan of hard-coding method names in exception or trace messages. Here is a utility method to allow access to method name at runtime:
|
1 2 3 4 5 |
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> TraceContext(<span style="color: #0000ff;">string</span> messageFormat)
{
<span style="color: #2b91af;">Trace</span>.WriteLine(<span style="color: #0000ff;">string</span>.Format(messageFormat,
<span style="color: #0000ff;">new</span> System.Diagnostics.<span style="color: #2b91af;">StackFrame</span>(1).GetMethod().Name));
} |
If I call the method above from inside another method:
|
1 2 3 4 |
<span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span> Application_AuthorizeRequest(<span style="color: #0000ff;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)
{
<span style="color: #2b91af;">Tools</span>.TraceContext(<span style="color: #a31515;">"Inside of {0} event handler"</span>);
} |
The … Continue Reading
Recursive FindControl by Michael Palermo
Need to find a control anywhere on the page or in a template? Here is the code to find it recursively. This example assumes the code is located in a custom base page.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<span style="color: teal;"> 1</span> <span style="color: blue;">public</span> T FindControl<T>(<span style="color: blue;">string</span> id) where T : Control
<span style="color: teal;"> 2</span> {
<span style="color: teal;"> 3</span> <span style="color: blue;">return</span> FindControl<T>(Page, id);
<span style="color: teal;"> 4</span> }
<span style="color: teal;"> 5</span>
<span style="color: teal;"> 6</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> T FindControl<T>(Control startingControl, <span style="color: blue;">string</span> id) where T : Control
<span style="color: teal;"> 7</span> {
<span style="color: teal;"> 8</span> <span style="color: green;">// this is null by default</span>
<span style="color: teal;"> 9</span> T found = <span style="color: blue;">default</span>(T);
<span style="color: teal;"> 10</span>
<span style="color: teal;"> 11</span> <span style="color: blue;">int</span> controlCount = startingControl.Controls.Count;
<span style="color: teal;"> 12</span>
<span style="color: teal;"> 13</span> <span style="color: blue;">if</span> (controlCount > <span style="color: maroon;">0</span>)
<span style="color: teal;"> 14</span> {
<span style="color: teal;"> 15</span> <span style="color: blue;">for</span> (<span style="color: blue;">int</span> i = <span style="color: maroon;">0</span>; i < controlCount; i++)
<span style="color: teal;"> 16</span> {
<span style="color: teal;"> 17</span> Control activeControl = startingControl.Controls[i];
<span style="color: teal;"> 18</span> <span style="color: blue;">if</span> (activeControl <span style="color: blue;">is</span> T)
<span style="color: teal;"> 19</span> {
<span style="color: teal;"> 20</span> found = startingControl.Controls[i] <span style="color: blue;">as</span> T;
<span style="color: teal;"> 21</span> <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.Compare(id, found.ID, <span style="color: maroon;">true</span>) == <span style="color: maroon;">0</span>) <span style="color: blue;">break</span>;
<span style="color: teal;"> 22</span> <span style="color: blue;">else</span> found = <span style="color: blue;">null</span>;
<span style="color: teal;"> 23</span> }
<span style="color: teal;"> 24</span> <span style="color: blue;">else</span>
<span style="color: teal;"> 25</span> {
<span style="color: teal;"> 26</span> found = FindControl<T>(activeControl, id);
<span style="color: teal;"> 27</span> <span style="color: blue;">if</span> (found != <span style="color: blue;">null</span>) <span style="color: blue;">break</span>;
<span style="color: teal;"> 28</span> }
<span style="color: teal;"> 29</span> }
<span style="color: teal;"> 30</span> }
<span style="color: teal;"> 31</span> <span style="color: blue;">return</span> found;
<span style="color: teal;"> 32</span> } |
