<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.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/"><channel><title>Raj Kaimal  : .NET</title><link>http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx</link><description>Tags: .NET</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Rendering an RDLC directly to the Response stream in ASP.NET MVC</title><link>http://weblogs.asp.net/rajbk/archive/2009/11/25/rendering-an-rdlc-directly-to-the-response-stream-in-asp-net-mvc.aspx</link><pubDate>Wed, 25 Nov 2009 07:21:59 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7264611</guid><dc:creator>rajbk</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=7264611</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2009/11/25/rendering-an-rdlc-directly-to-the-response-stream-in-asp-net-mvc.aspx#comments</comments><description>&lt;p&gt;The following post shows you how to render an &lt;a href="http://msdn.microsoft.com/en-us/library/ms252067.aspx" target="_blank"&gt;RDLC&lt;/a&gt; (Client Report Definition File) in a MVC project. For this tutorial, I am using VS 2008 with &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=4817cdb2-88ea-4af4-a455-f06b4c90fd2c&amp;amp;displaylang=en" target="_blank"&gt;MVC 2 Beta&lt;/a&gt;&lt;strong&gt;&lt;/strong&gt;. I will also be using the priceless Northwind database and the report will contain a list of customers in the Northwind database.     &lt;br /&gt;    &lt;br /&gt;A sample project zip file is provided at the bottom of this post.     &lt;br /&gt;    &lt;br /&gt;We start off by creating an ASP.NET MVC 2 Empty Web Application.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_7A859418.png" width="634" height="379" /&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Add a new ADO.NET entity model and choose the option to “Generate from the database”.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_390AE1BF.png" width="634" height="349" /&gt;     &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_3BD096B2.png" width="627" height="305" /&gt;&amp;#160; &lt;br /&gt;Choose the connection string to use&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_28AF6A06.png" width="627" height="435" /&gt;&lt;/p&gt;  &lt;p&gt;Choose the database objects (Customers for our scenario) and hit finish. We see that a new entity data model has been created in the Models directory.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_4066A16C.png" width="627" height="558" /&gt;&amp;#160; &lt;br /&gt;Create a folder called Content and a subfolder called Reports and add an RDLC into this folder. I hate working with Datasets and hence will not be using the “Report Wizard”. We choose the “Report” template instead.&amp;#160; &lt;br /&gt;    &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_1E9AC8DB.png" width="634" height="488" /&gt; &lt;/p&gt;  &lt;p&gt;Create a method that returns all the customers in the database through a Customer partial class and add this in the Model folder.    &lt;br /&gt;    &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_36520041.png" width="229" height="84" /&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; RDLCRendering.Models&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;partial&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; Customers&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; List&amp;lt;Customers&amp;gt; GetAllCustomers() {&lt;br /&gt;            var entities = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; NorthwindEntities();&lt;br /&gt;            var x = from c &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; entities.Customers&lt;br /&gt;                    select c;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; x.ToList();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;Open&amp;#160; the RDLC. Your UI should look like so (If you don’t see “Website Data Sources”, click on Data –&amp;gt; Show Data Sources):&lt;/div&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_63D32004.png" width="647" height="441" /&gt; &lt;/p&gt;

&lt;p&gt;Design your report by adding a table from the toolbox and dragging and dropping fields from the Website Data Sources (&lt;a href="http://msdn.microsoft.com/en-us/library/ms251659(VS.80).aspx" target="_blank"&gt;reference&lt;/a&gt;). &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_54500E35.png" width="673" height="290" /&gt;&amp;#160; &lt;/p&gt;

&lt;p&gt;We will now construct our basic MVC web application. The app will have a home screen and a link to view the PDF report of Northwind customers. 
  &lt;br /&gt;Lets start by adding a Home controller&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_484E1E01.png" width="497" height="204" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;We add the following code in our HomeController class&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; RDLCRendering.Controllers&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; HomeController : Controller&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Index()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; welcomeMessage = &lt;span style="color: #006080"&gt;&amp;quot;Welcome to Northwind Traders. Enjoy the view!&amp;quot;&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View((&lt;span style="color: #0000ff"&gt;object&lt;/span&gt;)welcomeMessage);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;br /&gt;Right click on the &amp;quot;Index()” method and select “Add View”.&lt;/div&gt;

&lt;div&gt;
  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/rajbk/image_1988655F.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_thumb_0D86752B.png" width="447" height="441" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Open the Index view and add code to display the string returned and also add a link for the report in the view like so:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&amp;lt;%@ Page Language=&lt;span style="color: #006080"&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; Inherits=&lt;span style="color: #006080"&gt;&amp;quot;System.Web.Mvc.ViewPage&amp;lt;string&amp;gt;&amp;quot;&lt;/span&gt; %&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;!DOCTYPE html PUBLIC &lt;span style="color: #006080"&gt;&amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN&amp;quot;&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #006080"&gt;&amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;lt;html xmlns=&lt;span style="color: #006080"&gt;&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;lt;head runat=&lt;span style="color: #006080"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;    &amp;lt;title&amp;gt;Index&amp;lt;/title&amp;gt;&lt;br /&gt;&amp;lt;/head&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;&lt;br /&gt;    &amp;lt;div&amp;gt;&lt;br /&gt;        &lt;font color="#ff0000"&gt;&amp;lt;h2&amp;gt;&lt;br /&gt;            &amp;lt;%=Html.Encode(Model) %&amp;gt;&amp;lt;/h2&amp;gt;&lt;/font&gt;&lt;br /&gt;            &lt;br /&gt;           &lt;font color="#ff0000"&gt; &amp;lt;%=Html.ActionLink(&lt;span style="color: #006080"&gt;&amp;quot;Customers Report (PDF)&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;DetailsReport&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;Customers&amp;quot;&lt;/span&gt;) %&amp;gt;&lt;br /&gt;&lt;/font&gt;    &amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;/body&amp;gt;&lt;br /&gt;&amp;lt;/html&amp;gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;Run the application. You should see a UI similar to the one below:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_4C77F5C6.png" width="575" height="240" /&gt; 

  &lt;br /&gt;If you click on the link you will get a “The resource cannot be found.” error. This is because our Html.Action link refers to a method called “DetailsReport” in a CustomersController which does not exist yet. 

  &lt;br /&gt;&lt;font size="1"&gt;Note that we are using the ActionLink overload(linkText, actionName, controllerName).&lt;/font&gt; &lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;First add a reference to Microsoft.ReportViewer.WebForms in your project.&lt;/div&gt;

&lt;div&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_0B697662.png" width="304" height="109" /&gt; &lt;/div&gt;

&lt;div&gt;Add a CustomerController class and add a DetailsReport method like so: 
  &lt;br /&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Web;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Web.Mvc;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; System.Web.Mvc.Ajax;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; Microsoft.Reporting.WebForms;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; RDLCRendering.Models;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;namespace&lt;/span&gt; RDLCRendering.Controllers&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; CustomersController : Controller&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult Index()&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; View();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; ActionResult DetailsReport()&lt;br /&gt;        {&lt;br /&gt;            LocalReport localReport = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; LocalReport();&lt;br /&gt;            localReport.ReportPath = Server.MapPath(&lt;span style="color: #006080"&gt;&amp;quot;~/Content/Reports/CustomerReport.rdlc&amp;quot;&lt;/span&gt;);&lt;br /&gt;            ReportDataSource reportDataSource = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ReportDataSource(&lt;span style="color: #006080"&gt;&amp;quot;Customers&amp;quot;&lt;/span&gt;, Customers.GetAllCustomers());&lt;br /&gt;&lt;br /&gt;            localReport.DataSources.Add(reportDataSource);&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; reportType = &lt;span style="color: #006080"&gt;&amp;quot;PDF&amp;quot;&lt;/span&gt;;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; mimeType;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; encoding;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; fileNameExtension;&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #008000"&gt;//The DeviceInfo settings should be changed based on the reportType&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #008000"&gt;//http://msdn2.microsoft.com/en-us/library/ms155397.aspx&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; deviceInfo =&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;DeviceInfo&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;OutputFormat&amp;gt;PDF&amp;lt;/OutputFormat&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;PageWidth&amp;gt;8.5in&amp;lt;/PageWidth&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;PageHeight&amp;gt;11in&amp;lt;/PageHeight&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;MarginTop&amp;gt;0.5in&amp;lt;/MarginTop&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;MarginLeft&amp;gt;1in&amp;lt;/MarginLeft&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;MarginRight&amp;gt;1in&amp;lt;/MarginRight&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;  &amp;lt;MarginBottom&amp;gt;0.5in&amp;lt;/MarginBottom&amp;gt;&amp;quot;&lt;/span&gt; +&lt;br /&gt;            &lt;span style="color: #006080"&gt;&amp;quot;&amp;lt;/DeviceInfo&amp;gt;&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;            Warning[] warnings;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;[] streams;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] renderedBytes;&lt;br /&gt;&lt;br /&gt;            &lt;span style="color: #008000"&gt;//Render the report&lt;/span&gt;&lt;br /&gt;            renderedBytes = localReport.Render(&lt;br /&gt;                reportType,&lt;br /&gt;                deviceInfo,&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; mimeType,&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; encoding,&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; fileNameExtension,&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; streams,&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;out&lt;/span&gt; warnings);&lt;br /&gt;            &lt;span style="color: #008000"&gt;//Response.AddHeader(&amp;quot;content-disposition&amp;quot;, &amp;quot;attachment; filename=NorthWindCustomers.&amp;quot; + fileNameExtension);&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; File(renderedBytes, mimeType);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;This method will create an instance of the LocalReport class, set the Report path property, add a ReportDataSource which points to the GetAllCustomers method we defined earlier, set the report output type and page dimensions, calls the render method and invokes the File method on the Controller class. This method return a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.filecontentresult.aspx" target="_blank"&gt;FileContentResult&lt;/a&gt; which sends the contents of a binary file to the response stream. 

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;Run the page and click on the link. You should see the PDF report like so:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/rajbk/image_6EEC8481.png" width="938" height="365" /&gt; &lt;/div&gt;

&lt;div&gt;&lt;strong&gt;If you wish to prompt the user to download the PDF file, uncomment the “content-disposition” line and recompile.&lt;/strong&gt; 

  &lt;br /&gt;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;&amp;#160; &lt;br /&gt;&lt;/div&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:F60BB8FA-6F02-4999-8F5E-9DD4E92C4DA7:2491eac2-cad1-4576-9281-da659a8c1205" class="wlWriterEditableSmartContent"&gt;&lt;div&gt;&lt;a href="http://weblogs.asp.net/blogs/rajbk/RDLCRendering_3BB04B18.zip" target="_blank"&gt;Download Sample MVC Beta 2 project&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div&gt;
  &lt;br /&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7264611" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Data/default.aspx">Data</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ADO.NET/default.aspx">ADO.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The Windows Azure Cloud Air Handling Unit</title><link>http://weblogs.asp.net/rajbk/archive/2009/11/24/the-windows-azure-cloud-air-handling-unit.aspx</link><pubDate>Wed, 25 Nov 2009 00:35:17 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7264449</guid><dc:creator>rajbk</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=7264449</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2009/11/24/the-windows-azure-cloud-air-handling-unit.aspx#comments</comments><description>&lt;p&gt;Great technical video presented by Patrick Yantz showing the Azure Cloud Air handling unit. Patrick is a Datacenter Systems Architecture Manager at Microsoft.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://tv.devexpress.com/PDC09AzureBox.movie" target="_blank"&gt;Version 1&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-Guided-Tour-inside-the-Windows-Azure-Cloud-with-Patrick-Yantz/" target="_blank"&gt;Version 2&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Sun Microsystems has something similar – &lt;a href="http://www.sun.com/products/sunmd/s20/" target="_blank"&gt;The Sun Modular DataCenter&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7264449" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Azure/default.aspx">Azure</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Microsoft/default.aspx">Microsoft</category></item><item><title>VS 2010 Beta 2 Installation instructions</title><link>http://weblogs.asp.net/rajbk/archive/2009/10/19/vs-2010-beta-2-installation-instructions.aspx</link><pubDate>Tue, 20 Oct 2009 01:39:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7233430</guid><dc:creator>rajbk</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=7233430</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2009/10/19/vs-2010-beta-2-installation-instructions.aspx#comments</comments><description>&lt;P&gt;For those of you that have VS 2010 Beta 1 installed.&lt;/P&gt;
&lt;P&gt;&lt;A title=http://go.microsoft.com/fwlink/?LinkID=166199 href="http://go.microsoft.com/fwlink/?LinkID=166199" mce_href="http://go.microsoft.com/fwlink/?LinkID=166199"&gt;http://go.microsoft.com/fwlink/?LinkID=166199&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A title=http://go.microsoft.com/fwlink/?linkid=167718 href="http://go.microsoft.com/fwlink/?LinkID=167718" mce_href="http://go.microsoft.com/fwlink/?LinkID=167718"&gt;http://go.microsoft.com/fwlink/?LinkID=167718&lt;/A&gt; (addendum)&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Other observations:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was prompted for my VS 2010 Beta 1 media when uninstalling it so have your iso or media ready.&lt;/P&gt;
&lt;P&gt;VS 2010 Beta 2 prompted for a OS restart during installation. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;A href="http://weblogs.asp.net/scottgu/archive/2009/10/19/vs-2010-and-net-4-0-beta-2.aspx" target=_blank mce_href="http://weblogs.asp.net/scottgu/archive/2009/10/19/vs-2010-and-net-4-0-beta-2.aspx"&gt;See announcement for more details&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7233430" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/C_2300_/default.aspx">C#</category></item><item><title>XMLSerializer and invalid XML</title><link>http://weblogs.asp.net/rajbk/archive/2009/10/04/xmlserializer-and-invalid-xml.aspx</link><pubDate>Mon, 05 Oct 2009 01:29:08 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7223527</guid><dc:creator>rajbk</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=7223527</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2009/10/04/xmlserializer-and-invalid-xml.aspx#comments</comments><description>&lt;p&gt;A user had pasted some text from powerpoint into a textarea in one of our web apps which was eventually serialized into XML. Upon trying to de-serialize the XML, the web server threw the exception below:&lt;/p&gt;  &lt;p&gt;&lt;font color="#ff0000" size="2" face="Courier New"&gt;System.InvalidOperationException: There is an error in XML document (1, 50). ---&amp;gt; System.Xml.XmlException: '♂', hexadecimal value 0x0B, is an invalid character. Line 1, position 50.&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The XML that was about to be de-serialized looked like something this:&lt;/p&gt;  &lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 300px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #800000"&gt;xml&lt;/span&gt; &lt;span style="color: #ff0000"&gt;version&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;utf-16&amp;quot;&lt;/span&gt;?&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;string&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;Quick&amp;amp;#xB;Brown&lt;br /&gt;Fox&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;string&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The code used to de-serialze the xml is shown below:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 300px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; T DeSerializeObject&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; xml)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (System.IO.StringReader sr = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.IO.StringReader(xml))&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;        XmlSerializer serializer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XmlSerializer(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; (T)serializer.Deserialize(sr);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;
  &lt;br /&gt;As seen from the exception above, the de-serializer was complaining about the &lt;a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets" target="_blank"&gt;invalid character&lt;/a&gt; - &amp;amp;#xB;.&amp;#160;&amp;#160; &lt;br /&gt;

  &lt;br /&gt;The PowerPoint slide that text was being pasted from looked something like this: &lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ppt" border="0" alt="ppt" src="http://weblogs.asp.net/blogs/rajbk/ppt_24C870FA.gif" width="290" height="218" /&gt; 

  &lt;br /&gt;The user had used the Shift-Enter key combination to force a line break between text in a bullet. So “Brown” is started on a new line for the same bullet as “Quick”. 

  &lt;br /&gt;

  &lt;br /&gt;By selecting all the text on the slide and pasting it into a hex editor, we see the standard hex values for carriage return and line feed – 0x0D 0x0A between “Brown” and “Fox”. But we see 0x0B was generated for the new line between “Quick” and “Brown”. 0x0B in ASCII stands for a &lt;a href="http://en.wikipedia.org/wiki/ASCII#ASCII_printable_characters" target="_blank"&gt;vertical line tab&lt;/a&gt; which is an &lt;a href="http://www.w3.org/TR/2006/REC-xml-20060816/#charsets" target="_blank"&gt;invalid character&lt;/a&gt; in XML.&amp;#160; &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ppthex" border="0" alt="ppthex" src="http://weblogs.asp.net/blogs/rajbk/ppthex_6B456102.gif" width="622" height="56" /&gt;&lt;/p&gt;

&lt;p&gt;The code used to serialize the string is shown below:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 300px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SerializeObject&amp;lt;T&amp;gt;(T source)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    var serializer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XmlSerializer(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&amp;#160;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var sw = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.IO.StringWriter())&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    &lt;strong&gt;&lt;font color="#ff0000"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var writer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XmlTextWriter(sw))&lt;/font&gt;&lt;/strong&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;        serializer.Serialize(writer, source);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; sw.ToString();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The problem with this code is that the XmlTextWriter class which inherits from XmlWriter, does not, on its own, validate each character before serializing it. 
  &lt;br /&gt;

  &lt;br /&gt;The recommended way to serialize is to use the static &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.create.aspx" target="_blank"&gt;XmlWriter.Create&lt;/a&gt; method. The method has overloads where you specify an &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx" target="_blank"&gt;XmlWriterSettings&lt;/a&gt; class. If you do not specify one, the default values for the &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.aspx" target="_blank"&gt;XmlWriterSettings&lt;/a&gt; class is used. One of the properties of this class is the &lt;a href="http://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings.checkcharacters.aspx" target="_blank"&gt;XmlWriterSettings.CheckCharacters&lt;/a&gt; Property which is set to &lt;strong&gt;true&lt;/strong&gt; by default. This property ensures that the XmlWriter instance created by the .Create method will perform character checking. By implementing the .Create* method in our code, we ensure that the serializer will throw an exception if it encounters invalid XML.&lt;/p&gt;

&lt;p&gt;We, therefore, can rewrite our serializer class like so:&lt;/p&gt;

&lt;div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; max-height: 300px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"&gt;
  &lt;div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;
    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; SerializeObject&amp;lt;T&amp;gt;(T source)&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;{&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    var serializer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; XmlSerializer(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T));&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var sw = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.IO.StringWriter())&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    &lt;font color="#ff0000"&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var writer = XmlWriter.Create(sw))&lt;/font&gt;&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    {&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;        serializer.Serialize(writer, source);&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; sw.ToString();&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: white; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;    }&lt;/pre&gt;
&lt;!--CRLF--&gt;

    &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;}&lt;/pre&gt;
&lt;!--CRLF--&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="1"&gt;* The writer created in this case is of type System.Xml.XmlWellFormedWriter. It will vary based on the .Create overload used.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7223527" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/XML/default.aspx">XML</category></item><item><title>FormView Binding Gotcha</title><link>http://weblogs.asp.net/rajbk/archive/2009/08/03/formview-binding-gotcha.aspx</link><pubDate>Mon, 03 Aug 2009 06:17:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7160036</guid><dc:creator>rajbk</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=7160036</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2009/08/03/formview-binding-gotcha.aspx#comments</comments><description>&lt;P&gt;&lt;STRONG&gt;Version: ASP.NET 3.5 SP1&lt;BR&gt;&lt;BR&gt;&lt;/STRONG&gt;This post describes two gotchas with the FormView control when binding:&lt;BR&gt;1) When performing two way databinding, Null values and Nullables get changed to string.Empty in the FormView. &lt;BR&gt;2) Two way binding is not supported for nested controls. &lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;When performing two way databinding, Null values and Nullables get changed to string.Empty in the FormView. &lt;BR&gt;&lt;/STRONG&gt;The way the FormView control handles null values has caused me some grief :-(&lt;/P&gt;
&lt;P&gt;Consider a FormView bound to an ObjectDataSource like so:&lt;/P&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&amp;lt;asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" DefaultMode="Edit"&amp;gt;&lt;BR&gt;    &amp;lt;EditItemTemplate&amp;gt;&lt;BR&gt;        FirstName:&lt;BR&gt;        &amp;lt;asp:TextBox ID="FirstNameTextBox" runat="server" Text=&lt;SPAN style="COLOR: #006080"&gt;'&amp;lt;%# Bind("FirstName") %&amp;gt;'&lt;/SPAN&gt; /&amp;gt;&lt;BR&gt;        &amp;lt;br /&amp;gt;&lt;BR&gt;        LastName:&lt;BR&gt;        &amp;lt;asp:TextBox ID="LastNameTextBox" runat="server" Text=&lt;SPAN style="COLOR: #006080"&gt;'&amp;lt;%# Bind("LastName") %&amp;gt;'&lt;/SPAN&gt; /&amp;gt;&lt;BR&gt;        &amp;lt;br /&amp;gt;&lt;BR&gt;        &amp;lt;asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="&lt;SPAN style="COLOR: #0000ff"&gt;True&lt;/SPAN&gt;" CommandName="&lt;SPAN style="COLOR: #0000ff"&gt;Update&lt;/SPAN&gt;"&lt;BR&gt;            Text="&lt;SPAN style="COLOR: #0000ff"&gt;Update&lt;/SPAN&gt;" /&amp;gt;&lt;BR&gt;    &amp;lt;/EditItemTemplate&amp;gt;&lt;BR&gt;&amp;lt;/asp:FormView&amp;gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&amp;lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="Employee"&lt;BR&gt;       &lt;STRONG&gt; OldValuesParameterFormatString="original_{0}"&lt;/STRONG&gt; SelectMethod="GetEmployee" TypeName="EmployeeWrapper"&lt;BR&gt;        &lt;STRONG&gt;ConflictDetection="CompareAllValues"&lt;/STRONG&gt; UpdateMethod="SetEmployee"&amp;gt;&amp;lt;/asp:ObjectDataSource&amp;gt;&lt;/PRE&gt;&lt;BR&gt;The Employee object returned by the ObjectDataSource has LastName set to null like so: &lt;BR&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;[DataObjectMethod(DataObjectMethodType.&lt;SPAN style="COLOR: #0000ff"&gt;Select&lt;/SPAN&gt;)]&lt;BR&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; Employee GetEmployee()&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; Employee()&lt;BR&gt;    {&lt;BR&gt;        FirstName = "Tom",&lt;BR&gt;        LastName = &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;&lt;BR&gt;    };&lt;BR&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Click the Update button on the FormView without changing any properties. In the Form &lt;A href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemupdating%28loband%29.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemupdating%28loband%29.aspx"&gt;ItemUpdating&lt;/A&gt; event, we see that both the OldValues and NewValues have the LastName property set to string.Empty. &lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;STRONG&gt;(OldValue according &lt;SPAN style="COLOR: #0000ff"&gt;to&lt;/SPAN&gt; FormView)&lt;/STRONG&gt;&lt;BR&gt;FirstName : Tom&lt;BR&gt;&lt;FONT color=#ff0000&gt;LastName :&lt;/FONT&gt;&lt;BR&gt; &lt;BR&gt;&lt;STRONG&gt;(NewValue according &lt;SPAN style="COLOR: #0000ff"&gt;to&lt;/SPAN&gt; FormView)&lt;/STRONG&gt;&lt;BR&gt;FirstName : Tom&lt;BR&gt;&lt;FONT color=#ff0000&gt;LastName :&lt;/FONT&gt; &lt;/PRE&gt;&lt;/DIV&gt;On the other hand, if we try the same thing with the DetailsView control, the LastName will still be “null” on update. &lt;BR&gt;&lt;BR&gt;This FormView behavior can be problematic when we we need the original object and the modified object to perform an update with &lt;A href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.updatemethod%28VS.80%29.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.updatemethod%28VS.80%29.aspx"&gt;conflict detection&lt;/A&gt; (see LINQ to SQL example below). Note that ConflictDetection is set to CompareAllValues in the ObjectDataSource. &lt;BR&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&amp;lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server" &lt;FONT color=#ff0000&gt;&lt;STRONG&gt;ConflictDetection="CompareAllValues"&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR&gt;    DataObjectTypeName="Customer" &lt;FONT color=#ff0000&gt;&lt;STRONG&gt;OldValuesParameterFormatString="original_{0}"&lt;/STRONG&gt;&lt;/FONT&gt; SelectMethod="GetCustomer"&lt;BR&gt;    TypeName="CustomerWrapper" UpdateMethod="SetCustomer"&amp;gt;&lt;BR&gt;    &amp;lt;UpdateParameters&amp;gt;&lt;BR&gt;        &amp;lt;asp:&lt;SPAN style="COLOR: #0000ff"&gt;Parameter&lt;/SPAN&gt; Name="customer" Type="&lt;SPAN style="COLOR: #0000ff"&gt;Object&lt;/SPAN&gt;" /&amp;gt;&lt;BR&gt;        &amp;lt;asp:&lt;SPAN style="COLOR: #0000ff"&gt;Parameter&lt;/SPAN&gt; Name="original_customer" Type="&lt;SPAN style="COLOR: #0000ff"&gt;Object&lt;/SPAN&gt;" /&amp;gt;&lt;BR&gt;    &amp;lt;/UpdateParameters&amp;gt;&lt;BR&gt;&amp;lt;/asp:ObjectDataSource&amp;gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;SPAN style="COLOR: #0000ff"&gt;public&lt;/SPAN&gt; void SetCustomer(Customer customer, Customer original_customer)&lt;BR&gt;{&lt;BR&gt;    DataClassesDataContext context = &lt;SPAN style="COLOR: #0000ff"&gt;new&lt;/SPAN&gt; DataClassesDataContext();&lt;BR&gt;    context.Customers.Attach(customer, original_customer);&lt;BR&gt;    context.SubmitChanges();&lt;BR&gt;}&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;In brief, LINQ to SQL compares the original value and new value and generates SQL code to update the original object. Since the original object was modified by the FormView, it will never be found by SQL and no update will occur! &lt;BR&gt;&lt;BR&gt;While there are &lt;A href="http://blogs.msdn.com/dinesh.kulkarni/archive/2007/10/08/attach-if-you-have-something-detached.aspx" target=_blank mce_href="http://blogs.msdn.com/dinesh.kulkarni/archive/2007/10/08/attach-if-you-have-something-detached.aspx"&gt;other techniques&lt;/A&gt; you can use with Linq to SQL to avoid the issue above, the point of this post is to show how the FormView handles null values.&lt;/P&gt;
&lt;P&gt;The workaround is to subscribe to the FormView ItemUpdating event and look for string.empty NewValues and set those to null instead. &lt;/P&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;protected void FormView2_ItemUpdating(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, FormViewUpdateEventArgs e)&lt;BR&gt;{&lt;BR&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt; (e.NewValues["LastName"].&lt;SPAN style="COLOR: #0000ff"&gt;Equals&lt;/SPAN&gt;(string.Empty))&lt;BR&gt;    {&lt;BR&gt;        e.NewValues["LastName"] = &lt;SPAN style="COLOR: #0000ff"&gt;null&lt;/SPAN&gt;;&lt;BR&gt;&lt;BR&gt;    }&lt;BR&gt;}&lt;/PRE&gt;&lt;BR&gt;In addition, to keep the OldValues unchanged (if you need&amp;nbsp;it for concurrency purposes), set the DataKeyNames property of the FormView to the properties of the object like so: &lt;BR&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&amp;lt;asp:FormView ID="FormView2" runat="server" DataSourceID="ObjectDataSource1" OnItemUpdating="FormView2_ItemUpdating"&lt;BR&gt;        DefaultMode="Edit" &lt;FONT color=#ff0000&gt;&lt;STRONG&gt;DataKeyNames="LastName, FirstName"&lt;/STRONG&gt;&lt;/FONT&gt;&amp;gt;&lt;/PRE&gt;&lt;BR&gt;When the Update button is clicked this time, the null values are retained. &lt;BR&gt;&lt;/DIV&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&lt;STRONG&gt;(OldValue according &lt;SPAN style="COLOR: #0000ff"&gt;to&lt;/SPAN&gt; FormView)&lt;/STRONG&gt;&lt;BR&gt;FirstName : Tom&lt;BR&gt;&lt;FONT color=#ff0000&gt;&lt;STRONG&gt;LastName : null&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR&gt; &lt;BR&gt;&lt;STRONG&gt;(NewValue according &lt;SPAN style="COLOR: #0000ff"&gt;to&lt;/SPAN&gt; FormView)&lt;/STRONG&gt;&lt;BR&gt;FirstName : Tom&lt;BR&gt;&lt;FONT color=#ff0000&gt;&lt;STRONG&gt;LastName : null&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/PRE&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;BR&gt;&lt;STRONG&gt;Two way binding is not supported for nested controls – AFAIK, this is by design.&lt;/STRONG&gt; &lt;BR&gt;See an example below:&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;&amp;lt;EditItemTemplate&amp;gt;&lt;BR&gt;   &lt;FONT color=#ff0000&gt; &amp;lt;asp:UpdatePanel runat="server" ID="up1"&amp;gt;&lt;BR&gt;        &amp;lt;ContentTemplate&amp;gt;&lt;BR&gt;            FirstName:&lt;BR&gt;            &amp;lt;asp:TextBox ID="FirstNameTextBox" runat="server" Text=&lt;SPAN style="COLOR: #006080"&gt;'&amp;lt;%# Bind("FirstName") %&amp;gt;'&lt;/SPAN&gt; /&amp;gt;&lt;BR&gt;        &amp;lt;/ContentTemplate&amp;gt;&lt;BR&gt;    &amp;lt;/asp:UpdatePanel&amp;gt;&lt;BR&gt;&lt;/FONT&gt;    LastName:&lt;BR&gt;    &amp;lt;asp:TextBox ID="LastNameTextBox" runat="server" Text=&lt;SPAN style="COLOR: #006080"&gt;'&amp;lt;%# Bind("LastName") %&amp;gt;'&lt;/SPAN&gt; /&amp;gt;&lt;BR&gt;    &amp;lt;br /&amp;gt;&lt;BR&gt;    &amp;lt;asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="&lt;SPAN style="COLOR: #0000ff"&gt;True&lt;/SPAN&gt;" CommandName="&lt;SPAN style="COLOR: #0000ff"&gt;Update&lt;/SPAN&gt;"&lt;BR&gt;        Text="&lt;SPAN style="COLOR: #0000ff"&gt;Update&lt;/SPAN&gt;" /&amp;gt;&lt;BR&gt;&amp;lt;/EditItemTemplate&amp;gt;&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;Here we have a TextBox inside an UpdatePanel inside the EditItemTemplate. Since two way binding is not supported for the FirstNameTextBox, the special code for &lt;A href="http://www.manuelabadia.com/blog/CommentView,guid,45f5c9da-8a03-423f-b5b6-5882c4bd67e5.aspx" target=_blank mce_href="http://www.manuelabadia.com/blog/CommentView,guid,45f5c9da-8a03-423f-b5b6-5882c4bd67e5.aspx"&gt;extracting values&lt;/A&gt; is not generated resulting in null being sent back for the FirstName property. &lt;BR&gt;&lt;BR&gt;To work around this, you will need to subscribe to the ItemUpdating event and extract the values yourself: &lt;BR&gt;&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;protected void FormView2_ItemUpdating(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, FormViewUpdateEventArgs e)&lt;BR&gt;{&lt;BR&gt;    TextBox FirstNameTextBox = (TextBox)FormView2.FindControl("FirstNameTextBox");&lt;BR&gt;    e.NewValues["FirstName"] = FirstNameTextBox.Text;&lt;BR&gt;}&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;STRONG&gt;Visibility of controls in a FormView.&lt;/STRONG&gt; &lt;BR&gt;Setting Visibility of a control inside a FormView does not work - the TextBox control goes back to visible on update. &lt;BR&gt;&lt;/DIV&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;protected void Page_Load(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, EventArgs e)&lt;BR&gt;{&lt;BR&gt;    FormView1.FindControl("FirstNameTextBox").Visible = &lt;SPAN style="COLOR: #0000ff"&gt;false&lt;/SPAN&gt;;&lt;BR&gt;}&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;Do it in the DataBound event instead:&lt;/P&gt;
&lt;DIV id=codeSnippetWrapper&gt;&lt;PRE style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: 'Courier New', courier, monospace; DIRECTION: ltr; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px" id=codeSnippet&gt;protected void FormView2_DataBound(&lt;SPAN style="COLOR: #0000ff"&gt;object&lt;/SPAN&gt; sender, EventArgs e)&lt;BR&gt;{&lt;BR&gt;    FormView1.FindControl("FirstNameTextBox").Visible = &lt;SPAN style="COLOR: #0000ff"&gt;false&lt;/SPAN&gt;;&lt;BR&gt;}&lt;/PRE&gt;&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;If you have better ideas on any of the workarounds above, please post a comment or link. Thanks. &lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7160036" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>The .NET Framework 3.5 Commonly Used Types and Namespaces poster</title><link>http://weblogs.asp.net/rajbk/archive/2007/11/03/the-net-framework-3-5-commonly-used-types-and-namespaces-poster.aspx</link><pubDate>Sun, 04 Nov 2007 04:39:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4886899</guid><dc:creator>rajbk</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=4886899</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2007/11/03/the-net-framework-3-5-commonly-used-types-and-namespaces-poster.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/pandrew/archive/2007/11/02/announcing-the-net-framework-3-5-commonly-used-types-and-namespaces-poster.aspx" target="_blank" mce_href="http://blogs.msdn.com/pandrew/archive/2007/11/02/announcing-the-net-framework-3-5-commonly-used-types-and-namespaces-poster.aspx"&gt;Paul Andrew&lt;/a&gt; has posted a link to the .NET framework 3.5 commonly Used Types and Namespaces poster - cool stuff!&lt;br&gt; &lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;a href="http://download.microsoft.com/download/4/a/3/4a3c7c55-84ab-4588-84a4-f96424a7d82d/NET35_Namespaces_Poster_LORES.pdf" target="_blank" mce_href="http://download.microsoft.com/download/4/a/3/4a3c7c55-84ab-4588-84a4-f96424a7d82d/NET35_Namespaces_Poster_LORES.pdf"&gt;Download it here&lt;/a&gt;.&amp;nbsp;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Daniel Moth also has a good older post on the &lt;a href="http://www.danielmoth.com/Blog/2007/06/net-framework-35.html" mce_href="http://www.danielmoth.com/Blog/2007/06/net-framework-35.html" target="_blank"&gt;.NET 3.5 bits&lt;/a&gt;&lt;br&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4886899" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/CLR/default.aspx">CLR</category></item><item><title>.NET Framework Libraries Source Code - License - MS-RL</title><link>http://weblogs.asp.net/rajbk/archive/2007/10/03/net-framework-libraries-source-code-license-ms-rl.aspx</link><pubDate>Wed, 03 Oct 2007 18:17:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4322772</guid><dc:creator>rajbk</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=4322772</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2007/10/03/net-framework-libraries-source-code-license-ms-rl.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;When I first read &lt;A class="" href="about:/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx" target=_blank mce_href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx"&gt;Scott's post&lt;/A&gt;, I missed the &lt;A class="" href="http://www.microsoft.com/resources/sharedsource/licensingbasics/referencelicense.mspx" target=_blank mce_href="http://www.microsoft.com/resources/sharedsource/licensingbasics/referencelicense.mspx"&gt;Microsoft Reference License (Ms-RL)&lt;/A&gt; which the .net Framework libraries is released under.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The key&amp;nbsp;thing to remember is this:&lt;/P&gt;
&lt;P mce_keep="true"&gt;"Reference use" means use of the software within your company as a reference, &lt;STRONG&gt;in read only form&lt;/STRONG&gt;, for the sole purposes of debugging your products, maintaining your products, or enhancing the interoperability of your products with the software, and specifically excludes the right to distribute the software outside of your company.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4322772" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>.NET Framework Libraries Source Code</title><link>http://weblogs.asp.net/rajbk/archive/2007/10/03/net-framework-libraries-source-code.aspx</link><pubDate>Wed, 03 Oct 2007 16:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4321703</guid><dc:creator>rajbk</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=4321703</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2007/10/03/net-framework-libraries-source-code.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;Scott just announced that his team will be&amp;nbsp; releasing the souce code to the .NET base class libraries, ASP.net, Windows Forms and WPF!&lt;/p&gt;
&lt;p mce_keep="true"&gt;The big advantage to this is that when you are stepping through the code, you no longer see the horrible "[External Code]' in your call stack. You are taken right to the source code! This really really helps in understanding how the internals of the classes work.&lt;br&gt;&lt;/p&gt;&lt;p mce_keep="true"&gt;Wonder what I am going to do with reflector...hmm... &lt;br&gt;&lt;/p&gt;
&lt;p mce_keep="true"&gt;&lt;a href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx" class="" target="_blank" mce_href="http://weblogs.asp.net/scottgu/archive/2007/10/03/releasing-the-source-code-for-the-net-framework-libraries.aspx"&gt;Read more at Scotts' blog&lt;/a&gt;&lt;br&gt;&lt;br&gt;PS: The source code is being released under &lt;a href="http://www.microsoft.com/resources/sharedsource/licensingbasics/referencelicense.mspx" mce_href="http://www.microsoft.com/resources/sharedsource/licensingbasics/referencelicense.mspx"&gt;Ms-RL&lt;/a&gt; which means that I can view but I'm not allowed to copy and paste the source code.&lt;br&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4321703" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>Microsoft Silverlight</title><link>http://weblogs.asp.net/rajbk/archive/2007/05/01/microsoft-sliverlight.aspx</link><pubDate>Tue, 01 May 2007 21:07:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2473180</guid><dc:creator>rajbk</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=2473180</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2007/05/01/microsoft-sliverlight.aspx#comments</comments><description>&lt;P&gt;I am at MIX 2007 and writing a quick post inbetween classes.&lt;/P&gt;
&lt;P&gt;Very excited about what I have seen so far regarding Sliverlight. Some things I like:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Manipulate the DOM using Silverlight.&lt;/LI&gt;
&lt;LI&gt;Write in C# - which is way better than Actionscript ;-)&lt;/LI&gt;
&lt;LI&gt;Create custom Silverlight controls&lt;/LI&gt;
&lt;LI&gt;Light plugin that installs really fast!!! &lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://www.microsoft.com/silverlight/streaming.aspx" mce_href="http://www.microsoft.com/silverlight/streaming.aspx"&gt;Silverlight&lt;SUP style="FONT-SIZE: 8px"&gt;TM&lt;/SUP&gt; Streaming by Windows Live&lt;SUP style="FONT-SIZE: 8px"&gt;TM&lt;/SUP&gt;&lt;/A&gt;&amp;nbsp; rocks! &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;My only concern is how can we guarantee that the client will install the plugin? There are organizations out there that have locked their active directory using group policies. How can we convince the IT department (those&amp;nbsp;that are concerned about security)&amp;nbsp;to deploy this plugin in across the directory? In addition, public access terminals are also generally locked down which prevents the user from viewing a page developed using Silverlight. IMHO, MS should put the word out there about why this is a safe plugin to install.&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2473180" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/WPF/default.aspx">WPF</category></item><item><title>Page.IsValid and Validate </title><link>http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx</link><pubDate>Thu, 15 Mar 2007 19:16:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2035144</guid><dc:creator>rajbk</dc:creator><slash:comments>21</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=2035144</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx#comments</comments><description>&lt;div class="Section1"&gt;&lt;p&gt;ASP.net ships with a couple of validator controls that allow you to determine whether the value of the input controls they are validating is valid. &lt;/p&gt;&lt;p&gt;Here is a simple example of a TextBox control with a RequiredFieldValidator attached and a Button control. &lt;/p&gt;&lt;div style="border: windowtext 1pt solid"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: #a31515; font-family: 'Courier New'"&gt;asp&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;:&lt;/span&gt;&lt;span style="font-size: 10pt; color: #a31515; font-family: 'Courier New'"&gt;TextBox&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;TextBox1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;RequiredFieldValidator&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;RequiredFieldValidator1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ControlToValidate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;TextBox1&amp;quot;&lt;br /&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: red"&gt;ErrorMessage&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;This field is required!&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;RequiredFieldValidator&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;OnClick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button1_Click&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;Note that all controls belong to the same &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup.aspx" target="_blank"&gt;ValidationGroup&lt;/a&gt;&amp;nbsp; - a new feature of ASP.net 2.0. With JavaScript turned on,&amp;nbsp;when a user clicks the button, they will see an error message displayed next to the control being validated if they fail to fill in the TextBox. (One could also use a &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.aspx" target="_blank"&gt;ValidationSummary&lt;/a&gt; control)&lt;/p&gt;&lt;p&gt;&lt;strong&gt;With JavaScript turned off, what may not be known is that, on the server side, even though the validators fire, it is left to the developer on &lt;em&gt;how&lt;/em&gt; to use that information.&lt;/strong&gt; &amp;nbsp;&lt;br /&gt;&lt;br /&gt;You may think you have built a secure application but a hacker could disable JavaScript and bypass *all* your validators! This is where the &lt;a href="http://msdn2.microsoft.com/en-us/library/0ke7bxeh.aspx" target="_blank"&gt;Page.Validate&lt;/a&gt; method and more importantly, the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.page.isvalid.aspx" target="_blank"&gt;&lt;strong&gt;Page.IsValid&lt;/strong&gt;&lt;/a&gt; property come in. &lt;/p&gt;&lt;p&gt;The Validate method is fired automatically by controls that have the &lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx" target="_blank"&gt;CausesValidation&lt;/a&gt; property set to true. Note that the Button control&amp;#39;s CausesValidation property is true by default. The Validate method iterates through all enabled validation controls on the page and validates them. This event occurs after the Load event in the page lifecycle.&lt;/p&gt;&lt;p&gt;If the control that raised the event has a ValidationGroup specified, then only enabled validator controls that belong to the same ValidationGroup are validated by calling the Page.Validate(ValidationGroup) overload method. As mentioned previously, this is done automatically for controls that have the CausesValidation property set to true.&lt;/p&gt;&lt;p&gt;The Page.IsValid property tells you whether the validation succeeded or not. It can be called only after the Page.Validiate method is called. By using this property, you can add logic to your page to determine whether to proceed with the PostBack event or not. &lt;strong&gt;So, in addition to relying on client side validation, it is also important that you call Page.IsValid when handling the postback event.&lt;/strong&gt; Here is what the code behind will look like:&lt;/p&gt;&lt;div style="border: windowtext 1pt solid"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;&lt;br /&gt;protected&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Button1_Click(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt; e) {&lt;br /&gt;&lt;span style="color: green"&gt;//Not required since the CausesValidation property of the Button control is true by default.&lt;br /&gt;//Page.Validate(&amp;quot;MyValidationGroup&amp;quot;);&lt;br /&gt;&lt;br /&gt;//Proceed only if the vaidation is successfull&lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;if&lt;/span&gt; (!Page.IsValid) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;span style="color: green"&gt;//Insert data into SQL&lt;br /&gt;&lt;/span&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;With the ASP.net declarative programming approach, the Page.IsValid method is something easy to forget.&lt;br /&gt;&lt;br /&gt;Since you have reached this point, here is a simple quiz ;-) In the code below, what happens when Button2 is clicked?&lt;/p&gt;&lt;div style="border: windowtext 1pt solid"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: #a31515; font-family: 'Courier New'"&gt;asp&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;:&lt;/span&gt;&lt;span style="font-size: 10pt; color: #a31515; font-family: 'Courier New'"&gt;TextBox&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;TextBox1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;RequiredFieldValidator&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;RequiredFieldValidator1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ControlToValidate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;TextBox1&amp;quot;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: red"&gt;&amp;nbsp;ErrorMessage&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;This field is required!&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;RequiredFieldValidator&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button1&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;&amp;nbsp;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;CausesValidation&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;TextBox2&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;AnotherValidationGroup&lt;/strong&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;TextBox&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;RequiredFieldValidator&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;RequiredFieldValidator2&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ControlToValidate&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;TextBox2&amp;quot;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: red"&gt;&lt;font color="#000000"&gt;&amp;nbsp;&lt;/font&gt;ErrorMessage&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;This field is required!&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;AnotherValidationGroup&lt;/strong&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;RequiredFieldValidator&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;br&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button&lt;/span&gt; &lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button2&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button&amp;quot;&lt;/span&gt; &lt;span style="color: red"&gt;ValidationGroup&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;AnotherValidationGroup&lt;/strong&gt;&amp;quot;&lt;/span&gt;&amp;nbsp; &lt;span style="color: red"&gt;OnClick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;strong&gt;Button2_Click&lt;/strong&gt;&amp;quot;&lt;/span&gt; &lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt; &lt;p style="border: medium none; padding: 0in"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;protected&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Button2_Click(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt; e) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Page.Validate(&lt;span style="color: #a31515"&gt;&amp;quot;&lt;strong&gt;MyValidationGroup&lt;/strong&gt;&amp;quot;&lt;/span&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (!Page.IsValid) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Response.Write(&lt;span style="color: #a31515"&gt;&amp;quot;Button was clicked at &amp;quot;&lt;/span&gt; + &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.ToShortTimeString());&lt;br /&gt;}&lt;/span&gt; &lt;/p&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;/div&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2035144" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Tips/default.aspx">Tips</category></item><item><title>EditorBrowsable - EditorBrowsableState.Never shows up in Intellisense</title><link>http://weblogs.asp.net/rajbk/archive/2007/03/04/editorbrowsable-editorbrowsablestate-never-shows-up-in-intellisense.aspx</link><pubDate>Mon, 05 Mar 2007 04:05:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:1906880</guid><dc:creator>rajbk</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=1906880</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2007/03/04/editorbrowsable-editorbrowsablestate-never-shows-up-in-intellisense.aspx#comments</comments><description>&lt;p&gt;I had a property in a WebControl marked as &lt;span style="color: #2b91af; font-family: Courier New"&gt;EditorBrowsableState.Never&lt;/span&gt; but it was still showing up in Intellisense. After googling for a long time, I found this &lt;a href="http://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms.controls/browse_thread/thread/2f88b987be3948d0?fwc=1"&gt;&lt;strong&gt;post&lt;/strong&gt;&lt;/a&gt;&amp;nbsp;with a response by Linda Liu. &lt;/p&gt;&lt;p&gt;&lt;em&gt;Speaking for C#, the EditorBrowsableAttribute attribute only influences C#&amp;nbsp; IntelliSense filtering behavior if the decorated item is imported from metadata. &lt;br /&gt;&lt;br /&gt;That&amp;#39;s to say, you should compile the EditorBrowsableAttribute-decorated&amp;nbsp; project (e.g project 1) into an assembly (.dll or .exe) and then in another project (e.g project 2) add a reference to that assembly. In project 2, you should see the attribute at work.&lt;br /&gt;&lt;br /&gt;C# IntelliSense filtering behavior is not influenced if you are coding in project 1. What&amp;#39;s more, if you add a project-to-project reference to project 1 in project 2, C# IntelliSense filtering behavior is not influenced when you are coding in project 2 either.&lt;br /&gt;&lt;br /&gt;The IDE is intended to filter items from consumers of your assembly and not to filter items from yourself as you code the assembly.&lt;/em&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Since I had a project to project reference, intellisense ignored this attribute - &lt;span style="font-size: 7pt"&gt;aargh :-(&lt;/span&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=1906880" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Custom+Controls/default.aspx">Custom Controls</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>GridView DropDownList Pager</title><link>http://weblogs.asp.net/rajbk/archive/2006/08/14/GridView-DropDownList-Pager.aspx</link><pubDate>Mon, 14 Aug 2006 15:18:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:469366</guid><dc:creator>rajbk</dc:creator><slash:comments>51</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=469366</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2006/08/14/GridView-DropDownList-Pager.aspx#comments</comments><description>&lt;p&gt;This post shows you how to add a custom DropDownlist pager and pager buttons&amp;nbsp;to the GridView as shown below:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/47036843@N00/215101374/" title="Photo Sharing"&gt;&lt;img alt="gridview_dropdown_pager" border="0" height="171" src="http://static.flickr.com/83/215101374_1c76f0ded2_o.gif" width="631" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Unlike the behavior of the default pager buttons,&amp;nbsp; these buttons get disabled instead of being hidden (depending on the state of the GridView). &lt;/p&gt;&lt;p&gt;IMHO, buttons that remain at the same position while paging through a resultset makes a better UI (spatial memory). &lt;/p&gt;&lt;p&gt;The code is pretty straightforward. &lt;/p&gt;&lt;p&gt;Add the following pager template to your GridView.&lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&amp;lt;PagerTemplate&amp;gt;Goto Page &lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:DropDownList ID=&lt;span style="color: maroon"&gt;&amp;quot;ddlPageSelector&amp;quot;&lt;/span&gt; runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; AutoPostBack=&lt;span style="color: maroon"&gt;&amp;quot;true&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/asp:DropDownList&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Button Text=&lt;span style="color: maroon"&gt;&amp;quot;First&amp;quot;&lt;/span&gt; CommandName=&lt;span style="color: maroon"&gt;&amp;quot;Page&amp;quot;&lt;/span&gt; CommandArgument=&lt;span style="color: maroon"&gt;&amp;quot;First&amp;quot;&lt;/span&gt; runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ID=&lt;span style="color: maroon"&gt;&amp;quot;btnFirst&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Button Text=&lt;span style="color: maroon"&gt;&amp;quot;Previous&amp;quot;&lt;/span&gt; CommandName=&lt;span style="color: maroon"&gt;&amp;quot;Page&amp;quot;&lt;/span&gt; CommandArgument=&lt;span style="color: maroon"&gt;&amp;quot;Prev&amp;quot;&lt;/span&gt; runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ID=&lt;span style="color: maroon"&gt;&amp;quot;btnPrevious&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Button Text=&lt;span style="color: maroon"&gt;&amp;quot;Next&amp;quot;&lt;/span&gt; CommandName=&lt;span style="color: maroon"&gt;&amp;quot;Page&amp;quot;&lt;/span&gt; CommandArgument=&lt;span style="color: maroon"&gt;&amp;quot;Next&amp;quot;&lt;/span&gt; runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ID=&lt;span style="color: maroon"&gt;&amp;quot;btnNext&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Button Text=&lt;span style="color: maroon"&gt;&amp;quot;Last&amp;quot;&lt;/span&gt; CommandName=&lt;span style="color: maroon"&gt;&amp;quot;Page&amp;quot;&lt;/span&gt; CommandArgument=&lt;span style="color: maroon"&gt;&amp;quot;Last&amp;quot;&lt;/span&gt; runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ID=&lt;span style="color: maroon"&gt;&amp;quot;btnLast&amp;quot;&lt;/span&gt; /&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;lt;/PagerTemplate&amp;gt; &lt;/p&gt;&lt;/div&gt;&lt;p style="margin: 0px"&gt;&lt;br /&gt;Add a RowCreated event handler to your GridView and add the following code:&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; GridView1_RowCreated(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: teal"&gt;GridViewRowEventArgs&lt;/span&gt; e) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (e.Row.RowType == &lt;span style="color: teal"&gt;DataControlRowType&lt;/span&gt;.Pager) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;PresentationUtils&lt;/span&gt;.&lt;strong&gt;SetPagerButtonStates&lt;/strong&gt;(GridView1, e.Row, &lt;span style="color: blue"&gt;this&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;}&lt;/p&gt;&lt;/div&gt;&amp;nbsp; &lt;p style="margin: 0px"&gt;Finally, add the following code to your common class (PresentationUtils in my case)&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; &lt;strong&gt;SetPagerButtonStates&lt;/strong&gt;(&lt;span style="color: teal"&gt;GridView&lt;/span&gt; gridView, &lt;span style="color: teal"&gt;GridViewRow&lt;/span&gt; gvPagerRow, &lt;span style="color: teal"&gt;Page&lt;/span&gt; page) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; pageIndex = gridView.PageIndex;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;int&lt;/span&gt; pageCount = gridView.PageCount;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;Button&lt;/span&gt; btnFirst = (&lt;span style="color: teal"&gt;Button&lt;/span&gt;)gvPagerRow.FindControl(&lt;span style="color: maroon"&gt;&amp;quot;btnFirst&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;Button&lt;/span&gt; btnPrevious = (&lt;span style="color: teal"&gt;Button&lt;/span&gt;)gvPagerRow.FindControl(&lt;span style="color: maroon"&gt;&amp;quot;btnPrevious&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;Button&lt;/span&gt; btnNext = (&lt;span style="color: teal"&gt;Button&lt;/span&gt;)gvPagerRow.FindControl(&lt;span style="color: maroon"&gt;&amp;quot;btnNext&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;Button&lt;/span&gt; btnLast = (&lt;span style="color: teal"&gt;Button&lt;/span&gt;)gvPagerRow.FindControl(&lt;span style="color: maroon"&gt;&amp;quot;btnLast&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; btnFirst.Enabled = btnPrevious.Enabled = (pageIndex != 0);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; btnNext.Enabled = btnLast.Enabled = (pageIndex &amp;lt; (pageCount - 1));&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;DropDownList&lt;/span&gt; ddlPageSelector = (&lt;span style="color: teal"&gt;DropDownList&lt;/span&gt;)gvPagerRow.FindControl(&lt;span style="color: maroon"&gt;&amp;quot;ddlPageSelector&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ddlPageSelector.Items.Clear();&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i = 1; i &amp;lt;= gridView.PageCount; i++) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ddlPageSelector.Items.Add(i.ToString());&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ddlPageSelector.SelectedIndex = pageIndex;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;//Anonymous method (see another way to do this at the bottom)&lt;/strong&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ddlPageSelector.SelectedIndexChanged += &lt;span style="color: blue"&gt;delegate&lt;/span&gt; {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; gridView.PageIndex = ddlPageSelector.SelectedIndex;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; gridView.DataBind();&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; };&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;} &lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;br /&gt;AFAIK, VB.net 2.0 does not support anonymous methods. Therefore, for those of you converting this code to VB.net, you will need to add the handler manually. Go to the GridView Tasks and select &amp;quot;Edit templates&amp;quot;, select the PagerTemplate and double click on the DropDownList to add a index changed event handler. In the event handler, write the VB.net equivalent of this:&lt;/p&gt;&lt;font color="#0000ff" size="2"&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: 'Courier New'"&gt;protected&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt;&lt;font color="#000000"&gt; ddlPageSelector_SelectedIndexChanged(&lt;/font&gt;&lt;span style="color: blue"&gt;object&lt;/span&gt;&lt;font color="#000000"&gt; sender, &lt;/font&gt;&lt;span style="color: #2b91af"&gt;EventArgs&lt;/span&gt;&lt;font color="#000000"&gt; e) {&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt;&lt;font color="#000000"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GridView1.PageIndex = ((&lt;/font&gt;&lt;span style="color: #2b91af"&gt;DropDownList&lt;/span&gt;&lt;font color="#000000"&gt;)sender).SelectedIndex;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal"&gt;&lt;span style="font-size: 10pt; font-family: 'Courier New'"&gt;&lt;font color="#000000"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;GridView1.DataBind();&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 10pt"&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: 'Courier New'"&gt;&lt;font color="#000000"&gt;}&lt;/font&gt;&lt;/span&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;/p&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;/font&gt;&lt;p&gt;Related : &lt;a href="http://weblogs.asp.net/rajbk/archive/2006/08/04/Clickable-GridView-Headers.aspx"&gt;Clickable GridView Headers&lt;/a&gt; .&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=469366" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Data/default.aspx">Data</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/Tips/default.aspx">Tips</category></item><item><title>Clickable GridView Headers</title><link>http://weblogs.asp.net/rajbk/archive/2006/08/04/Clickable-GridView-Headers.aspx</link><pubDate>Fri, 04 Aug 2006 16:40:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:463710</guid><dc:creator>rajbk</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=463710</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2006/08/04/Clickable-GridView-Headers.aspx#comments</comments><description>&lt;p&gt;By default, you can sort on the columns of a GridView by clicking on the LinkButton in the header of a column that has a SortExpression defined. This post is going to attempt three things. &lt;/p&gt;&lt;ol&gt;&lt;li&gt;Sort the column by clicking anywhere in the header cell&lt;/li&gt;&lt;li&gt;Add a sort indicator to cells that are sortable. Cells that are sortable will have a &amp;quot;neutral&amp;quot; icon. Note in the screen capture below that UnitsInStock does not have a &amp;quot;neutral&amp;quot; icon because it does not have a SortExpression defined.&lt;/li&gt;&lt;li&gt;Highlight the column of data that is being sorted.&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;&lt;a href="http://www.flickr.com/photos/47036843@N00/206576417/" title="Photo Sharing"&gt;&lt;img src="http://static.flickr.com/58/206576417_9fd01add99_o.gif" border="0" alt="gridview_clickable_headers" width="628" height="203" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;The idea is to add an onclick attribute that calls a javascript function to all GridView header cells that are sortable. &lt;/p&gt;&lt;p&gt;In the javscript function, we determine which element raised the event. If the element that raised the event is not the LinkButton, we find the LinkButton and invoke its onclick or href attribute method based on the GridView EnableSortingAndPagingCallbacks property. When the EnableSortingAndPagingCallbacks is set to true, we invoke the onclick method otherwise we invoke the method the href attribute points to. &lt;/p&gt;&lt;p&gt;We are checking which element raised the event in the javscript function to prevent the postback or callback from being called twice. This is because, without this check, clicking on the LinkButton will fire the method as well as bubble the event up to its parent cell causing it to be fired again (since we have added an onclick attribute to the cell). The javascript function calls the postback or callback method only if the element that raised the event is not the LinkButton. &lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; &lt;strong&gt;MakeGridViewHeaderClickable&lt;/strong&gt;(&lt;span style="color: teal"&gt;GridView&lt;/span&gt; gridView, &lt;span style="color: teal"&gt;GridViewRow&lt;/span&gt; gridViewRow) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (gridViewRow.RowType == &lt;span style="color: teal"&gt;DataControlRowType&lt;/span&gt;.Header) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i = 0; i &amp;lt; gridView.Columns.Count; i++) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; sortExpression = gridView.Columns[i].SortExpression;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: teal"&gt;TableCell&lt;/span&gt; tableCell = gridViewRow.Cells[i];&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: green"&gt;//Make sure the column we are working with has a sort expression&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(sortExp ression)) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Enumerate the controls within the current cell and find the link button.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: teal"&gt;Control&lt;/span&gt; gridViewRowCellControl &lt;span style="color: blue"&gt;in&lt;/span&gt; gridViewRow.Cells[i].Controls) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;LinkButton&lt;/span&gt; linkButton = gridViewRowCellControl &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: teal"&gt;LinkButton&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; ((linkButton != &lt;span style="color: blue"&gt;null&lt;/span&gt;) &amp;amp;&amp;amp; (linkButton.CommandName == &lt;span style="color: maroon"&gt;&amp;quot;Sort&amp;quot;&lt;/span&gt;)) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; &lt;span style="color: green"&gt;//Add an onclick attribute to the current cell&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; tableCell.Attributes.Add(&lt;span style="color: maroon"&gt;&amp;quot;onclick&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;RequestData(&amp;#39;&amp;quot;&lt;/span&gt; + linkButton.ClientID + &lt;span style="color: maroon"&gt;&amp;quot;&amp;#39;, this, event)&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; tableCell.Style.Add(&lt;span style="color: teal"&gt;HtmlTextWriterStyle&lt;/span&gt;.Cursor, &lt;span style="color: maroon"&gt;&amp;quot;hand&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; tableCell.Style.Add(&lt;span style="color: teal"&gt;HtmlTextWriterStyle&lt;/span&gt;.Cursor, &lt;span style="color: maroon"&gt;&amp;quot;pointer&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; &lt;span style="color: blue"&gt;break&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; GridView1_RowDataBound(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;span style="color: teal"&gt;GridViewRowEventArgs&lt;/span&gt; e) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;MakeGridViewHeaderClickable&lt;/strong&gt;(GridView1, e.Row);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/p&gt;&lt;/div&gt;&lt;!--endfragment--&gt;&lt;p&gt;The javascript function is shown below &lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;script language=&lt;span style="color: maroon"&gt;&amp;quot;javascript&amp;quot;&lt;/span&gt; type=&lt;span style="color: maroon"&gt;&amp;quot;text/javascript&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; function &lt;strong&gt;RequestData&lt;/strong&gt;(linkId, cellElement, evt) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var evtSource;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; evt = (evt)? evt : window.&lt;span style="color: blue"&gt;event&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; evtSource = (evt.srcElement)? evt.srcElement : evt.target;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//When a hyperlink is clicked, Safari returns the text node as the source element rather&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//than the hyperlink. parentNode will give us the hyperlink element.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//ref: http://developer.apple.com/internet/webcontent/eventmodels.html&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (evt.target) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (evt.target.nodeType == 3) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; evtSource = evtSource.parentNode;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//If event was raised from an element other than the LinkButton&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; ((evtSource.getAttribute(&lt;span style="color: maroon"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;) != linkId) &amp;amp;&amp;amp; (evt.type == &lt;span style="color: maroon"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;)) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Get a collection of &amp;quot;a&amp;quot; tags inside the cell&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; var linkCollection = cellElement.getElementsByTagName(&lt;span style="color: maroon"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (var i = 0; i &amp;lt; linkCollection.length; i++) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//If the link button has an onclick attribute, call the onclick.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//The onclick attribute is present when the GridView is using callback&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//example: onclick=&amp;quot;java script:__gvGridSort1_GridView1.callback(...); return false;&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; var onClickAttribute = linkCollection[i].getAttribute(&lt;span style="color: maroon"&gt;&amp;quot;onclick&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: blue"&gt;if&lt;/span&gt; (onClickAttribute != &lt;span style="color: blue"&gt;null&lt;/span&gt;) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; linkCollection[i].onclick();&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: blue"&gt;break&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&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; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//If the link button has a href attribute, set the location of the page&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//to the href value.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//The href attribute is used when the GridView is not using callbacks&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: green"&gt;//example: href=&amp;quot;java script:__doPostBack(&amp;#39;GridSort1$GridView1&amp;#39;,&amp;#39;Sort$UnitsOnOrder&amp;#39;)&amp;quot; &lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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; var hrefAttribute = linkCollection[i].getAttribute(&lt;span style="color: maroon"&gt;&amp;quot;href&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: blue"&gt;this&lt;/span&gt;.location.href = hrefAttribute;&lt;/p&gt;&lt;p style="margin: 0px"&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; &lt;span style="color: blue"&gt;break&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/script&amp;gt;&lt;/p&gt;&lt;/div&gt;&lt;!--endfragment--&gt;&lt;p&gt;If you run the above code, you will notice that it is different from screen capture above because the sort image and column highlighting are missing. To implement this, we need to add some code to the MakeGridViewHeaderClickable method as shown below.&lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; &lt;strong&gt;MakeGridViewHeaderClickable&lt;/strong&gt;(&lt;span style="color: teal"&gt;GridView&lt;/span&gt; gridView, &lt;span style="color: teal"&gt;GridViewRow&lt;/span&gt; gridViewRow) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (gridViewRow.RowType == &lt;span style="color: teal"&gt;DataControlRowType&lt;/span&gt;.Header) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i = 0; i &amp;lt; gridView.Columns.Count; i++) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; sortExpression = gridView.Columns[i].SortExpression;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: teal"&gt;TableCell&lt;/span&gt; tableCell = gridViewRow.Cells[i];&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: green"&gt;//Make sure the column we are working with has a sort expression&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (!&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrEmpty(sortExp ression)) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.&lt;span style="color: teal"&gt;Image&lt;/span&gt; sortDirectionImageControl;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Create an instance of a Image WebControl&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sortDirectionImageControl = &lt;span style="color: blue"&gt;new&lt;/span&gt; System.Web.UI.WebControls.&lt;span style="color: teal"&gt;Image&lt;/span&gt;();&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Determine the image url based on the SortDirection&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; imageUrl = &lt;span style="color: maroon"&gt;&amp;quot;~/Images/sort_neutral.gif&amp;quot;&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (sortExpression == gridView.SortExpression) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; imageUrl = (gridView.SortDirection == &lt;span style="color: teal"&gt;SortDirection&lt;/span&gt;.Ascending) ?&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; &lt;span style="color: maroon"&gt;&amp;quot;~/Images/sort_asc.gif&amp;quot;&lt;/span&gt; : &lt;span style="color: maroon"&gt;&amp;quot;~/Images/sort_desc.gif&amp;quot;&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Add the Image Web Control to the cell&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sortDirectionImageControl.ImageUrl = imageUrl;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sortDirectionImageControl.Style.Add(&lt;span style="color: teal"&gt;HtmlTextWriterStyle&lt;/span&gt;.MarginLeft, &lt;span style="color: maroon"&gt;&amp;quot;10px&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tableCell.Wrap = &lt;span style="color: blue"&gt;false&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; tableCell.Controls.Add(sortDirectionImageControl);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Enumerate the controls within the current cell and find the link button.&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: teal"&gt;Control&lt;/span&gt; gridViewRowCellControl &lt;span style="color: blue"&gt;in&lt;/span&gt; gridViewRow.Cells[i].Controls) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;LinkButton&lt;/span&gt; linkButton = gridViewRowCellControl &lt;span style="color: blue"&gt;as&lt;/span&gt; &lt;span style="color: teal"&gt;LinkButton&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; ((linkButton != &lt;span style="color: blue"&gt;null&lt;/span&gt;) &amp;amp;&amp;amp; (linkButton.CommandName == &lt;span style="color: maroon"&gt;&amp;quot;Sort&amp;quot;&lt;/span&gt;)) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; &lt;span style="color: green"&gt;//Add an onclick attribute to the current cell&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; tableCell.Attributes.Add(&lt;span style="color: maroon"&gt;&amp;quot;onclick&amp;quot;&lt;/span&gt;, &lt;span style="color: maroon"&gt;&amp;quot;RequestData(&amp;#39;&amp;quot;&lt;/span&gt; + linkButton.ClientID + &lt;span style="color: maroon"&gt;&amp;quot;&amp;#39;, this, event)&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; tableCell.Style.Add(&lt;span style="color: teal"&gt;HtmlTextWriterStyle&lt;/span&gt;.Cursor, &lt;span style="color: maroon"&gt;&amp;quot;hand&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; tableCell.Style.Add(&lt;span style="color: teal"&gt;HtmlTextWriterStyle&lt;/span&gt;.Cursor, &lt;span style="color: maroon"&gt;&amp;quot;pointer&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&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;&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; &lt;span style="color: blue"&gt;break&lt;/span&gt;;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;//Enuerate all the rows and change the color of the column that is being sorted&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; (gridViewRow.RowType == &lt;span style="color: teal"&gt;DataControlRowType&lt;/span&gt;.DataRow) {&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;for&lt;/span&gt; (&lt;span style="color: blue"&gt;int&lt;/span&gt; i = 0; i &amp;lt; gridViewRow.Cells.Count; i++) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; &lt;span style="color: blue"&gt;if&lt;/span&gt; ((!&lt;span style="color: teal"&gt;String&lt;/span&gt;.IsNullOrEmpty(gridView.SortExpr ession)) &amp;amp;&amp;amp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (gridView.Columns[i].SortExp ression == gridView.SortExp ression)) {&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;Color&lt;/span&gt; sortColumnBgColor;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sortColumnBgColor = (gridViewRow.RowState != &lt;span style="color: teal"&gt;DataControlRowState&lt;/span&gt;.Alternate) ?&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: teal"&gt;ColorTranslator&lt;/span&gt;.FromHtml(&lt;span style="color: maroon"&gt;&amp;quot;#d7e3f1&amp;quot;&lt;/span&gt;) : &lt;span style="color: teal"&gt;ColorTranslator&lt;/span&gt;.FromHtml(&lt;span style="color: maroon"&gt;&amp;quot;#f7fbff&amp;quot;&lt;/span&gt;);&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gridViewRow.Cells[i].BackColor = sortColumnBgColor;&lt;/p&gt;&lt;p style="margin: 0px"&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;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;/div&gt;&lt;!--endfragment--&gt;&lt;p&gt;You can also use this with HeaderTemplates. Just make sure your HeaderTemplate contains a LinkButton with a CommandName attribute set to &amp;quot;Sort&amp;quot; as shown below:&lt;/p&gt;&lt;div style="font-size: 8pt; background: white; color: black; font-family: Courier New"&gt;&lt;p style="margin: 0px"&gt;&amp;lt;asp:TemplateField HeaderText=&lt;span style="color: maroon"&gt;&amp;quot;ProductID&amp;quot;&lt;/span&gt; SortExpression=&lt;span style="color: maroon"&gt;&amp;quot;ProductID&amp;quot;&lt;/span&gt;&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;ItemTemplate&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:Label ID=&lt;span style="color: maroon"&gt;&amp;quot;Label1&amp;quot;&lt;/span&gt; runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; Text=&lt;span style="color: maroon"&gt;&amp;#39;&amp;lt;%# Bind(&amp;quot;ProductID&amp;quot;) %&amp;gt;&amp;#39;&lt;/span&gt;&amp;gt;&amp;lt;/asp:Label&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/ItemTemplate&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;HeaderTemplate&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;asp:LinkButton runat=&lt;span style="color: maroon"&gt;&amp;quot;server&amp;quot;&lt;/span&gt; CommandName=&lt;span style="color: maroon"&gt;&amp;quot;Sort&amp;quot;&lt;/span&gt; CommandArgument=&lt;span style="color: maroon"&gt;&amp;quot;ProductID&amp;quot;&lt;/span&gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Text=&lt;span style="color: maroon"&gt;&amp;quot;ProductID&amp;quot;&lt;/span&gt;&amp;gt;&amp;lt;/asp:LinkButton&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/HeaderTemplate&amp;gt;&lt;/p&gt;&lt;p style="margin: 0px"&gt;&amp;lt;/asp:TemplateField&amp;gt;&lt;/p&gt;&lt;/div&gt;&lt;!--EndFragment--&gt;&lt;p&gt;You can easily make these transparent sort icons by working at the pixel level ( I used Photoshop)&lt;br /&gt;&lt;a href="http://www.flickr.com/photos/47036843@N00/206576418/" title="Photo Sharing"&gt;&lt;img src="http://static.flickr.com/72/206576418_4d7af2140d_o.gif" border="0" alt="sort_asc" width="10" height="9" /&gt;&lt;/a&gt; &lt;a href="http://www.flickr.com/photos/47036843@N00/206576420/" title="Photo Sharing"&gt;&lt;img src="http://static.flickr.com/57/206576420_c9275d69db_o.gif" border="0" alt="sort_desc" width="10" height="9" /&gt;&lt;/a&gt; &lt;a href="http://www.flickr.com/photos/47036843@N00/206576422/" title="Photo Sharing"&gt;&lt;img src="http://static.flickr.com/77/206576422_0a99a7340b_o.gif" border="0" alt="sort_neutral" width="10" height="9" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The &lt;span style="margin: 0px"&gt;MakeGridViewHeaderClickable&lt;/span&gt; method can be placed in a Utility class if you so desire. A reference to the GridView can be obtained by using &lt;font face="Courier New" style="font-size: 8pt"&gt;gridViewRow.Parent.Parent or (GridView) gridViewRow.NamingContainer &lt;/font&gt;if you don&amp;#39;t want to pass in a GridView reference to the method.&lt;/p&gt;&lt;p&gt;Tested on IE 6.0.2900, Firefox 1.5.0.6 and Safari 1.3.2&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note: I had to add a space in some variables because Community Server (running this blog) was blocking my scripts. Example: sortExp ression.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=463710" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/rajbk/archive/tags/ASP.NET/default.aspx">ASP.NET</category></item><item><title>Virtual Sever and Port Security</title><link>http://weblogs.asp.net/rajbk/archive/2006/05/15/446531.aspx</link><pubDate>Mon, 15 May 2006 22:01:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:446531</guid><dc:creator>rajbk</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=446531</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2006/05/15/446531.aspx#comments</comments><description>&lt;p&gt;&lt;font face="Verdana" size="2"&gt;If you are running Virtual Server and your Virtual Machine is having trouble obtaining an IP&amp;nbsp;Address from your DHCP server, it could be because you have port security turned on on your&amp;nbsp;switch.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Verdana" size="2"&gt;AFAIK, when Virtual sever is directly connected to the host network or if it is configured to&amp;nbsp;perform NAT, it configures the physical network adapter to run in Promiscuous mode. More importantly, as &lt;/font&gt;&lt;a href="http://blogs.msdn.com/virtual_pc_guy/archive/2005/04/01/404816.aspx "&gt;&lt;font face="Verdana" size="2"&gt;described here&lt;/font&gt;&lt;/a&gt;&lt;font face="Verdana" size="2"&gt;, outbound packets from the Virtual machine will contain the MAC address of the Virtual machine.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Verdana" size="2"&gt;So, you now have a NIC advertising two MAC addresses (the host os and the VM). When you have port security turned on, the switch will ignore packets from the second MAC address.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Verdana" size="2"&gt;To fix this, go to your switch and either turn off port security for the port used by your&amp;nbsp; physical network adapter or increase the max number of mac addesses to secure on&amp;nbsp;the port.&lt;/font&gt;&lt;/p&gt; &lt;p&gt;&lt;font face="Verdana" size="2"&gt;I did not find any&amp;nbsp;info about this on Google which lead me to post this. If any one has&amp;nbsp;more information, please post here.&lt;/font&gt; &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=446531" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Health monitoring in asp.net 2.0 - logging events</title><link>http://weblogs.asp.net/rajbk/archive/2006/02/09/437846.aspx</link><pubDate>Fri, 10 Feb 2006 00:18:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:437846</guid><dc:creator>rajbk</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/rajbk/rsscomments.aspx?PostID=437846</wfw:commentRss><comments>http://weblogs.asp.net/rajbk/archive/2006/02/09/437846.aspx#comments</comments><description>&lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana" size="2"&gt;I was about to use the &lt;/font&gt;&lt;a href="http://msdn.microsoft.com/library/en-us/dnpag2/html/EntLibJan2006_LoggingAppBlock.asp?frame=true"&gt;&lt;font face="Verdana" size="2"&gt;Logging Application Block&lt;/font&gt;&lt;/a&gt;&lt;font face="Verdana" size="2"&gt; of the Enterprise Library 2.0 &lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;when I found out about the built in Health Monitoring feature in ASP.net 2.0. IMHO, the Logging block is great for an enterprise app but might be overkill for smaller apps.&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;font face="Verdana" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana" size="2"&gt;With just a few lines in web.config, you can easily add health monitoring to your application as described &lt;/font&gt;&lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/PAGHT000011.asp"&gt;&lt;font face="Verdana" size="2"&gt;here&lt;/font&gt;&lt;/a&gt;&lt;font face="Verdana" size="2"&gt;.&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;br /&gt;&lt;font face="Verdana" size="2"&gt;You get the following providers out of the box:&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;font face="Verdana" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana"&gt;&lt;font size="2"&gt;SimpleMailWebEventProvider&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana"&gt;&lt;font size="2"&gt;TemplatedMailWebEventProvider&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana"&gt;&lt;font size="2"&gt;SqlWebEventProvider&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana"&gt;&lt;font size="2"&gt;EventLogWebEventProvider&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana"&gt;&lt;font size="2"&gt;TraceWebEventProvider&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana" size="2"&gt;WmiWebEventProvider&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;font face="Verdana" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana" size="2"&gt;MSDN even has an example of &lt;/font&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms227676.aspx"&gt;&lt;font face="Verdana" size="2"&gt;how to create a custom provider that writes specified health monitoring event information to a local file&lt;/font&gt;&lt;/a&gt;&lt;font face="Verdana" size="2"&gt;.&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;font face="Verdana" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;font face="Verdana" size="2"&gt;&amp;nbsp;&lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;font face="Verdana"&gt;&lt;font size="2"&gt;Very cool &lt;span style="FONT-FAMILY: Wingdings; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;&lt;span style="mso-char-type: symbol; mso-symbol-font-family: Wingdings"&gt;J&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="MARGIN: 0in 0in 0pt"&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=437846" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/rajbk/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>