<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.iis.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:cs="http://blogs.iis.net/"><channel><title>Search results matching tag 'Code'</title><link>http://blogs.iis.net/search/SearchResults.aspx?o=DateDescending&amp;tag=Code&amp;orTags=0</link><description>Search results matching tag 'Code'</description><dc:language>en-US</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>How to Add a Locked Header Row to an ASP.NET GridView Control</title><link>http://blogs.iis.net/webtopics/archive/2009/06/23/how-to-add-a-locked-header-row-to-an-asp-net-gridview-control.aspx</link><pubDate>Wed, 24 Jun 2009 00:22:00 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:3254129</guid><dc:creator>Anonymous</dc:creator><cs:applicationKey>webtopics</cs:applicationKey><description>&lt;P&gt;The GridView control is often used to display tabular data, much like an Excel spreadsheet. However, unlike Excel, the GridView control doesn't have any automatic way of locking the header row so that it doesn't scroll out of view. &lt;/P&gt;
&lt;P&gt;Check out &lt;A href="http://www.jimcobooks.com/lockedgrid/unlocked.aspx" target=_blank mce_href="http://www.jimcobooks.com/lockedgrid/unlocked.aspx"&gt;this example&lt;/A&gt; of a GridView within a DIV with the overflow-Y property set to &lt;EM&gt;scroll&lt;/EM&gt;. Notice that as you scroll the GridView, the header row scrolls out of view. It would be more convenient to have a locked header row so that the header row is always visible regardless of the scoll position of the GridView. It would also be nice to have a header row that dynamically configured itself as per the data source so that if we add a new field to the GridView, a corresponding column automatically gets added to the locked header row. &lt;/P&gt;
&lt;P&gt;Here's &lt;A href="http://www.jimcobooks.com/lockedgrid/locked.aspx" target=_blank mce_href="http://www.jimcobooks.com/lockedgrid/locked.aspx"&gt;another example&lt;/A&gt;, but this time, the header row stays locked at the top of the GridView so that you can scroll without losing track of column names. There are a few steps required to implement this functionality.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Create a DIV on the page to contain the header row and set the DIV (either statically or dynamically) to the same width as the DIV that contains the GridView.&lt;/LI&gt;
&lt;LI&gt;Insert an ASP.NET Table control into the header DIV.&lt;/LI&gt;
&lt;LI&gt;In Page_Init, dynamically add the necessary cells to the header table and set the appropriate properties so that it matches the appearance of the rest of the GridView.&lt;/LI&gt;
&lt;LI&gt;Check for sortable columns and dynamically add sorting LinkButtons to the header row.&lt;/LI&gt;
&lt;LI&gt;Add code to handle the LinkButton's Click event and sort appropriately.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Let's go through each of these steps. (If you'd prefer to work with a working project, you can download the project at the end of this post.)&lt;/P&gt;
&lt;H1&gt;Creating the Header DIV and Inserting an ASP.NET Table Control (Steps 1 and 2)&lt;/H1&gt;
&lt;P&gt;To create a locked header row, we need to disable the header for the GridView control and instead dynamically create our own header. &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Select the GridView control and change the &lt;STRONG&gt;ShowHeader&lt;/STRONG&gt; property to &lt;EM&gt;false&lt;/EM&gt;.&lt;/LI&gt;
&lt;LI&gt;Create a new DIV above the DIV containing the GridView control using the following code.&lt;/LI&gt;&lt;/OL&gt;
&lt;P class=code&gt;&amp;lt;!-- *** Begin Header Table *** --&amp;gt;&lt;BR&gt;&amp;lt;div id="DivHeader" style="vertical-align:top;width:650px;"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Table ID="HeaderTable" runat="server" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CellPadding="4" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CellSpacing="0" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Font-Size="11pt" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BackColor="#274511" &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ForeColor="White"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:Table&amp;gt;&lt;BR&gt;&amp;lt;/div&amp;gt;&lt;BR&gt;&amp;lt;!-- *** End Header Table *** --&amp;gt;&lt;/P&gt;
&lt;P&gt;Note that the properties for my Table control were derived from the theme applied to my sample page. You can set these properties as you wish or dynamically set them so that they adjust to the GridView's properties automatically.&lt;/P&gt;
&lt;P&gt;Notice that this Table control doesn't contain any cells. We'll add the necessary cells in Page_Init in the next step.&lt;/P&gt;
&lt;H1&gt;Add Code to Page_Init to Dynamically Create Header Cells and LinkButtons (Steps 3 and 4)&lt;/H1&gt;
&lt;P&gt;To dynamically add the cells for the header table, add the following code to Page_Init.&lt;/P&gt;
&lt;P class=code&gt;protected void Page_Init(object sender, EventArgs e)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TableRow headerRow = new TableRow();&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int x = 0; x &amp;lt; GridView1.Columns.Count; x++)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DataControlField col = GridView1.Columns[x];&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TableCell headerCell = new TableCell();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerCell.BorderStyle = BorderStyle.Solid;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerCell.BorderWidth = 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerCell.Font.Bold = true;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if the column has a SortExpression, we want to allow&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // sorting by that column. Therefore, we create a linkbutton&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // on those columns.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (col.SortExpression != "")&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LinkButton lnkHeader = new LinkButton();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnkHeader.PostBackUrl = HttpContext.Current.Request.Url.LocalPath;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnkHeader.CommandArgument = col.SortExpression;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnkHeader.ForeColor = System.Drawing.Color.White;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnkHeader.Text = col.HeaderText;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lnkHeader.Click += new EventHandler(HeaderLink_Click);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerCell.Controls.Add(lnkHeader);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerCell.Text = col.HeaderText;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerCell.Width = col.ItemStyle.Width;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; headerRow.Cells.Add(headerCell);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HeaderTable.Rows.Add(headerRow);&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;This code adds a new TableCell to the header table and then sets specific properties on that TableCell based on whether or not the GridView's column has a SortExpression specified. If it doesn't, this code just sets the Text property of the cell to the GridView column's HeaderText property. Otherwise, it creates a LinkButton and sets the following properties on it.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;PostBackUrl&lt;/STRONG&gt; - Set to the local URL of the current request.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;CommandArgument&lt;/STRONG&gt; - Set to the SortExpression of the GridView's column so that we can apply it when the LinkButton is clicked.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;ForeColor&lt;/STRONG&gt; - Set to White in my example, but you can set this dynamically if you wish to make the example more generic.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Text&lt;/STRONG&gt; - Set to the GridView column's HeaderText property.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;I also add a new event handler for the Click event of the LinkButton control. Let's look at the code for that event.&lt;/P&gt;
&lt;H1&gt;Code for the LinkButton's Click Event&lt;/H1&gt;
&lt;P&gt;The code in the LinkButton's Click event applies the necessary SortExpression based on which LinkButton was clicked. The code for the Click event appears below.&lt;/P&gt;
&lt;P class=code&gt;protected void HeaderLink_Click(object sender, System.EventArgs e)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LinkButton lnkHeader = (LinkButton)sender;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SortDirection direction = SortDirection.Ascending;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // the CommandArgument of each linkbutton contains the sortexpression&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // for the column that was clicked.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (GridView1.SortExpression == lnkHeader.CommandArgument)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (GridView1.SortDirection == SortDirection.Ascending)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; direction = SortDirection.Descending;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; GridView1.Sort(lnkHeader.CommandArgument, direction); &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;This code checks the CommandArgument for the LinkButton that was clicked (retrieved from the EventArgs) and applies the ascending or descending SortDirection based upon the result. It then calls the Sort method on the GridView.&lt;/P&gt;
&lt;H1&gt;AJAX-Enabling the Sample&lt;/H1&gt;
&lt;P&gt;If you are using ASP.NET AJAX, the code as-is will not do a partial postback. In order to have the LinkButtons sort using an AJAX async postback, you need to replace the line of code that sets the &lt;STRONG&gt;PostBackUrl&lt;/STRONG&gt; property of the LinkButton with the following code.&lt;/P&gt;
&lt;P class=code&gt;lnkHeader.ID = "Sort" + col.HeaderText;&lt;/P&gt;
&lt;P&gt;Doing this will allow ASP.NET Viewstate to track the LinkButton so that ASP.NET AJAX will work with the LinkButton.&lt;/P&gt;
&lt;P&gt;Using this example, you can easily apply a locked header row to your GridView controls. If you'd like the completed sample project, you can download it below.&lt;/P&gt;
&lt;H2&gt;Sample Project&lt;/H2&gt;
&lt;P&gt;Size: 12KB&lt;BR&gt;&lt;A href="http://www.jimcobooks.com/lockedgrid/LockedGrid.zip" mce_href="http://www.jimcobooks.com/lockedgrid/LockedGrid.zip"&gt;Download Now&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Note: This download is hosted on my personal site, JimcoBooks.com.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9798229" width="1" height="1"&gt;</description></item><item><title>Improving Time-Critical Code written in C</title><link>http://blogs.iis.net/tomchris/archive/2008/09/02/improving-time-critical-code-written-in-c.aspx</link><pubDate>Tue, 02 Sep 2008 10:00:33 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2595111</guid><dc:creator>Anonymous</dc:creator><cs:applicationKey>tomchris</cs:applicationKey><description>&lt;p&gt;Writing web pages is very similar to writing any other type of code.&amp;#160; You have to understand how it is going to be used and where you need to really make things run as quickly as possible.&lt;/p&gt;  &lt;p&gt;There are a number of tips on this blog already that deal with managed code and ways to optimize it.&amp;#160; But one of the things that we don't look at as often are other areas of code that we are dependant on.&amp;#160; For example, if you P/Invoke a native function, whatever it is doing, is something that you need to be aware of.&amp;#160; Because there is a lot of native functionality that is still available to us, I wanted to remind everyone of some of the tips you can use when dealing with C/C++ code.&lt;/p&gt;  &lt;p&gt;A great place to start is with: &lt;a title="http://msdn.microsoft.com/en-us/library/eye126ky.aspx" href="http://msdn.microsoft.com/en-us/library/eye126ky.aspx"&gt;Tips for Improving Time-Critical Code&lt;/a&gt;.&amp;#160; While most of these things don't apply to .NET based code, they are all very important when working with native code.&amp;#160; Another useful thing to be aware of is what the compiler can do for you.&amp;#160; For example, take a look at &lt;a title="http://msdn.microsoft.com/en-us/library/aa290055.aspx" href="http://msdn.microsoft.com/en-us/library/aa290055.aspx"&gt;Optimizing Your Code with Visual C++&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Feel free to add your own tips or pointers here.&amp;#160; I'd like to get a good collection going and then I can share that out as well.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8916635" width="1" height="1"&gt;</description></item><item><title>Debugging .NET Framework Source Code</title><link>http://blogs.iis.net/tomchris/archive/2008/05/13/debugging-net-framework-source-code.aspx</link><pubDate>Tue, 13 May 2008 12:45:10 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2354800</guid><dc:creator>Anonymous</dc:creator><cs:applicationKey>tomchris</cs:applicationKey><description>&lt;p&gt;I am not sure how many people are familiar with the .NET Reference Source project, but if you do a lot of debugging of .NET code, chances are that you have had a situation where you wanted to be able to step into the .NET Source from within Visual Studio.&lt;/p&gt;  &lt;p&gt;Well, thanks to this project.&amp;#160; This is now something that you can accomplish.&amp;#160; It just takes a few steps and you will be off and running.&lt;/p&gt;  &lt;p&gt;I don’t want to go into the specific steps to get this setup as Shawn did a fantastic job explaining them on his blog &lt;a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx"&gt;here&lt;/a&gt;.&amp;#160; This is also mentioned on &lt;a href="http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx"&gt;ScottGu’s blog&lt;/a&gt; with another example.&lt;/p&gt;  &lt;p&gt;This is a big win for everyone.&amp;#160; If you have any problems with this and Shawn’s blog doesn’t have your issue documented, feel free to post it here or on the &lt;a href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=2019&amp;amp;SiteID=1"&gt;MSDN Forum&lt;/a&gt;.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Keep in mind that you view this through a read-only reference license so feel free to look at the code.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2ftom%2farchive%2f2008%2f05%2f13%2fdebugging-net-framework-source-code.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2ftom%2farchive%2f2008%2f05%2f13%2fdebugging-net-framework-source-code.aspx" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8500106" width="1" height="1"&gt;</description></item><item><title>Visual Studio Designer not Respecting Assembly Binding Redirection</title><link>http://blogs.iis.net/tomchris/archive/2008/04/24/visual-studio-designer-not-respecting-assembly-binding-redirection.aspx</link><pubDate>Thu, 24 Apr 2008 19:16:33 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2319889</guid><dc:creator>Anonymous</dc:creator><cs:applicationKey>tomchris</cs:applicationKey><description>&lt;p&gt;So we recently came across an issue where Visual Studio 2005 and 2008 Designers were not respecting assembly binding redirection.&lt;/p&gt;  &lt;h3&gt;Scenario&lt;/h3&gt;  &lt;p&gt;We have a file which is now version 2 and stored in the &lt;a href="http://msdn2.microsoft.com/en-us/library/yf1d93sz.aspx"&gt;GAC&lt;/a&gt;.&amp;#160; So the assembly version is 2.0.0.0.&amp;#160; We have some existing customers using version 1 so we want to be able to use &lt;a href="http://msdn2.microsoft.com/en-us/library/2fc472t2.aspx"&gt;Assembly Binding Redirection&lt;/a&gt; to point them to the new version of the assembly.&lt;/p&gt;  &lt;p&gt;For more information on configuring this, check out &lt;a href="http://msdn2.microsoft.com/en-us/library/433ysdt1.aspx"&gt;Configuring Assembly Binding Redirection&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The web page has code like:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;tc:TestContacts&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;QuickContacts1&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;tc:AContact&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Test Tester&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;Email&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;test@test.com&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;Phone&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;(999) 555-1212&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;tc:AContact&lt;/span&gt; &lt;span class="attr"&gt;Email&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;no@spam.thx&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Mr. A. Nonymous&amp;quot;&lt;/span&gt; 
        &lt;span class="attr"&gt;Phone&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;(999) 555-1234&amp;quot;&lt;/span&gt; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;tc:TestContacts&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;We also have a Register Assembly line at the top that points to the 1.0.0.0 version of the file.&amp;#160; And we have an entry in our web.config that looks like this:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;runtime&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;assemblyBinding&lt;/span&gt; &lt;span class="attr"&gt;xmlns&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;urn:schemas-microsoft-com:asm.v1&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;dependentAssembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;assemblyIdentity&lt;/span&gt; &lt;span class="attr"&gt;name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;Tester1&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;publicKeyToken&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;6ace53ab3256c766&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;bindingRedirect&lt;/span&gt; &lt;span class="attr"&gt;oldVersion&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;1.0.0.0-1.1.0.0&amp;quot;&lt;/span&gt;
                             &lt;span class="attr"&gt;newVersion&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;2.0.0.0&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;dependentAssembly&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;assemblyBinding&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;runtime&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;We could have done this in a policy file and got the same results.&amp;#160; So now we open this file in Design view inside Visual Studio and change a property of the TestContacts control.&amp;#160; For instance, adding an AccessKey.&amp;#160; Switching back to the Source view, we will now see:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;tc:TestContacts&lt;/span&gt; &lt;span class="attr"&gt;ID&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;QuickContacts1&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;runat&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;samples.aspnet.cs.controls.acontact&lt;/span&gt; Name=&amp;quot;Test Tester&amp;quot; 
        Email=&amp;quot;test@test.com&amp;quot; 
        Phone=&amp;quot;(999) 555-1212&amp;quot; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;samples.aspnet.cs.controls.acontact&lt;/span&gt; Email=&amp;quot;no@spam.thx&amp;quot; 
        Name=&amp;quot;Mr. A. Nonymous&amp;quot; 
        Phone=&amp;quot;(999) 555-1234&amp;quot; &lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;tc:TestContacts&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;This breaks the site and it no longer works.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: Assembly Binding Redirection works find in other scenarios, it just doesn’t work right inside the Designer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;Resolutions&lt;/h3&gt;

&lt;p&gt;There are two ways to resolve this issue.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Don’t use Assembly Binding Redirection at all and just leave the 1.0.0.0 version of the file in the GAC.&amp;#160; You will see a lot of .NET files do this, there are versions for .NET 1.0, 1.1, 2.0, etc all inside the GAC at the same time. &lt;/li&gt;

  &lt;li&gt;Change the existing files so that the Register Assembly line at the top of the web page points to the 2.0.0.0 version of the file. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep in mind that this is only a problem if you are actively changing the older files already, if you don’t touch the file it will be fine since this issue is only with the Designer.&amp;#160; So since you are already updating the file, it is trivial to update this line also.&lt;/p&gt;

&lt;p&gt;We are looking into this issue also, so there may be another resolution posted in the future.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2ftom%2farchive%2f2008%2f04%2f24%2fvisual-studio-designer-not-respecting-assembly-binding-redirection.aspx"&gt;&lt;img border="0" alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2ftom%2farchive%2f2008%2f04%2f24%2fvisual-studio-designer-not-respecting-assembly-binding-redirection.aspx" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8422491" width="1" height="1"&gt;</description></item><item><title>Making an Asynchronous Call using the Impersonation Identity</title><link>http://blogs.iis.net/tomchris/archive/2008/04/22/making-an-asynchronous-call-using-the-impersonation-identity.aspx</link><pubDate>Wed, 23 Apr 2008 02:20:09 GMT</pubDate><guid isPermaLink="false">50bcf3b4-f6fe-4638-adff-0c150e922e99:2315024</guid><dc:creator>Anonymous</dc:creator><cs:applicationKey>tomchris</cs:applicationKey><description>&lt;p&gt;If you try to make an asynchronous call, you will notice that the thread that executes the call doesn't run under the same account as the thread that called it, assuming you are using impersonation.&lt;/p&gt;  &lt;p&gt;There are a number of ways to change this if you would like to have it use the same identity.&lt;/p&gt;  &lt;h4&gt;Method 1&lt;/h4&gt;  &lt;p&gt;The Thread used by BeginInvoke doesn't copy the windowsIdentity from the calling thread. You have to impersonate the new thread manually:    &lt;br /&gt;Before calling BeginInvoke save the current identity into a variable:&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;Dim&lt;/span&gt; identity &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; WindowsIdentity
identity = System.Security.Principal.WindowsIdentity.GetCurrent()&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Inside the asynchronously called method use this variable and execute:&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;identity.Impersonate&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;From that point on the asynchronous call uses the same privileges than the calling thread and it should not be a problem to execute the callback. 
  &lt;br /&gt;&lt;/p&gt;

&lt;h4&gt;Method 2&lt;/h4&gt;

&lt;p&gt;If the thread making the asynchronous call creates the thread being used, and you are using .NET 2.0 or later, you can set the following in the config file.&amp;#160; For ASP.NET, use the aspnet.config file:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&amp;lt;configuration&amp;gt;&lt;br /&gt; &amp;lt;runtime&amp;gt;&lt;br /&gt;   &amp;lt;alwaysFlowImpersonationPolicy enabled=&amp;quot;true&amp;quot;/&amp;gt;&lt;br /&gt;   &amp;lt;legacyImpersonationPolicy enabled=&amp;quot;false&amp;quot;/&amp;gt;&lt;br /&gt; &amp;lt;/runtime&amp;gt;&lt;br /&gt;&amp;lt;/configuration&amp;gt;&lt;/pre&gt;

&lt;h4&gt;Method 3&lt;/h4&gt;

&lt;p&gt;Another way to handle this is if you have an account that you want the asynchronous call to run under, you can impersonate that account, run what you need to, and then undo the impersonation.&amp;#160; The following code will do that:&lt;/p&gt;

&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;
  &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Security.Principal;
...
WindowsIdentity wi = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; WindowsIdentity(userName@fullyqualifieddomainName);
WindowsImpersonationContext ctx = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
&lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
{
  ctx = wi.Impersonate();
  &lt;span style="color: #008000"&gt;// Thread is now impersonating&lt;/span&gt;
}
&lt;span style="color: #0000ff"&gt;catch&lt;/span&gt;
{
  &lt;span style="color: #008000"&gt;// Prevent exceptions propagating.&lt;/span&gt;
}
&lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
{
  &lt;span style="color: #008000"&gt;// Ensure impersonation is reverted&lt;/span&gt;
  ctx.Undo();
}&lt;/pre&gt;
&lt;/div&gt;

&lt;h4&gt;Additional Information&lt;/h4&gt;

&lt;p&gt;For more information, you can check out these links&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a title="How To- Use Impersonation and Delegation in ASP.NET 2.0" href="http://msdn2.microsoft.com/en-us/library/ms998351.aspx"&gt;How To- Use Impersonation and Delegation in ASP.NET 2.0&lt;/a&gt; &lt;/li&gt;

  &lt;li&gt;&lt;a title="ASP.NET 2.0 Security Questions and Answers - Impersonation" href="http://www.securityguidanceshare.com/index.php?title=ASP.NET_2.0_Security_Questions_and_Answers_-_Impersonation_/_Delegation&amp;amp;printable=yes"&gt;ASP.NET 2.0 Security Questions and Answers - Impersonation&lt;/a&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fblogs.msdn.com%2ftom%2farchive%2f2008%2f04%2f22%2fmaking-an-asynchronous-call-using-the-impersonation-identity.aspx"&gt;&lt;img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fblogs.msdn.com%2ftom%2farchive%2f2008%2f04%2f22%2fmaking-an-asynchronous-call-using-the-impersonation-identity.aspx" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8417771" width="1" height="1"&gt;</description></item></channel></rss>