<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kerry D. Wong &#187; ASP.Net</title>
	<atom:link href="http://www.kerrywong.com/tag/aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrywong.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 00:51:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>An Obscure System.Web.Mail Error Message</title>
		<link>http://www.kerrywong.com/2008/09/09/an-obscure-systemwebmail-error-message/</link>
		<comments>http://www.kerrywong.com/2008/09/09/an-obscure-systemwebmail-error-message/#comments</comments>
		<pubDate>Wed, 10 Sep 2008 01:08:05 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/?p=345</guid>
		<description><![CDATA[Today I encountered a rather obscure error while maintaining some old ASP.Net code (Framework 1.1). One of our mail servers is going to be replaced with another one. So naturally we needed to change configurations for those applications that send emails using the old server. This change affected one of the applications I wrote a [...]]]></description>
			<content:encoded><![CDATA[<p>Today I encountered a rather obscure error while maintaining some old ASP.Net code (Framework 1.1).<span id="more-345"></span> One of our mail servers is going to be replaced with another one. So naturally we needed to change configurations for those applications that send emails using the old server.</p>
<p>This change affected one of the applications I wrote a while ago. Since I stored all the configuration information inside the web.config file, I thought it was just a simple matter of changing the server from &#8216;server1&#8242; to &#8216;server2&#8242; and then I was done. Well, as it turned out, the result was totally unexpected.</p>
<p>Upon finishing the configuration change, I decided to test the application even though I was sure that everything would be working just fine. I was wrong, instead of getting the expected results, I got the following error message while trying to send email through the application:</p>
<p><strong>Exception:</strong><br />
<span style="color: #800000;">Could not access &#8216;CDO.Message&#8217; object.</span></p>
<p><strong>InnerException:</strong><br />
<span style="color: #800000;">Exception has been thrown by the target of an invocation.InnerException:<br />
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)<br />
at System.RuntimeType.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParameters)<br />
at System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)<br />
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Type type, Object obj, String methodName, Object[] args)<br />
at System.Web.Mail.LateBoundAccessHelper.CallMethod(Object obj, String methodName, Object[] args)</span></p>
<p>Since the only thing changed was the server configuration, I thought that it was definitely a configuration problem of the new mail server. But that could not have been the problem as many applications had already been migrated over and they were working just fine.</p>
<p>After a Google search, it became apparent to me that many people out there are having <a href="http://www.systemwebmail.com/">the same issue</a>. But no information seemed to point to a definitive answer. And the tricks mentioned in various articles did not solve my particular problem. After some struggle, I found out that the culprit was actually my <strong>MailMessage.From</strong> field. Instead of an actual email address, I had always used some random names (e.g. system notification, etc.). And apparently, the spaces in the From field string would cause the error I mentioned above. And if I remove all the spaces in the From field, the exception goes away. Since the <strong>SmtpMail</strong> object is simply a wrapper for the Win32 CDO (Collaborative Data Objects), the error was poorly propagated back (like many of the errors in .Net as I mentioned previously. See <a href="/2008/01/11/the-elusive-filecopy/">1</a>, <a href="/2007/11/15/understanding-a-generic-error-occurred-in-gdi-error/">2</a>, <a href="/2007/10/23/lousy-gdi-error-messages/">3</a>).</p>
<p>Indeed, had the error message been something more clear like &#8220;<em>Error: From field must not contain any space</em>&#8220;, I would not have spent the good portion of the afternoon hunting for bugs (I still think it must have something to do with the new mail server&#8217;s configuration, since this code has been working with other mail servers).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/09/09/an-obscure-systemwebmail-error-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the GridView — Part IV</title>
		<link>http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/</link>
		<comments>http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/#comments</comments>
		<pubDate>Mon, 18 Aug 2008 02:56:42 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/</guid>
		<description><![CDATA[In the previous three posts(I, II, III), I showed you how to inherit from GridView control to create an extended control that can bind to a generic list of objects. In this final post of the series, I will show you a simple example to illustrate how to use the GridViewEx control. 4. Putting it [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous three posts(<a href="/2008/08/08/extending-the-gridview-part-i/">I</a>,<a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/"> II</a>, <a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">III</a>), I showed you how to inherit from GridView control to create an extended control that can bind to a generic list of objects. In this final post of the series, I will show you a simple example to illustrate how to use the GridViewEx control.<span id="more-327"></span></p>
<p><strong>4. Putting it all together</strong></p>
<p>In fact, using an extended control isn&#8217;t that much different than using the stock control thanks to inheritance.&nbsp; For the most part, the extended control works exactly the same as the standard GridView control. I wanted to just touch base on a couple of places that a lot of people are having problem with.</p>
<p>First is the asp tag prefix issue. By default, all the custom controls (*.ascx) will automatically show up in the Toolbox strip within Vistuo Studio. So using the extended GridView is a simple matter of drag and drop. However, the default prefix for custom control is <em>cc1</em>, and depending on how many pre-existing custom controls there are on a page, it could be <em>cc{num}</em>. This becomes hard for replacing existing GridViews with the extended GridViewEx. To resolve this naming issue, we can register the assembly which contains the extended control within system.web-&gt;pages-&gt;controls tag in web.config:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">add</span><span style="color: blue;"> </span><span style="color: red;">assembly</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">ExtendedGridView</span>&quot;<span style="color: blue;"> </span><span style="color: red;">namespace</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">ExtendedGridView</span>&quot;<span style="color: blue;"> </span><span style="color: red;">tagPrefix</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">ExtendedGridView</span>&quot;<span style="color: blue;">/&gt;</span></div>
</div>
<p>With this prefix defined, we can use the tag ExtendedGridView wherever GridView is expected. By registering the prefix, we have also provided the solution to a second problem people often run into when using extended web controls&#8211;namely the ability to &quot;skin&quot; the control. With the extended control prefix registered, we can now create skins the same way as we do with other controls. For instance, my default.skin might look like:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: rgb(163, 21, 21);">ExtendedGridView:GridViewEx</span><span style="color: blue;"> </span><span style="color: red;">runat</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">server</span>&quot;<span style="color: blue;"> </span><span style="color: red;">SkinID</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Professional</span>&quot;<span style="color: blue;"> </span><span style="color: red;">Font-Name</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Arial</span>&quot;<span style="color: blue;"> </span><span style="color: red;">Font-Size</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">9pt</span>&quot;<span style="color: blue;"> </span><span style="color: red;">Cellpadding</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">1</span>&quot;<span style="color: blue;"> </span><span style="color: red;">HeaderStyle-BackColor</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Gray</span>&quot;<span style="color: blue;"> </span><span style="color: red;">HeaderStyle-ForeColor</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">White</span>&quot;<span style="color: blue;"> </span><span style="color: red;">AlternatingRowStyle-BackColor</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">LightGray</span>&quot;<span style="color: blue;"> /&gt;</span></div>
</div>
<p>In this way, the theming issue of the extended GridView is addressed.</p>
<p>The code for the code behind page to display the extended GridView is trivial:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewPager1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedIndexChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>PagerSelectedIndexChanged;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSourceChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> GridViewPager1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GridViewDataSourceChanged;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(128, 128, 192); font-weight: bold;">!</span>Page<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>IsPostBack)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">SampleDataProvider</span> provider <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">SampleDataProvider</span>();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> provider<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetSampleData();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> CBSelected_CheckedChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span> cbSel <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> sender <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (cbSel <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataControlFieldCell</span> cellObj <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cbSel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">DataControlFieldCell</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRow</span> rowObj <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cellObj<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRow</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEx</span> gv <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> rowObj<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEx</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> v <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; v[gv<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Index(rowObj<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowIndex)]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Selected <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cbSel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Checked;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> v;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridViewEx1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>As you can see,&nbsp; the code is pretty much in line with what we would use for a typical GridView control. When the page is posted back, we set the DataSource and bind the data to the grid. Everything else happens transparently behind the scene. To illustrate the capability of GridViewEx, I included a CheckBox at the end of each row, whenever the check status is changed (CBSelected_CheckedChanged) we dynamically obtain the reference to the GridViewEx object (as an alternative, we could have used the actual control (GridViewEx1) instead of going through the trouble of dynamically converting if from the sender object since GridViewEx1 exists at compile time. Should the control be created dynamically, the code here will come in handy). Please pay special attention to how the checked state is determined. Because paging might be involved when the bound list has more entries than can be displayed, the current row index on the screen might not be the same as the row index in the list. To simplify coding, I created an Index function within GridViewEx to translate the selected row index into what is stored in the list.</p>
<p align="center"><img src="/blog/wp-content/uploads/2008/08/gridviewex.jpg" alt="" /></p>
<p>The code samples provided in this series can be downloaded <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">here</a>.</p>
<p>Of course, there are a lot more fancy things you can do with the grid or with any objects in that matter. The only limit is your imagination&#8230;</p>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/17/extending-the-gridview-%e2%80%94-part-iv/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extending the GridView — Part III</title>
		<link>http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/</link>
		<comments>http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/#comments</comments>
		<pubDate>Sun, 17 Aug 2008 00:04:44 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/</guid>
		<description><![CDATA[In Part II, I showed you how to extend the GridView control so that we can bind a generic list to it. Because GridViewEx inherits from GridView, it has all the functionalities GridView has (i.e. paging). Sometimes, it is desirable to change the number of items displayed on a page dynamically, to accommodate this, we [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Part II</a>, I showed you how to extend the GridView control so that we can bind a generic list to it. Because GridViewEx inherits from GridView, it has all the functionalities GridView has (i.e. paging). Sometimes, it is desirable to change the number of items displayed on a page dynamically, to accommodate this, we will create a GridViewPager control (ascx).<span id="more-326"></span></p>
<p><strong>3. GridViewPager</strong></p>
<p>&nbsp;Here is the code for GridViewPager:</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">partial</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewPager</span> : System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Web<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>UI<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserControl</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">delegate</span> <span style="color: blue; font-weight: bold;">void</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">SelectedIndexChangedHandler</span>(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">int</span> pageSize);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">event</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">SelectedIndexChangedHandler</span> SelectedIndexChanged;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">bool</span> _visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">int</span> _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">10</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">bool</span> Visible</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[UniqueID] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>TryParse(ViewState[<span style="color: blue; font-weight: bold;">this</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>UniqueID]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString(), <span style="color: blue; font-weight: bold;">out</span> _visible);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> _visible;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[UniqueID] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> _visible;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (_visible)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MainPanel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MainPanel<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> PageSize</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(UniqueID, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;PageSize&quot;</span>)] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedValue);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(ViewState[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(UniqueID, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;PageSize&quot;</span>)]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString());</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> _pageSize;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _pageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[<span style="color: rgb(43, 145, 175); font-weight: bold;">String</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(UniqueID, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;PageSize&quot;</span>)] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnInit(<span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedIndexChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">EventHandler</span>(DropDownItemsPerPage_SelectedIndexChanged);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnInit(e);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> DropDownItemsPerPage_SelectedIndexChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SelectedValue);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (SelectedIndexChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SelectedIndexChanged(sender, PageSize);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(128, 128, 192); font-weight: bold;">!</span>Page<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>IsPostBack)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownItemsPerPage<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> This method links to the DataSourceChangedEvent defined in GridViewEx so that</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> we can decide whether or not to show the pager depending on whether the list</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> to be bound is empty.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name=&quot;sender&quot;&gt;</span><span style="color: green;">sender object</span><span style="color: gray;">&lt;/param&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name=&quot;isEmpty&quot;&gt;</span><span style="color: green;">whether the list to be bound is empty</span><span style="color: gray;">&lt;/param&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> GridViewDataSourceChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">bool</span> isEmpty)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (isEmpty)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Visible <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>As you can see, the pager control is quite simple. It communicates with GridViewEx control by the SelectedIndexChanged event. Depending on whether the state of number of items per page needs to persist between post backs, either ViewState or SessionState can be used.</p>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/16/extending-the-gridview-%e2%80%94-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the GridView — Part II</title>
		<link>http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/</link>
		<comments>http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 00:10:53 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/</guid>
		<description><![CDATA[In Part I, I talked about why we needed to extend the GridView and some of the benefit in doing that. In this post, I will discuss how to extend the standard GridView control to take advantage of binding to the type safe value objects mentioned previously. 2. GridViewEx To make the binding as generic [...]]]></description>
			<content:encoded><![CDATA[<p>In<a href="/2008/08/08/extending-the-gridview-part-i/"> Part I</a>, I talked about why we needed to extend the GridView and some of the benefit in doing that. In this post, I will discuss how to extend the standard GridView control to take advantage of binding to the type safe value objects mentioned previously.<span id="more-325"></span></p>
<p><strong>2. GridViewEx</strong><br />
To make the binding as generic as possible, there are pretty much two ways. One way is to bind to an object collection of typed value objects. Since all value objects can be cast into objects, we could manipulate them generically. And because value objects are typically consisted of simple types, we could encapsulate all the operations needed for the GridView within the extended GridView itself. </p>
<p>Another solution is to utilize generics in .Net 2.0 and above. One obvious advantage is that the type information is not lost at any point of the object manipulation and thus a lot of small mistakes can be caught at compile time. This approach does have its draw backs. As you will see, because the purpose of generic types are to eliminate casting introduced by object containers, we have to change the extended GridView to accommodate the type of objects we are binding to the list whenever a new generic type is introduced. This of course requires the changing of the extended GridView control itself whenever we add a new generic type and thus might not be as convenient as the pure object oriented approach mentioned previously. However, weighing both the advantages and disadvantages I personally think this approach is more attractive. </p>
<p>So here, I will show you how to extend the GridView to handle generic lists.</p>
<p>GridViewEx inherits from GridView. We override the DataSource property so that we get a chance to inspect it before the databinding actually taking place. This gives us an opportunity to test whether the data source to be bound is empty. We use this information to decide whether to show the pager or not as you will see in a later post.</p>
<p>The initialization routine (OnInit) is overridden to setup some property values and hook up some events so that we provide row editing functionalities.</p>
<p>The RowEditing, PageSelectedIndexChanged and some of the other overridden functions are handled in a pretty standard way. To demonstrate how easy it is to add new functionality to the GridView, I added an event handler for RowDataBound. The purpose of the code for this RowDataBound event is to check if the CheckBox on a particular row is checked and if so, the background color of that row is changed. This behavior is particularly helpful for giving a visual cue to the end user which row of data he/she is currently dealing with. </p>
<p>When each row is created, OnRowCreated method is called, here we determine whether the row being created is the header or not and if so, we add an arrow to indicate the current sorting option depending on how the grid is currently sorted. If the row being created is a data row, we add some nice touch so that the data row becomes highlighted when mouse moves pass it.</p>
<p>We also have to override the OnSorting function to handle our generic lists. This is where we need to add the supported list types (see comments in code). For additional generic list types, we simply need to add three lines for each type of the list. While this might not be as convenient as using generic objects, all the changes are kept in a central place so that adding/removing types should not be an issue.</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;"><span style="color: blue; font-weight: bold;">namespace</span> ExtendedGridView</div>
<div style="margin: 0px;">{</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Extended GridView</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Supports paging/sorting using a customized object data source.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> http://www.kerrywong.com/</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEx</span> : <span style="color: rgb(43, 145, 175); font-weight: bold;">GridView</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">delegate</span> <span style="color: blue; font-weight: bold;">void</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">DataSourceChangedEventHandler</span>(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">bool</span> isEmpty);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">event</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">DataSourceChangedEventHandler</span> DataSourceChanged;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> A readonly unique ID for the current view.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> CurrentViewID</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentView_&quot;</span>, UniqueID); }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Current page id when paging is used.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> To remember the page index user selected, a session variable is used.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> CurrentPageID</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentPageID_&quot;</span>, UniqueID)] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">int</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Parse(<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentPageID_&quot;</span>, UniqueID)]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString());</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">HttpContext</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Current<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Session[<span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Concat(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;CurrentPageID_&quot;</span>, UniqueID)] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Overrides the data source property, so that we get a chance to test whether the source </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> being bound contains data or not.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> This is used to communicate with the pager so that for an empty list no pager is shown.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">object</span> DataSource</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: blue; font-weight: bold;">value</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (DataSourceChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSourceChanged(<span style="color: blue; font-weight: bold;">this</span>, <span style="color: blue; font-weight: bold;">true</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (DataSourceChanged <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSourceChanged(<span style="color: blue; font-weight: bold;">this</span>, <span style="color: blue; font-weight: bold;">false</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Set up event handlers.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnInit(System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AllowPaging <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; AllowSorting <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">true</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">this</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>AutoGenerateColumns <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: teal;">10</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RowCancelingEdit <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">GridViewCancelEditEventHandler</span>(GridViewEx_RowCancelingEdit);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RowDataBound <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">GridViewRowEventHandler</span>(GridViewEx_RowDataBound);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RowEditing <span style="color: rgb(128, 128, 192); font-weight: bold;">+=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">GridViewEditEventHandler</span>(GridViewEx_RowEditing);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnInit(e);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnPageIndexChanging(<span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewPageEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>NewPageIndex;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CurrentPageID <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> PageIndex;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Handles the sort event.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Since the data source is a generic list, we need to cast the list</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> back to what ever type it is before we can sort. All the list types</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> need to be handled here (see comments below)</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnSorting(<span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewSortEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;SortExpression&quot;</span>] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression] <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortDirection;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: olive; font-weight: bold;">SortDirection</span>)(<span style="color: teal;">1</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span> (<span style="color: blue; font-weight: bold;">int</span>)ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression]);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortDirection <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: olive; font-weight: bold;">SortDirection</span>)ViewState[e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">object</span> o <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">null</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green;">//This is where more list types can be added. </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[CurrentViewID] <span style="color: blue; font-weight: bold;">is</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span> l <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; l<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Sort(<span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GenericComparer</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>(e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortDirection, e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>SortExpression));</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; o <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> l;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnSorting(e);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> o;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[CurrentViewID] <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> o;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnRowCreated(<span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRowEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnRowCreated(e);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowType <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">DataControlRowType</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Header)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">TableCell</span> cell <span style="color: blue; font-weight: bold;">in</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Cells)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>HasControls())</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">LinkButton</span> button <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Controls[<span style="color: teal;">0</span>] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">LinkButton</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (button <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">Image</span> image <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">Image</span>();</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: olive; font-weight: bold;">SortDirection</span> dir <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (ViewState[<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;SortExpression&quot;</span>] <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;SortExpression&quot;</span>]<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ToString();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dir <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: olive; font-weight: bold;">SortDirection</span>)ViewState[sortExpression];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: blue; font-weight: bold;">null</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">&amp;&amp;</span> sortExpression <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> button<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CommandArgument)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (dir <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; image<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ImageUrl <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;~/images/sort_asc.gif&quot;</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; image<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>ImageUrl <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;~/images/sort_desc.gif&quot;</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Controls<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(image);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span> <span style="color: blue; font-weight: bold;">if</span> (e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowType <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">DataControlRowType</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>DataRow) <span style="color: green;">//Handles datarow highlighting on mouse move</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Attributes<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;onmouseover&quot;</span>, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;this.originalstyle=this.style.backgroundColor;this.style.backgroundColor=&#8217;lightgreen&#8217;&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Attributes<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Add(<span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;onmouseout&quot;</span>, <span style="color: rgb(163, 21, 21); font-weight: bold;">&quot;this.style.backgroundColor=this.originalstyle;&quot;</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">override</span> <span style="color: blue; font-weight: bold;">void</span> OnDataBinding(System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (CurrentPageID <span style="color: rgb(128, 128, 192); font-weight: bold;">!=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PageIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> CurrentPageID;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">base</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>OnDataBinding(e);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> GridViewEx_RowEditing(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewEditEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EditIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>NewEditIndex;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> GridViewEx_RowDataBound(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRowEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">TableCell</span> cell <span style="color: blue; font-weight: bold;">in</span> e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Cells)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Web<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>UI<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175); font-weight: bold;">Control</span> c <span style="color: blue; font-weight: bold;">in</span> cell<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Controls)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (c <span style="color: blue; font-weight: bold;">is</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span> chkBox <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> c <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (chkBox<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Checked)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>RowState <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">DataControlRowState</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Alternate)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>BackColor <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175);">Color</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>LightSteelBlue;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; e<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Row<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>BackColor <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> System<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Drawing<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span><span style="color: rgb(43, 145, 175);">Color</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>AliceBlue;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> GridViewEx_RowCancelingEdit(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewCancelEditEventArgs</span> e)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; EditIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: rgb(128, 128, 192); font-weight: bold;">-</span><span style="color: teal;">1</span>;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">void</span> PagerSelectedIndexChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: blue; font-weight: bold;">int</span> pageSize)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">this</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> pageSize;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataSource <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> ViewState[CurrentViewID];</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DataBind();</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Calculate the real index since the actual index returned from the </span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> GridView is the index in relation to the current page. We need the</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> real index in order to index into the list.</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> Index(<span style="color: blue; font-weight: bold;">int</span> rowIndex)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> PageIndex <span style="color: rgb(128, 128, 192); font-weight: bold;">*</span> PageSize <span style="color: rgb(128, 128, 192); font-weight: bold;">+</span> rowIndex;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">
<div>}</div>
</div>
</div>
<p>Because we are using customized objects as data bound objects, we also need to provider our own comparer so that the grid knows how to sort each column. This task is fairly easy and I modified the implementation from <a href="http://www.gridviewguy.com/ArticleDetails.aspx?articleID=203">GridViewGuy</a>. One thing you need to pay special attention is that since the comparison is done using property reflections, the property names are by default case sensitive (e.g. if you have a value property named <em>Item</em>, the binding in ASP.Net must be &quot;<em>Item</em>&quot; as well. &quot;item&quot; will give a run time error).&nbsp; The code for the comparer is as follows:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GenericComparer</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>T<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span>: <span style="color: rgb(128, 64, 64); font-weight: bold;">IComparer</span><span style="color: rgb(128, 128, 192); font-weight: bold;">&lt;</span>T<span style="color: rgb(128, 128, 192); font-weight: bold;">&gt;</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: olive; font-weight: bold;">SortDirection</span> _direction <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">string</span> _expression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: olive; font-weight: bold;">SortDirection</span> Direction</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _direction; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _direction <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> GenericComparer(<span style="color: olive; font-weight: bold;">SortDirection</span> direction, <span style="color: blue; font-weight: bold;">string</span> expression)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _direction <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> direction;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _expression <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> expression;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">int</span> Compare(T x, T y)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">PropertyInfo</span> propertyInfo <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">typeof</span>(T)<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetProperty(_expression);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span> obj1 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span>)propertyInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetValue(x, <span style="color: blue; font-weight: bold;">null</span>);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span> obj2 <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> (<span style="color: rgb(128, 64, 64); font-weight: bold;">IComparable</span>)propertyInfo<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>GetValue(y, <span style="color: blue; font-weight: bold;">null</span>);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (_direction <span style="color: rgb(128, 128, 192); font-weight: bold;">==</span> <span style="color: olive; font-weight: bold;">SortDirection</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Ascending)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> obj1<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CompareTo(obj2);</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> obj2<span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>CompareTo(obj1);</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/15/extending-the-gridview-%e2%80%94-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending the GridView &#8212; Part I</title>
		<link>http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/</link>
		<comments>http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/#comments</comments>
		<pubDate>Sat, 09 Aug 2008 00:41:55 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/</guid>
		<description><![CDATA[Most business applications require presenting result sets to the end users in the form of some kind of grid views. While the default ASP.NET GridView control is sufficient for simple scenarios when doing rapid application development, its drawbacks become clear when building any large scale solutions (discussions here apply to all the current .Net frameworks, [...]]]></description>
			<content:encoded><![CDATA[<p>Most business applications require presenting result sets to the end users in the form of some kind of grid views.<span id="more-323"></span> While the default ASP.NET GridView control is sufficient for simple scenarios when doing <a href="http://en.wikipedia.org/wiki/Rapid_application_development">rapid application development</a>, its drawbacks become clear when building any large scale solutions (discussions here apply to all the current .Net frameworks, 2.0, 3.5 etc.).</p>
<p>For instance, the default data binding is limited to SqlDataSource, ObjectDataSource or a DataSet object (including DataView, DataTable, etc.). When building a large scale business solution, it is generally desirable to complete separate the data manipulation and business logic from the presentation layer and the most common solution is to employee a <a href="http://en.wikipedia.org/wiki/Model-view-controller">Model-View-Controller</a> pattern.&nbsp; This requirement makes SqlDataSource and DataSet bindings unsuitable. While ObjectDataSource does provide some level of isolation, the way business objects can be constructed is limited in order for these objects to be used as ObjectDataSource.</p>
<p>Most business objects incorporate the so-called <a href="http://en.wikipedia.org/wiki/Data_Transfer_Object">Value Object</a> data structure. Basically, it is a serializable class with the sole purpose of holding values and possibly other functionalities like data validation. Unfortunately, by default, GridView&#8217;s support for such value objects is poor (i.e. you can bind a value object to a grid view, but sorting is not supported by default). Over the years, I have seen many ad-hoc implementations to make GridView suitable for binding with customized value objects. Unfortunately, many of the solutions were implemented poorly and the implementation techniques vary from person to person, making it difficult if not impossible to maintain.</p>
<p>In the next few blog posts, I will show you how to extend the stock GridView in ASP.Net to make it suitable for using with value objects. The goal of extending the standard GridView is not only to add sorting capabilities, but to make the new component compatible with the standard GridView. I also intended to hide as much implementation details as possible so that the end users do not need to worry about the syntactical nuances as do with the standard GridView when binding to objects.</p>
<p>I assume that the development environment is Visual Studio 2005 with AJAX extension installed. The samples I show here can run within either Visual Studio 2005 (with AJAX extension) or Visual Studio 2008 without any modifications.</p>
<p><strong>1.Defining the Value Object </strong></p>
<p>As mentioned before, the reason we would like to define our own so-called value objects instead of using a DataSet to hold data is to ensure type safety. Since DataSet can hold any kind of objects, debugging becomes more difficult as error checking happens at run time not compile time. It also makes it easy to make mistakes as columns must be accessed by their names and any changes in column names could result in code broken at runtime somewhere in the UI.&nbsp; Most importantly, binding a dataset directly to the UI violates the layered design principal, making the data and the presentation tightly coupled and difficult to adapt. The solution is to create a level of indirection by utilizing the so called value objects.  Value objects are nothing fancy, they are merely a serializable class that provides the properties that represent the data (e.g. a row in a table) we are dealing with. For instance, here is what we might create as a value object for a simple phone book:</p>
<p>&nbsp;</p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; [<span style="color: rgb(43, 145, 175); font-weight: bold;">Serializable</span>()]</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">ItemVal</span></div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _address <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _zipCode <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">string</span> _phoneNumber <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: rgb(128, 128, 192); font-weight: bold;">.</span>Empty;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">bool</span> _selected <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> Name</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _name; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> Address</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _address; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _address <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> ZipCode</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _zipCode; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _zipCode <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> PhoneNumber</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _phoneNumber; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _phoneNumber <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">bool</span> Selected</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _selected; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _selected <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> ItemVal(<span style="color: blue; font-weight: bold;">string</span> name, <span style="color: blue; font-weight: bold;">string</span> address, <span style="color: blue; font-weight: bold;">string</span> zipCode, <span style="color: blue; font-weight: bold;">string</span> phoneNumber)</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _name <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> name;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _address <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> address;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _zipCode <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> zipCode;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _phoneNumber <span style="color: rgb(128, 128, 192); font-weight: bold;">=</span> phoneNumber;</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>By creating a collection of such value object we just defined, we have a full representation of the result set returned from the database via a query or stored procedure. And via value objects, all the accesses are guaranteed to be type safe.</p>
<p><strong>See also</strong></p>
<div><a href="/2008/08/08/extending-the-gridview-part-i/">Extending the GridView &mdash; Part I</a></div>
<div><a href="/2008/08/15/extending-the-gridview-&mdash;-part-ii/">Extending the GridView &mdash; Part II</a></div>
<div><a href="/2008/08/16/extending-the-gridview-&mdash;-part-iii/">Extending the GridView &mdash; Part III</a></div>
<div><a href="/2008/08/17/extending-the-gridview-&mdash;-part-iv/">Extending the GridView &mdash; Part IV</a></div>
<p>Code Download: <a href="/blog/wp-content/uploads/2008/08/extendingthegridview.zip">ExtendingTheGridView.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/08/08/extending-the-gridview-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unexpected Event Behavior</title>
		<link>http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/</link>
		<comments>http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/#comments</comments>
		<pubDate>Sun, 04 May 2008 01:13:55 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/</guid>
		<description><![CDATA[Typically, there is rarely a need to change a control&#8217;s ID. However, this practice is sometimes useful when debugging a custom control, partiularly one that contains dynamically generated controls. When a control resides&#160;within an&#160;AJAX update panel which in turn resides in a Content control on a page that uses a master page however, changing the [...]]]></description>
			<content:encoded><![CDATA[<p>Typically, there is rarely a need to change a control&#8217;s ID. However, this practice is sometimes useful when debugging a custom control, partiularly one that contains dynamically generated controls.<span id="more-287"></span></p>
<p>When a control resides&nbsp;within an&nbsp;AJAX update panel which in turn resides in a Content control on a page that uses a master page however, changing the control ID will result in the&nbsp;attached event not being fired (please note, if the update panel is placed in a page directly, e.g. no master page is used, the code iluustrated below works correctly). At least this is what I have observed&nbsp;in&nbsp;AJAX extension 1.0 for Visual Studio&nbsp;2005.&nbsp;</p>
<p>The the following simple user control for example&nbsp;&nbsp;</p>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New"><span style="font-weight: bold; color: blue">public</span> <span style="font-weight: bold; color: blue">partial</span> <span style="font-weight: bold; color: blue">class</span> <span style="font-weight: bold; color: #2b91af">WebUserControl</span> : System.Web.UI.<span style="font-weight: bold; color: #2b91af">UserControl</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">{</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">protected</span> <span style="font-weight: bold; color: blue">void</span> Page_Load(<span style="font-weight: bold; color: blue">object</span> sender, <span style="font-weight: bold; color: #2b91af">EventArgs</span> e)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; BuildControls();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">private</span> <span style="font-weight: bold; color: blue">void</span> BuildControls()</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: #2b91af">CheckBox</span> cb = <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #2b91af">CheckBox</span>();</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cb.AutoPostBack = <span style="font-weight: bold; color: blue">true</span>;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">//Event will not fire if the control&#8217;s ID is changed</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">//and the control is in an update panel.</span></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <font style="background-color: #ffff00">cb.ID = <span style="font-weight: bold; color: #2b91af">String</span>.Format(<span style="font-weight: bold; color: #a31515">&quot;{0}{1}&quot;</span>, <span style="font-weight: bold; color: #a31515">&quot;MyBtn&quot;</span>, UniqueID); </font></div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; cb.CheckedChanged += <span style="font-weight: bold; color: blue">new</span> <span style="font-weight: bold; color: #8080c0">EventHandler</span>(cb_CheckedChanged);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">this</span>.Controls.Add(cb);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; <span style="font-weight: bold; color: blue">void</span> cb_CheckedChanged(<span style="font-weight: bold; color: blue">object</span> sender, <span style="font-weight: bold; color: #2b91af">EventArgs</span> e)</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; {</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Label1.Text = <span style="font-weight: bold; color: #2b91af">String</span>.Format(<span style="font-weight: bold; color: #a31515">&quot;Check box checked: {0} &lt;br /&gt;&quot;</span>, (sender <span style="font-weight: bold; color: blue">as</span> <span style="font-weight: bold; color: #2b91af">CheckBox</span>).Checked);</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">&nbsp;&nbsp;&nbsp; }</div>
<div style="font-size: 10pt; background: white; margin: 0px; color: black; font-family: Courier New">}</div>
<p><!--EndFragment--></p>
<p>When the above control (a dynamic button and a label) is embedded within a normal page (e.g. a non-AJAX page), the CheckedChanged event is fired whenever the check box is clicked. But when the same control is put into an update panel, whith the highlighted line of code included, the event will no longer fire.</p>
<p>This strange behavior had caused me some headaches while debugging through a rather complex web user control. So you will need to be extra careful when controls are placed in update panels. Due to limitations like the one I just mentioned a control that works in a normal page might not work properly in an AJAX enabled web page. The solution to this problem is simple, just remove the highlighted line and the event will fire again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/05/03/unexpected-event-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Certain IIS Settings Cause Websetup Failure</title>
		<link>http://www.kerrywong.com/2008/03/15/certain-iis-settings-cause-websetup-failure/</link>
		<comments>http://www.kerrywong.com/2008/03/15/certain-iis-settings-cause-websetup-failure/#comments</comments>
		<pubDate>Sun, 16 Mar 2008 01:20:21 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[IIS]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/03/15/certain-iis-settings-cause-websetup-failure/</guid>
		<description><![CDATA[I ran into an interesting problem with the Web Setup project in ASP.Net 2.0 last Friday.To facilitate production deployment, I typically use Web Setup project to create an installer so that everything needs to be installed is packaged nicely in a single executable. Anyway, the problem I encountered was that while in test, the installation [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into an interesting problem with the Web Setup project in ASP.Net 2.0 last Friday.<span id="more-271"></span>To facilitate production deployment, I typically use Web Setup project to create an installer so that everything needs to be installed is packaged nicely in a single executable. Anyway, the problem I encountered was that while in test, the installation worked perfectly but it failed in production with the following cryptic error message:</p>
<blockquote>
<p><em>The specified path &#8216;/LM/W3SVC/1/ROOT/&#8217; is unavailable. This Internet Information Server might not be running or path exists and is redirected to another machine. Please check the status of this virtual directory in the Internet Service Manager.</em></p>
</blockquote>
<p>As it turned out, the production server I was doing the installation on had a url redirection set at the root level (on the Default Web Site) in IIS. It apparently is used to &quot;alias&quot; the server name with another server.</p>
<p>This is a little bit tricky as all the virtual directory settings on the actual websites are normal (a local directory). Had I not happened to check on the IIS root level settings, it would probably take me much longer to figure out.</p>
<p>So, the solution is to temporarily disable the default site level redirection by changing it to pointing to a local directory. Then, the installation can be proceeded.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/03/15/certain-iis-settings-cause-websetup-failure/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Stange Issue With Global.asax</title>
		<link>http://www.kerrywong.com/2008/02/18/a-stange-issue-with-globalasax/</link>
		<comments>http://www.kerrywong.com/2008/02/18/a-stange-issue-with-globalasax/#comments</comments>
		<pubDate>Mon, 18 Feb 2008 04:17:36 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/02/18/a-stange-issue-with-globalasax/</guid>
		<description><![CDATA[I always use Global.asax for centralized error handling in my ASP.Net applications and typically it works great. The other day though, I ran into a very strange issue: While running local, everything works fine (the error page shows up via a Server.Transfer within Application_Error). But once deployed, the custom error page no longer seemed to [...]]]></description>
			<content:encoded><![CDATA[<p>I always use Global.asax for centralized error handling in my ASP.Net applications and typically it works great.<span id="more-267"></span></p>
<p>The other day though, I ran into a very strange issue: While running local, everything works fine (the error page shows up via a Server.Transfer within Application_Error). But once deployed, the custom error page no longer seemed to work, and the standard error page showed up.</p>
<p>I was quite perplexed as to why this happened. And a quick search using Google I found out that it seemed that quite a few people out there were experiencing the same issue. The server is running Windows 2003 and has both Framework 1.1 and 2.0 (the application is in 2.0).</p>
<p>As what I am doing within Application_Error does not require any special privilege (only saves the error in session and does a server transfer) so I am pretty sure that this could not have been a permission issue. Anyway, an aspnet_regiis /u and /i seemed to have fixed the issue. But the strangest thing is that nothing seems to have changed&#8230;</p>
<p>Well,&nbsp; I am still not sure what the exact cause was, and I will definitely come back to it if I ever find out&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/02/18/a-stange-issue-with-globalasax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Row Selection With GridView</title>
		<link>http://www.kerrywong.com/2008/02/10/multiple-row-selection-with-gridview/</link>
		<comments>http://www.kerrywong.com/2008/02/10/multiple-row-selection-with-gridview/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 18:36:27 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[GridView]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/02/10/multiple-row-selection-with-gridview/</guid>
		<description><![CDATA[We often need the capability of selecting multiple rows from a grid, unfortunately the GridView component in ASP.Net 2.0 does not offer an out-of-box support for such functionality.Fortunately, implementing such behavior is pretty straight if you have a clear understanding of how the GridView component works. Many of the samples demonstrated in various other posts [...]]]></description>
			<content:encoded><![CDATA[<p>We often need the capability of selecting multiple rows from a grid, unfortunately the GridView component in ASP.Net 2.0 does not offer an out-of-box support for such functionality.<span id="more-265"></span>Fortunately, implementing such behavior is pretty straight if you have a clear understanding of how the GridView component works. Many of the samples demonstrated in various other posts require registering some&nbsp; kind of client side JavaScript, here I will show a slightly different approach in which no custom JavaScript is needed (of course, there are JavaScripts generated automatically by the framework).</p>
<p>Suppose we are creating a simple application where you can select multiple people from a contact list and then do somthing with your selection (e.g. export the selection etc.) An intuitive way is to include a column of CheckBoxes in the front of all the rows so that the staus of each CheckBox indicates whether that row is included in the user selection (See below):</p>
<table rules="all" cellspacing="0" border="1" align="center" style="border-collapse: collapse;" id="GridView1">
<tbody>
<tr>
<th align="center" scope="col">Select</th>
<th scope="col">User Name</th>
<th scope="col">Phone Number</th>
</tr>
<tr>
<td>
<input type="checkbox" name="GridView1$ctl02$CBEntrySelected" onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl02$CBEntrySelected\',\'\')', 0)" id="GridView1_ctl02_CBEntrySelected" /></td>
<td>Bob</td>
<td>(232) 111-1312</td>
</tr>
<tr>
<td>
<input type="checkbox" name="GridView1$ctl03$CBEntrySelected" onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl03$CBEntrySelected\',\'\')', 0)" id="GridView1_ctl03_CBEntrySelected" /></td>
<td>Sue</td>
<td>(088) 676-9987</td>
</tr>
<tr>
<td>
<input type="checkbox" name="GridView1$ctl04$CBEntrySelected" onclick="javascript:setTimeout('__doPostBack(\'GridView1$ctl04$CBEntrySelected\',\'\')', 0)" id="GridView1_ctl04_CBEntrySelected" /></td>
<td>Jim</td>
<td>(678) 123-4567</td>
</tr>
</tbody>
</table>
<div align="center">&nbsp;</div>
<p align="left">In this demonstration, I will use a customized object data source called UserDefinition:&nbsp;</p>
<blockquote>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>[<span style="color: rgb(43, 145, 175); font-weight: bold;">Serializable</span>()]</div>
<div><span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">class</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span></div>
<div>{</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">bool</span> _entrySelected <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">false</span>;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">string</span> _userName <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: navy;">.</span>Empty;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">string</span> _phoneNumber <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">string</span><span style="color: navy;">.</span>Empty;</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">bool</span> EntrySelected</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _entrySelected; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _entrySelected <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> UserName</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _userName; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _userName <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> PhoneNumber</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> { <span style="color: blue; font-weight: bold;">return</span> _phoneNumber; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> { _phoneNumber <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">value</span>; }</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> UserDefinition(<span style="color: blue; font-weight: bold;">string</span> name, <span style="color: blue; font-weight: bold;">string</span> phone)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _userName <span style="color: navy;">=</span> name;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _phoneNumber <span style="color: navy;">=</span> phone;</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>}</div>
</div>
</blockquote>
<p align="left">Alternatively, you could use pretty much any data sources that you are accustomed to. The concept should be similar&nbsp; enough.</p>
<p align="left">And the GridView is defined as follows:</p>
<blockquote>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div><span style="color: navy;">&lt;</span>asp:GridView ID<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;GridView1&quot;</span> runat<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;server&quot;</span> AutoGenerateColumns<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;False&quot;</span><span style="color: navy;">&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;</span>Columns<span style="color: navy;">&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;</span>asp:TemplateField HeaderText<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;Select&quot;</span><span style="color: navy;">&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;</span>ItemTemplate<span style="color: navy;">&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;</span>asp:CheckBox ID<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;CBEntrySelected&quot;</span> runat<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;server&quot;</span> AutoPostBack<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;True&quot;</span> Checked<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&#8216;&lt;%# Bind(&quot;EntrySelected&quot;) %&gt;&#8217;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; OnCheckedChanged<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;CBEntrySelected_CheckedStateChanged&quot;</span> <span style="color: navy;">/&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;/</span>ItemTemplate<span style="color: navy;">&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;/</span>asp:TemplateField<span style="color: navy;">&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;</span>asp:BoundField HeaderText<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;User Name&quot;</span> DataField<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;UserName&quot;</span> <span style="color: navy;">/&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;</span>asp:BoundField HeaderText<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;Phone Number&quot;</span> DataField<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;PhoneNumber&quot;</span> <span style="color: navy;">/&gt;</span></div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: navy;">&lt;/</span>Columns<span style="color: navy;">&gt;</span></div>
<div><span style="color: navy;">&lt;/</span>asp:GridView<span style="color: navy;">&gt;</span></div>
</div>
</blockquote>
<p align="left">Note that the AutoPostBack property is set to true so that the server side code would have an opportunity to record the user&#8217;s action. So far, every thing should be pretty straightforward. The only trick is in the server side code:</p>
<blockquote>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DisplayGrid();</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">private</span> <span style="color: blue; font-weight: bold;">void</span> DisplayGrid()</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: navy;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span><span style="color: navy;">&gt;</span> list <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: navy;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span><span style="color: navy;">&gt;</span>();</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; list<span style="color: navy;">.</span>Add(<span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span>(<span style="color: rgb(163, 21, 21);">&quot;Bob&quot;</span>, <span style="color: rgb(163, 21, 21);">&quot;(232) 111-1312&quot;</span>));</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; list<span style="color: navy;">.</span>Add(<span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span>(<span style="color: rgb(163, 21, 21);">&quot;Sue&quot;</span>, <span style="color: rgb(163, 21, 21);">&quot;(088) 676-9987&quot;</span>));</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; list<span style="color: navy;">.</span>Add(<span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span>(<span style="color: rgb(163, 21, 21);">&quot;Jim&quot;</span>, <span style="color: rgb(163, 21, 21);">&quot;(678) 123-4567&quot;</span>));</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridView1<span style="color: navy;">.</span>DataSource <span style="color: navy;">=</span> list;</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: navy;">!</span>Page<span style="color: navy;">.</span>IsPostBack)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GridView1<span style="color: navy;">.</span>DataBind();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ViewState[<span style="color: rgb(163, 21, 21);">&quot;CurrentView&quot;</span>] <span style="color: navy;">=</span> list;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> CBEntrySelected_CheckedStateChanged(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span> checkBox <span style="color: navy;">=</span> sender <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">CheckBox</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRow</span> selectedRow <span style="color: navy;">=</span> checkBox<span style="color: navy;">.</span>Parent<span style="color: navy;">.</span>Parent <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">GridViewRow</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: navy;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span><span style="color: navy;">&gt;</span> list <span style="color: navy;">=</span> ViewState[<span style="color: rgb(163, 21, 21);">&quot;CurrentView&quot;</span>] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: navy;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span><span style="color: navy;">&gt;</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; list[selectedRow<span style="color: navy;">.</span>RowIndex]<span style="color: navy;">.</span>EntrySelected <span style="color: navy;">=</span> checkBox<span style="color: navy;">.</span>Checked;</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> BtnSubmit_Click(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: navy;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span><span style="color: navy;">&gt;</span> list <span style="color: navy;">=</span> ViewState[<span style="color: rgb(163, 21, 21);">&quot;CurrentView&quot;</span>] <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">List</span><span style="color: navy;">&lt;</span><span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span><span style="color: navy;">&gt;</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">StringBuilder</span> sb <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">StringBuilder</span>();</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb<span style="color: navy;">.</span>Append(<span style="color: rgb(163, 21, 21);">&quot;&lt;br /&gt;Selected items:&lt;br /&gt;&quot;</span>);</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">foreach</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">UserDefinition</span> def <span style="color: blue; font-weight: bold;">in</span> list)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (def<span style="color: navy;">.</span>EntrySelected)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb<span style="color: navy;">.</span>Append(<span style="color: blue; font-weight: bold;">string</span><span style="color: navy;">.</span>Format(<span style="color: rgb(163, 21, 21);">&quot;{0} : {1}&lt;br /&gt;&quot;</span>, def<span style="color: navy;">.</span>UserName, def<span style="color: navy;">.</span>PhoneNumber));</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Label1<span style="color: navy;">.</span>Text <span style="color: navy;">=</span> sb<span style="color: navy;">.</span>ToString();</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
</div>
</blockquote>
<p align="left">Note that in our CkeckBox checked event handler CBEntrySelected_CheckedStateChanged, we used the GridView object hierarchy to figure out which row was selected/unselected by user and record it accordingly in the object that the grid renders. The hierarchy is as follows:</p>
<blockquote>
<p align="left">&nbsp;CheckBox -&gt; DataControlFieldCell -&gt; GridViewRow -&gt; ChildTable -&gt; GridView</p>
</blockquote>
<p align="left">&nbsp;Also note that in our simple example, only the RowIndex is used to determine whether a row is selected or not. This only works when paging is not involved. When there are multiple pages, we have to go two hierarchies higher to the GridView level, where we can use PageIndex and PageSize to identify the correct selection (i.e. PageSize * PageIndex + RowIndex).</p>
<p align="center">&nbsp;<img src="/blog/wp-content/uploads/2008/02/multiipleselection.GIF" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/02/10/multiple-row-selection-with-gridview/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reflection and Case Sensitivity</title>
		<link>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/</link>
		<comments>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/#comments</comments>
		<pubDate>Mon, 04 Feb 2008 02:48:56 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/</guid>
		<description><![CDATA[Reflection is case sensitive by default, which comes at no surprise to most C# developers.However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do [...]]]></description>
			<content:encoded><![CDATA[<p>Reflection is case sensitive by default, which comes at no surprise to most C# developers.<span id="more-263"></span>However, it is easy to forget the case sensitivity issue sometimes especially when doing web development. I recently ran into such an issue and it took me a while to realize that the problem I had actually had something to do with the case sensitivity issue while using reflection.</p>
<p>I was doing some ASP.NET work and was using customized objects as data sources to bind to DataGrid. For sorting, I used the technique similar to what described in <a href="http://www.gridviewguy.com/ArticleDetails.aspx?articleID=203">Sorting GridView Using IComparer</a>. When I was testing my grids, however, I noticed that for most of the columns it worked perfectly, but for a few I somehow was greeted with the null reference error.</p>
<p>At first I thought that this was caused by the null items in those columns. After spending quite sometime digging, I realized that this was due to the case sensitivity issue in reflection. I had used the wrong case in the DataField of&nbsp; the BoundField. And after I changed the spelling to match that defined in the value object, the problem was solved.</p>
<p>VB developers should pay special attention when using reflections, as in VB.Net the language itself is not case sensitive.</p>
<p>As a side note, even though it is possible to set the binding flag to make the member lookup case insensitive, it is not recommended as <a href="http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx?loc=&amp;side=true">such approach tend to make the operation less efficient</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/02/03/reflection-and-case-sensitivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JScript Exception in AJAX Control Toolkit</title>
		<link>http://www.kerrywong.com/2008/01/21/jscript-exception-in-ajax-control-toolkit/</link>
		<comments>http://www.kerrywong.com/2008/01/21/jscript-exception-in-ajax-control-toolkit/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 20:46:19 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2008/01/21/jscript-exception-in-ajax-control-toolkit/</guid>
		<description><![CDATA[I ran across an exception &#34;Microsoft JScript runtime error: &#8216;this._postBackSettings.async&#8217; is null or not an object&#34; the other day while writing some AJAX code with .Net AJAX Extensions for .Net framework 2.0. The page I was developing was simply enough, basically, it just consists of a button and a textbox inside an update panel. Doing [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across an exception &quot;<font color="#ff0000">Microsoft JScript runtime error: &#8216;this._postBackSettings.async&#8217; is null or not an object</font>&quot; the other day while writing some AJAX code with .Net AJAX Extensions for .Net framework 2.0.<span id="more-261"></span></p>
<p>The page I was developing was simply enough, basically, it just consists of a button and a textbox inside an update panel. Doing a search on the error turned out quite a few matches, and it seems that a lot of people are having the same issue.</p>
<p>After reading a couple of posts, I realized that the reasons given were either incorrect (i.e. did not solve the issue I had) or pretty vague. So, after carefully looking through the exception path within the JScript library, I realized that this might have been caused by the fact that the page does not have a default submit button.</p>
<p>So here is the solution:</p>
<p>Add an asp panel&nbsp; around the submit button and the textbox, and set the DefaultButton property to the submit button. Now the the submit button should be working without triggering the JScript exception mentioned earlier.</p>
<p>&nbsp;&nbsp;&nbsp; &lt;asp:Panel ID=&quot;Panel1&quot; runat=&quot;server&quot;&nbsp; DefaultButton=&quot;BtnSubmit&quot;&gt;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2008/01/21/jscript-exception-in-ajax-control-toolkit/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Interesting Databinding Behavior</title>
		<link>http://www.kerrywong.com/2007/12/21/interesting-databinding-behavior/</link>
		<comments>http://www.kerrywong.com/2007/12/21/interesting-databinding-behavior/#comments</comments>
		<pubDate>Sat, 22 Dec 2007 02:54:07 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2007/12/21/interesting-databinding-behavior/</guid>
		<description><![CDATA[Earlier today, while I was trying to bind a collection to a drop down list, I got the following exception: &#160;&#160;&#160; &#8216;DropDownList1&#8242; has a SelectedValue which is invalid because it does not exist in the list of items. After inspecting my code, it turned out that the culprit was a rather benign line of code. [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today, while I was trying to bind a collection to a drop down list, I got the following exception:<span id="more-257"></span></p>
<p><em>&nbsp;&nbsp;&nbsp; &#8216;DropDownList1&#8242; has a SelectedValue which is invalid because it does not exist in the list of items.</em></p>
<p>After inspecting my code, it turned out that the culprit was a rather benign line of code. Since this exception generated is so obscure, I am going to show you the code in its simplest form that would cause this exception so that you can solve similar problems easier.</p>
<p>Suppose I have a drop down list in my aspx page that is bound to a DataTable (the DataSource is not important in the scenario when the exception occurs, it can be an object data source as well, to illustrate the point, I will use a DataTable):</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div><span style="color: navy;">&lt;</span>asp:<span style="color: rgb(43, 145, 175); font-weight: bold;">DropDownList</span> ID<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;DropDownList1&quot;</span> runat<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;server&quot;</span> DataSource<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&#8216;&lt;%#GetData() %&gt;&#8217;</span> DataTextField<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;Value&quot;</span> DataValueField<span style="color: navy;">=</span><span style="color: rgb(163, 21, 21);">&quot;Index&quot;</span> <span style="color: navy;">/&gt;</span></div>
</div>
<p>Where the data is populated through method GetData()</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">DataTable</span> GetData()</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataTable</span> dt <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">DataTable</span>();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataColumn</span> dc1 <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">DataColumn</span>(<span style="color: rgb(163, 21, 21);">&quot;Index&quot;</span>);</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataColumn</span> dc2 <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">new</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">DataColumn</span>(<span style="color: rgb(163, 21, 21);">&quot;Value&quot;</span>);</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt<span style="color: navy;">.</span>Columns<span style="color: navy;">.</span>Add(dc1);</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt<span style="color: navy;">.</span>Columns<span style="color: navy;">.</span>Add(dc2);</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataRow</span> r1 <span style="color: navy;">=</span> dt<span style="color: navy;">.</span>NewRow();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r1[<span style="color: rgb(163, 21, 21);">&quot;Index&quot;</span>] <span style="color: navy;">=</span> <span style="color: teal;">0</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r1[<span style="color: rgb(163, 21, 21);">&quot;Value&quot;</span>] <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;Test1&quot;</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt<span style="color: navy;">.</span>Rows<span style="color: navy;">.</span>Add(r1);</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: rgb(43, 145, 175); font-weight: bold;">DataRow</span> r2 <span style="color: navy;">=</span> dt<span style="color: navy;">.</span>NewRow();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r2[<span style="color: rgb(163, 21, 21);">&quot;Index&quot;</span>] <span style="color: navy;">=</span> <span style="color: teal;">1</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r2[<span style="color: rgb(163, 21, 21);">&quot;Value&quot;</span>] <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;Test2&quot;</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dt<span style="color: navy;">.</span>Rows<span style="color: navy;">.</span>Add(r2);</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">return</span> dt;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>In the Page_Load method we can bind the DropDownList as follows:</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: navy;">!</span>Page<span style="color: navy;">.</span>IsPostBack)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownList1<span style="color: navy;">.</span>DataBind();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>Indeed, this is what we have expected. The problem comes when you try to assign the displayed text to the DropDownList. To illustrate, the following code will cause the exception mentioned earlier:</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: navy;">!</span>Page<span style="color: navy;">.</span>IsPostBack)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownList1<span style="color: navy;">.</span>Text <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;This assignment will cause exception&quot;</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownList1<span style="color: navy;">.</span>DataBind();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
<p>If the text is assigned after the DropDownList is bound however, the code will execute just fine but the assignment will have no effect of course.</p>
<p>Interesting enough, if the above code is placed within a try..catch block, the DataBinding will work just fine even though an exception had been thrown!</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: navy;">!</span>Page<span style="color: navy;">.</span>IsPostBack)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">try</span></div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownList1<span style="color: navy;">.</span>Text <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;This assignment will cause exception&quot;</span>;</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DropDownList1<span style="color: navy;">.</span>DataBind();</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">catch</span> (<span style="color: rgb(43, 145, 175); font-weight: bold;">Exception</span> ex)</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response<span style="color: navy;">.</span>Write(ex<span style="color: navy;">.</span>Message);</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/12/21/interesting-databinding-behavior/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Weird Master Page Problem</title>
		<link>http://www.kerrywong.com/2007/12/08/weird-master-page-problem/</link>
		<comments>http://www.kerrywong.com/2007/12/08/weird-master-page-problem/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 03:17:29 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Master Page]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2007/12/08/weird-master-page-problem/</guid>
		<description><![CDATA[I recently ran into a very strange problem with ASP.Net 2.0. I have used master pages for a couple of years now, and this is the first time I encountered such an odd problem. Well, I have a label (Label1) on my master page (say MasterPage.master), this label can be accessed by the content pages [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a very strange problem with ASP.Net 2.0. I have used master pages for a couple of years now, and this is the first time I encountered such an odd problem.<span id="more-253"></span></p>
<p>Well, I have a label (Label1) on my master page (say MasterPage.master), this label can be accessed by the content pages via a simple property, for example:</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">public</span> <span style="color: blue; font-weight: bold;">string</span> TestText</div>
<div>&nbsp;&nbsp;&nbsp; {</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">get</span> {<span style="color: blue; font-weight: bold;">return</span> Label1<span style="color: navy;">.</span>Text;}</div>
<div>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">set</span> {Label1<span style="color: navy;">.</span>Text <span style="color: navy;">=</span> <span style="color: blue; font-weight: bold;">value</span>;}</div>
<div>&nbsp;&nbsp;&nbsp; }</div>
</div>
<p>&nbsp;</p>
<p>And in my content page, I wanted to set a value to TestText via:</p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<div>(Master <span style="color: blue; font-weight: bold;">as</span> <span style="color: rgb(43, 145, 175); font-weight: bold;">MasterPage</span>)<span style="color: navy;">.</span>TestText <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;123&quot;</span>;</div>
</div>
<p>&nbsp;</p>
<p>It is just as simple as that. Over the years I have used this exact approach to update shared properties across many different pages (e.g. page tile on the master page). But the code I was writing at work threw a runtime exception (InvalidCastException), complaining that it couldn&#8217;t cast my page (say Default.aspx) back to the master page (MasterPage). The actual page logic was quite complex, but the problem at hand was nevertheless exactly the same as the code illustrated above.</p>
<p>At first I thought that I might have over looked something, but carefully looking through code (as you can imagine, the code to achieve this is very trivial) multiple times, I was convinced that there&#8217;s something wrong with the .Net Framework.</p>
<p>Out of curiosity I also tried the &lt;%@ MasterType directive. But using either TypeName or VirtualPath resulted the same exact problem. When using VirtualPath, I could see the error at design time and using TypeName the error appeared at runtime.</p>
<p>After scratching my head for quite sometime, I decided to look through Microsoft&#8217;s Knowledge base articles. And sure enough, here is the <a href="http://support.microsoft.com/kb/915782">article </a>that describes the exact same problem I faced. Unfortunately, the suggested patch did not solve my problem and since this knowledge base article was updated only a few days ago (12/3/2007) it was not all that surprising that the patch did not work for my particular case.</p>
<p>I guess I will have to wait for the SP2 release of .Net framework 2.0 to get this simple code to work&#8230; In the mean time, I had to move the Label control to each individual content page (obviously not an elegant solution).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/12/08/weird-master-page-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Stange Post Back Problem</title>
		<link>http://www.kerrywong.com/2007/11/07/a-stange-post-back-problem/</link>
		<comments>http://www.kerrywong.com/2007/11/07/a-stange-post-back-problem/#comments</comments>
		<pubDate>Thu, 08 Nov 2007 02:39:00 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://www.kerrywong.com/2007/11/07/a-stange-post-back-problem/</guid>
		<description><![CDATA[A few days ago, I was deploying a Website at work and ran into a very mysterious problem. The problem is what I call a &#34;Skipped Post Back&#34; or a page double load problem. For example, if I load a page say, default.aspx in a browser and hit a submit button. The default behavior should [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, I was deploying a Website at work and ran into a very mysterious problem.<span id="more-234"></span></p>
<p>The problem is what I call a &quot;Skipped Post Back&quot; or a page double load problem. For example, if I load a page say, default.aspx in a browser and hit a submit button. The default behavior should be that when the submit button is clicked, a post back event is fired. The simplest way to see how this works is to consider the code snippet below:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red0\green0\blue128;\red163\green21\blue21;}??\fs20     \cf3 {\b protected}\cf0  \cf3 {\b void}\cf0  Page_Load(\cf3 {\b object}\cf0  sender, \cf4 {\b EventArgs}\cf0  e)\par ??    \{\par ??        \cf3 {\b if}\cf0  (\cf5 !\cf0 Page\cf5 .\cf0 IsPostBack)\par ??        \{\par ??            Response\cf5 .\cf0 Write(\cf6 "Page Load"\cf0 );\par ??        \}\par ??        \cf3 {\b else}\par ??\cf0         \{\par ??            Response\cf5 .\cf0 Write(\cf6 "Post Back"\cf0 );\par ??        \}\par ??    \}}<br />
--></p>
<div style="background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">protected</span> <span style="color: blue; font-weight: bold;">void</span> Page_Load(<span style="color: blue; font-weight: bold;">object</span> sender, <span style="color: rgb(43, 145, 175); font-weight: bold;">EventArgs</span> e)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">if</span> (<span style="color: navy;">!</span>Page<span style="color: navy;">.</span>IsPostBack)</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response<span style="color: navy;">.</span>Write(<span style="color: rgb(163, 21, 21);">&quot;Page Load&quot;</span>);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue; font-weight: bold;">else</span></p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Response<span style="color: navy;">.</span>Write(<span style="color: rgb(163, 21, 21);">&quot;Post Back&quot;</span>);</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</p>
</div>
<p>If I run the page, I should see &quot;Page Load&quot; when the page is initially loaded and &quot;Post Back&quot; when I hit a submit button.</p>
<p>But what had happened was that when I deployed a website to a Windows 2003 server (.Net Framework 1.1), and when viewed with IE6 what happened really baffled me.</p>
<p>If I just type in the url (say http://mywebsite/default.aspx), everything works as expected. If however, I set my default page as default.aspx and omit the page in the url (e.g http://mywebsite/), when I hit the submit button, I would see a &quot;Page Load&quot; displayed instead of &quot;Post Back&quot;.</p>
<p>At first, I thought that this was an issue with the code (the original page was more complicated then the sample illustrated above), but after reducing the page to the example I gave, it was clear that something was wrong with the combination of IE6 and .Net 1.1 on Windows 2003 Server, at least to this particular installation. As if the same code was deployed on a Windows 2000 server or simply just Windows XP, everything would work correctly. Further more, viewing the page with FireFox does not produce the erroneous behavior.</p>
<p>Clearly something is wrong here. While scratching my head, I noticed something very interesting: when I hit the submit button while running IE6 I would sometimes see &quot;Post Back&quot; briefly before &quot;Page Load&quot; showed up, which indicates that the page was for some reason loaded twice.</p>
<p>After hours of researching without any clear answer, I had to resort to the silliest code to fix the issue. Namely, i added a default page (say index.aspx) and all it does is to redirect to default.aspx. And I configure IIS so that index.aspx would be the default page if no page was supplied. This setting ensures that when default.aspx is loaded the url will contain the page itself.</p>
<p>Given that it works with Firefox but not IE6, I am pretty sure that this is an IE6 issue (or the particular patch level of IE6 we had at work).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/11/07/a-stange-post-back-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Captcha Problem with dasBlog 1.9 under ASP.Net 2.0</title>
		<link>http://www.kerrywong.com/2007/05/31/captcha-problem-with-dasblog-19-under-aspnet-20/</link>
		<comments>http://www.kerrywong.com/2007/05/31/captcha-problem-with-dasblog-19-under-aspnet-20/#comments</comments>
		<pubDate>Fri, 01 Jun 2007 00:33:22 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[dasBlog]]></category>

		<guid isPermaLink="false">http://dimension/2007/05/31/captcha-problem-with-dasblog-19-under-aspnet-20/</guid>
		<description><![CDATA[I upgraded my blog engine from dasBlog 1.8.5223.2 to 1.9.6264.0 roughly a month ago. A few people recently brought to my attention that the capcha on the comment page no longer worked properly. Since I have been pretty busy recently after my move, I haven&#8217;t really got a chance to look at the problem yet. [...]]]></description>
			<content:encoded><![CDATA[<p>I upgraded my blog engine from dasBlog 1.8.5223.2 to 1.9.6264.0 roughly a month ago.<span id="more-170"></span> A few people recently brought to my attention that the capcha on the comment page no longer worked properly. Since I have been pretty busy recently after my move, I haven&rsquo;t really got a chance to look at the problem yet. But just a quick Google, I found out that many people apparently are having the same issues.</p>
<p>From what I have read, it&rsquo;s a compatibility issue between WebControlCaptcha.dll and ASP.Net 2.0 (which is what my site is running on right now). I will try to find out if there is an easy way to fix this problem (I can&rsquo;t simply revert back to ASP.Net 1.1 since many new pages I added were in 2.0). Stay tuned&hellip;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/05/31/captcha-problem-with-dasblog-19-under-aspnet-20/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WebForm_AutoFocus Issue with Custom Pages in dasBlog</title>
		<link>http://www.kerrywong.com/2007/05/04/webform_autofocus-issue-with-custom-pages-in-dasblog/</link>
		<comments>http://www.kerrywong.com/2007/05/04/webform_autofocus-issue-with-custom-pages-in-dasblog/#comments</comments>
		<pubDate>Sat, 05 May 2007 00:21:11 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[dasBlog]]></category>

		<guid isPermaLink="false">http://dimension/2007/05/04/webform_autofocus-issue-with-custom-pages-in-dasblog/</guid>
		<description><![CDATA[I have created quite a few customized pages (e.g. http://www.kerrywong.com/cmsg ) on my blog site. One thing I noticed is that some of the .Net generated client side java scripts (e.g. WebForm_AutoFocus and some client side validations) do not fire correctly when running under framework 2.0. At first I thought that this was due to [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">I have created quite a few customized pages (e.g. http://www.kerrywong.com/cmsg ) on my blog site.</span><span id="more-162"></span><span style="font-size: 10pt; font-family: Verdana;"> One thing I noticed is that some of the .Net generated client side java scripts (e.g. WebForm_AutoFocus and some client side validations) do not fire correctly when running under framework 2.0. At first I thought that this was due to some build problems or some incorrect configuration settings in machine.config. But when I deployed the same webpages to a new website (say on a different port), all the client side java scripts worked fine. And if I load the same page deployed on the same site where dasBlog is deployed with IE, I got a scripting error which complains WebForm_AutoFocus is not defined. So I knew that something is wrong with the web.config that came with dasBlog.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>After some searching, I found out that the problem was caused by certain settings in web.config. In .Net 2.0 client side scripts are dynamically generated via WebResource.axd, and for whatever reason, certain settings prevent the generated client side java scripts to execute. Ross Hawkins has a <a href="http://www.rosshawkins.net/rosshawkins.nsf/vwBlogsByDate/9A734FBAB1430629CC2571FC003DCDA1">great article</a> on this issue<o:p></o:p> In his blog, he examined many different scenarios which might lead to the behavior I discussed above. For dasBlog users, the key is to find this line in your dasBlog web.config file (located in &lt;blowery.web&gt; section):<o:p></o:p></span></p>
<pre><span style="font-size: 10pt; font-family: 'Courier New'; color: blue;">&lt;</span><span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(163, 21, 21);">httpCompress</span><span style="font-size: 10pt; font-family: 'Courier New'; color: blue;"> </span><span style="font-size: 10pt; font-family: 'Courier New'; color: red;">preferredAlgorithm</span><span style="font-size: 10pt; font-family: 'Courier New'; color: blue;">=</span><span style="font-size: 10pt; font-family: 'Courier New';">&quot;<span style="color: blue;">gzip</span>&quot;<span style="color: blue;"> </span><span style="color: red;">compressionLevel</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">high</span>&quot;<span style="color: blue;">&gt;<o:p></o:p></span></span></pre>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">And change the compression level to &quot;none&quot;.<o:p> </o:p></span>  <span style="font-size: 10pt; font-family: Verdana;">This approach is not ideal though since by removing the compression capability the website performance may suffer a little bit, depending on the request size and the website traffic. But right now, it seems that this is the only way that I can find to solve this problem. I will certainly investigate more and will keep everyone posted if I find a better solution.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/05/04/webform_autofocus-issue-with-custom-pages-in-dasblog/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Browser Impersonation</title>
		<link>http://www.kerrywong.com/2007/01/20/browser-impersonation/</link>
		<comments>http://www.kerrywong.com/2007/01/20/browser-impersonation/#comments</comments>
		<pubDate>Sat, 20 Jan 2007 11:47:13 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Impersonation]]></category>

		<guid isPermaLink="false">http://dimension/2007/01/20/browser-impersonation/</guid>
		<description><![CDATA[Sometimes it is necessary to impersonate a certain browser type. For example, some sites only allow certain types of browsers (e.g. IE, Firefox, etc.) and would consider other less commonly used browsers (e.g. Konqueror) as &#8220;not supported&#8221;. It is thus often useful for the code to impersonate as a different type of browser. As it [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-size: 10pt; font-family: Verdana;">Sometimes it is necessary to impersonate a certain browser type.</span><span id="more-136"></span><span style="font-size: 10pt; font-family: Verdana;"> For example, some sites only allow certain types of browsers (e.g. IE, Firefox, etc.) and would consider other less commonly used browsers (e.g. Konqueror) as &ldquo;not supported&rdquo;. It is thus often useful for the code to impersonate as a different type of browser.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>As it turns out, browser impersonation can be done very easily using .Net. The following code illustrates how this is done in C#:<o:p></o:p></span></p>
<blockquote>
<p class="MsoNormal"><o:p></o:p><strong><span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(43, 145, 175);">HttpWebRequest</span></strong><span style="font-size: 10pt; font-family: 'Courier New';"> r <span style="color: navy;">=</span> (<strong><span style="color: rgb(43, 145, 175);">HttpWebRequest</span></strong>)<strong><span style="color: rgb(43, 145, 175);">WebRequest</span></strong><span style="color: navy;">.</span>Create(<span style="color: rgb(163, 21, 21);">&quot;<em>{url}</em>&quot;</span>);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><o:p></o:p>r<span style="color: navy;">.</span>UserAgent <span style="color: navy;">=</span> <span style="color: rgb(163, 21, 21);">&quot;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)&quot;</span>;</span></p>
<p class="MsoNormal"><strong><span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(43, 145, 175);"><o:p></o:p>WebResponse</span></strong><span style="font-size: 10pt; font-family: 'Courier New';"> wResponse <span style="color: navy;">=</span> r<span style="color: navy;">.</span>GetResponse();<o:p></o:p></span></p>
<p class="MsoNormal"><strong><span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(43, 145, 175);"><o:p></o:p>Stream</span></strong><span style="font-size: 10pt; font-family: 'Courier New';"> rStream <span style="color: navy;">=</span> wResponse<span style="color: navy;">.</span>GetResponseStream();<o:p></o:p></span><strong><span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(43, 145, 175);"> </span></strong></p>
<p class="MsoNormal"><strong><span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(43, 145, 175);">StreamReader</span></strong><span style="font-size: 10pt; font-family: 'Courier New';"> reader <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: rgb(43, 145, 175);">StreamReader</span></strong>(rStream, <strong><span style="color: rgb(43, 145, 175);">Encoding</span></strong><span style="color: navy;">.</span>ASCII);<o:p></o:p></span> <strong><span style="font-size: 10pt; font-family: 'Courier New'; color: blue;">string</span></strong><span style="font-size: 10pt; font-family: 'Courier New';"> respHTML <span style="color: navy;">=</span> reader<span style="color: navy;">.</span>ReadToEnd();</span></p>
</blockquote>
<p class="MsoNormal"><o:p></o:p><span style="font-size: 10pt; font-family: Verdana;">The key is to set the UserAgent property of the HttpWebRequest object according to the type of browser that the code intends to impersonate. In the example given above, we were trying to impersonate IE7. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p>To impersonate Firefox 2.0, change the UserAgent to</span> <span style="font-size: 10pt; font-family: 'Courier New'; color: rgb(163, 21, 21);">&ldquo;Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1&rdquo;</span> <span style="font-size: 10pt; font-family: Verdana;">instead.</span></p>
<p><o:p></o:p><span style="font-size: 10pt; font-family: Verdana;">UserAgent value can be obtained from the HttpRequest object. Or you can go to http://www.kerrywong.com/httpreq/ to find out.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2007/01/20/browser-impersonation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using X509 Certificate with Web Service in ASP.Net &#8211; Update</title>
		<link>http://www.kerrywong.com/2006/12/23/using-x509-certificate-with-web-service-in-aspnet-update/</link>
		<comments>http://www.kerrywong.com/2006/12/23/using-x509-certificate-with-web-service-in-aspnet-update/#comments</comments>
		<pubDate>Sat, 23 Dec 2006 10:35:20 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[X509]]></category>

		<guid isPermaLink="false">http://dimension/2006/12/23/using-x509-certificate-with-web-service-in-aspnet-update/</guid>
		<description><![CDATA[In one of my previous posts, I described in detail for using X509 certificates with web services. Here are a couple more debugging tips that might also be helpful. RSA Key Length One reason that the secure communication fails to establish is the mismatch in private/public key pairs. Ensuring the keys are of the same [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">In one of my previous <a href="/2006/12/01/using-x509-certificate-with-web-service-in-aspnet/">posts</a>, I described in detail for using X509 certificates with web services.</span><span id="more-127"></span><span style="font-size: 10pt; font-family: Verdana;"> Here are a couple more debugging tips that might also be helpful.<o:p></o:p></span></p>
<ol type="1" start="1" style="margin-top: 0in;">
<li class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">RSA Key Length<o:p></o:p></span></li>
</ol>
<p style="margin-left: 0.25in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">One reason that the secure communication fails to establish is the mismatch in private/public key pairs. Ensuring the keys are of the same length should really be the first step before going much deeper into debugging.<o:p> </o:p></span></p>
<ol type="1" start="2" style="margin-top: 0in;">
<li class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Proxy Server<o:p></o:p></span></li>
</ol>
<p style="margin-left: 0.25in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">You should be aware that if you are using a proxy server for your http traffic (you can check your browser settings to see if you are using a proxy server to connect to the network) and if you run into this exception <o:p></o:p></span></p>
<blockquote>
<p style="margin-left: 0.25in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">&ldquo;The remote server returned an error: (502) Bad Gateway.&rdquo;<o:p></o:p></span></p>
</blockquote>
<p><span style="font-size: 10pt; font-family: Verdana;">Chances are that you are running through a proxy server and you should remove the proxy settings. And a simple fix would be to disable it (Internet Options -&gt; Connections -&gt; LAN Settings and uncheck Proxy Server). This will solve the problem.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2006/12/23/using-x509-certificate-with-web-service-in-aspnet-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using X509 Certificate with Web Service in ASP.Net</title>
		<link>http://www.kerrywong.com/2006/12/01/using-x509-certificate-with-web-service-in-aspnet/</link>
		<comments>http://www.kerrywong.com/2006/12/01/using-x509-certificate-with-web-service-in-aspnet/#comments</comments>
		<pubDate>Fri, 01 Dec 2006 09:23:58 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Web Service]]></category>
		<category><![CDATA[X509]]></category>

		<guid isPermaLink="false">http://dimension/2006/12/01/using-x509-certificate-with-web-service-in-aspnet/</guid>
		<description><![CDATA[Recently, I needed to incorporate a java web service (not developed in-house) into my client&#8217;s existing ASP.Net website. The web service calls need to be secured by utilizing X509 certificate over SSL. Surprisingly, I found very little information on this topic (there are some, but they are scattered here and there and the information was [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Recently, I needed to incorporate a java web service (not developed in-house) into my client&rsquo;s existing ASP.Net website.</span><span id="more-120"></span><span style="font-size: 10pt; font-family: Verdana;"> The web service calls need to be secured by utilizing X509 certificate over SSL.<o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Surprisingly, I found very little information on this topic (there are some, but they are scattered here and there and the information was not as complete as I hoped). And although the code to implement web service call using certificate is trivial (a few lines), debugging can be quite challenging. Here I will share some my experience. This discussion applies to calling all web services (not just limited to jws) from ASP.Net using certificates. Do not confuse what we are trying to achieve here with WSE (Web Service Enhancement), as communication with web services using X509 certificate can be done with or without WSE, it is an orthogonal concept. Here we are just using the plain web service, no WSE.<o:p></o:p></span></p>
<ol type="1" style="margin-top: 0in;">
<li class="MsoNormal"><strong><span style="font-size: 10pt; font-family: Verdana;">Importing certificates.<o:p></o:p></span></strong></li>
</ol>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The import process differs slightly for Windows XP and Windows Server 2003. For either operating system, you will need to import the certificate to the local machine store instead of personal store. To do this, run mmc (from start-&gt;run), and choose Add Snap-in (Ctrl+M), select Add to add &ldquo;Certificates&rdquo; to the certificate manager. When prompted for location, select Computer Account, and then Local Computer (the default is user account). We assume that the certificate is imported to one of the root certificate authorities (e.g. Third-Party Root Certificate Authorities).<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">ASP.Net typically runs under ASPNET_WP user under Windows XP and IIS_WPG under Windows 2003 server by default. On a Windows XP machine, the above steps are sufficient for ASP.Net to utilize the certificate imported. But for Windows 2003 server, you will need to grant the rights to the IIS_WPG user so that ASP.Net can access the certificate. This can be achieved by using Microsoft&rsquo;s winhttpcertcfg.exe utility (<a href="http://www.microsoft.com/downloads/details.aspx?familyid=C42E27AC-3409-40E9-8667-C748E422833F&amp;displaylang=en">download here</a>) as shown below:<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">winhttpcertcfg.exe -g -c LOCAL_MACHINE\Root -s &quot;{certificate name}&quot; -a &quot;IIS_WPG&quot;<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Note that ASP.Net worker process might be running under an account other then the ones mentioned above (i.e. customized to run under a particular user account), and in this case, you will need to grant permission to whatever process the work process is running under.<o:p></o:p></span></p>
<ol type="1" start="2" style="margin-top: 0in;">
<li class="MsoNormal"><strong><span style="font-size: 10pt; font-family: Verdana;">Access certificate in code<o:p></o:p></span></strong></li>
</ol>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">To use the certificate in code, only a few lines are needed (C#):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><font size="2" face="Courier New"><strong><span style="color: teal;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; X509Store</span></strong> store <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: teal;">X509Store</span></strong>(<strong><span style="color: teal;">StoreName</span></strong><span style="color: navy;">.</span>Root, <strong><span style="color: teal;">StoreLocation</span></strong><span style="color: navy;">.</span>LocalMachine);</font></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><font size="2" face="Courier New"> <span>            </span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; store<span style="color: navy;">.</span>Open(<strong><span style="color: teal;">OpenFlags</span></strong><span style="color: navy;">.</span>ReadOnly); <span>            </span><strong><span style="color: teal;">X509Certificate2Collection</span></strong> col <span style="color: navy;">=</span> </font></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: 'Courier New';"><font size="2" face="Courier New">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; store<span style="color: navy;">.</span>Certificates<span style="color: navy;">.</span>Find(<strong><span style="color: teal;">X509FindType</span></strong><span style="color: navy;">.</span>FindBySerialNumber, <span style="color: maroon;">&quot;{serial number no space}&quot;</span>, <strong><span style="color: blue;">true</span></strong>);<o:p> </o:p></font></span></p>
<p class="MsoNormal"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><span>            </span><span><font color="#008000">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //service is the webservice that need to</font> </span><span><font color="#008000">//be authenticated using X509 certificate</font> </span></span></font></p>
<p class="MsoNormal"><font size="2" face="Courier New"><span style="font-size: 10pt; font-family: 'Courier New';"><strong><span style="color: teal;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; TestWebService</span></strong> service <span style="color: navy;">=</span> <strong><span style="color: blue;">new</span></strong> <strong><span style="color: teal;">TestWebService</span></strong>();<o:p></o:p></span></font></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><font size="2" face="Courier New"><span>      </span></font><font size="2" face="Courier New" color="#008000"><span>//Note, we should find the certificate from the the<o:p></o:p> </span><span>//root certificate store on local machine if the <o:p></o:p> </span></font></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><font size="2" face="Courier New" color="#008000"><span>//certificate is imported correctly and the serial<o:p></o:p> </span></font><font size="2" face="Courier New"><span><font color="#008000">//number is correct</font><o:p></o:p> </span><strong><span style="color: blue;">if</span></strong> (col<span style="color: navy;">.</span>Count <span style="color: navy;">==</span> <span style="color: teal;">1</span>)<o:p></o:p> <span>      </span>{<o:p></o:p> </font><font size="2" face="Courier New" color="#008000"><span>          </span><span>//all we need to do is to add the certificate<o:p></o:p> </span></font><font size="2" face="Courier New"><span><font color="#008000">//after that we can use the webservice as usual</font><o:p></o:p> </span>service<span style="color: navy;">.</span>ClientCertificates<span style="color: navy;">.</span>Add(col[<span style="color: teal;">0</span>]); </font></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><font size="2" face="Courier New"><span>          </span>service<span style="color: navy;">.</span>Test();<o:p></o:p> <span>      </span></font></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><font size="2" face="Courier New">}</font><o:p> </o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The code above assumes that you are using .Net 2.0, since quite a few earlier methods in framework 1.0 have been deprecated. The serial number of a certificate can be found in the certificate store by clicking on the properties of the certificate:</span></p>
<p align="center" style="margin-left: 0.5in;" class="MsoNormal"><a title="View Certificates" href="/blog/wp-content/uploads/2007/09/certificate.JPG"><img alt="View Certificates" src="/blog/wp-content/uploads/2007/09/certificate.JPG" /></a></p>
<p align="left" style="margin-left: 0.5in;" class="MsoNormal">&nbsp;</p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Note that you will need to remove all the spaces in the serial number. Alternatively, you may also use any other X509FindType&rsquo;s (e.g. FindByIssuerName, FindBySubjectKey, etc.) to retrieve the certificate in the certificate store.<o:p></o:p></span></p>
<ol type="1" style="margin-top: 0in;">
<li class="MsoNormal"><strong><span style="font-size: 10pt; font-family: Verdana;">Debugging<o:p></o:p></span></strong></li>
</ol>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The first step in debugging is to make sure that you actually can access the certificate in code. Set a breakpoint at <em>col.Count</em> and make sure that it is 1. If <em>col.Count</em> is 0, you will need to go back and inspect whether step 1 and 2 were performed correctly.<o:p> </o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">So far we have verified that the certificate was imported correctly. Making sure that the certificate actually works would take a bit more patience if you are stuck with mysterious errors. <o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">One of the most common problems when you try to run the program after utilizing thee certificate is the error <o:p> </o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The request was aborted: Could not create SSL/TLS secure channel.<o:p></o:p> The underlying connection was closed: An unexpected error occurred on a send.<o:p> </o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">This is typically caused by incorrect certificate (e.g. key length, certificate type mismatch) settings. To help debugging the exact cause of this problem, you may want to include some trace listeners in your web.config (or app.config) file (see <a href="http://blogs.msdn.com/dgorti/">Durgaprasad Gorti&#8217;s Blog</a>).<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"><span>  </span>&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">system.diagnostics</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>    </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">trace</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">autoflush</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">true</span>&quot;<span>/</span></span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; </span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"><span>    </span>&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">sources</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; </span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"><span>      </span>&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">source</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">System.Net</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxdatasize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">1024</span>&quot;<span>&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">listeners</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; </span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"><span>          </span>&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">add</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">TraceFile</span>&quot;<span>/&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">listeners</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; </span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"><span>      </span>&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">source</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>      </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">source</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">System.Net.Sockets</span>&quot;<span style="color: blue;"> </span><span style="color: red;">maxdatasize</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">1024</span>&quot;<span>&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">listeners</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; </span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"><span>          </span>&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">add</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">TraceFile</span>&quot;<span>/&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">listeners</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>      </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">source</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>    </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">sources</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>    </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">sharedListeners</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>      </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">add</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">TraceFile</span>&quot;<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">System.Diagnostics.TextWriterTraceListener</span>&quot;<span style="color: blue;"> </span><span style="color: red;">initializeData</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">trace.log</span>&quot;<span>/&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">sharedListeners</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>    </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">switches</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>      </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">add</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">System.Net</span>&quot;<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Verbose</span>&quot;<span> /&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">add</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';"> </span><span style="font-size: 10pt; color: red; font-family: 'Courier New';">name</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">=</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&quot;<span style="color: blue;">System.Net.Sockets</span>&quot;<span style="color: blue;"> </span><span style="color: red;">value</span><span style="color: blue;">=</span>&quot;<span style="color: blue;">Verbose</span>&quot;<span> /&gt; </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">switches</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt; <span>  </span></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&lt;/</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New';">system.diagnostics</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New';">&gt;<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">When you run your ASP.Net project with this configuration section, you will see a trace file (trace.log) generated in your project folder when you run your web project. When your code errors out with the exception <o:p> </o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The request was aborted: Could not create SSL/TLS secure channel.<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Take a look at trace file, you should see that the service found the certificate on your computer (assume that the certificate is imported correctly, if not go to step 1 and 2), and you might also find something similar to the following (just an example to give you an idea what to look for):<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [1916] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 6019cc0:1749d0, targetName = {<em>IP Address</em>}, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [1916] InitializeSecurityContext(In-Buffer length=7, Out-Buffer length=0, returned code=<strong>IllegalMessage</strong>).<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Error: 0 : [1916] Exception in the HttpWebRequest#37256635::EndGetResponse &#8211; <strong>The request was aborted: Could not create SSL/TLS secure channel.</strong><o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">The above trace information generally indicates that the certificate used is not correct. You might want to double check the RSA key length and certificate type to ensure that they are correct before investigating further. There is a lot of information in the trace file, so you might need to comb through it a couple of times before you find the problem. If your certificate is a test certificate you might want to change the last argument passed to </span><span style="font-size: 10pt; font-family: 'Courier New';">store<span style="color: navy;">.</span>Certificates<span style="color: navy;">.</span>Find</span><span style="font-size: 10pt; font-family: Verdana;"> is set to false because the test certificate might not be valid.<o:p></o:p></span></p>
<p style="margin-left: 0.5in;" class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Another common problem for this error is that the account under which ASP.Net is running does not have proper permissions to access the provided certificate. This situation typically happens on Windows 2003 server where ASP.Net is running under IIS_WPG. This situation can be confirmed from the following trace messages (occurs right before InitializeSecurityContext):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;"><o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [4608] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent<span>  </span>= Outbound, scc<span>     </span>= System.Net.SecureCredential)<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Error: 0 : [4608] <strong>AcquireCredentialsHandle() failed with error 0X8009030D</strong>.<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [4608] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent<span>  </span>= Outbound, scc<span>     </span>= System.Net.SecureCredential)<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Error: 0 : [4608] Exception in the HttpWebRequest#34090260::EndGetResponse &#8211; <strong>The request was aborted: Could not create SSL/TLS secure channel.<o:p></o:p></strong></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">If you get the AcquireCredentialsHandle() failed message, you might want to double check step 1 to see if you have granted the ASP.Net worker process the correct permission (using winhttpcertcfg.exe).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">A successful hand shake looks like this (only a small portion is shown):<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [3048] SecureChannel#56552832 &#8211; Certificate is of type X509Certificate2 and contains the private key.<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [3048] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent<span>  </span>= Outbound, scc<span>     </span>= System.Net.SecureCredential)<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [3048] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = {<em>IP Address</em>}, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [3048] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=70, returned code=ContinueNeeded).<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Socket#49783578::Send()<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Data from Socket#49783578::Send<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000000 : 16 03 01 00 41 01 00 00-3D 03 01 45 70 5F AF FD : &#8230;.A&#8230;=..Ep_..<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000010 : BA F6 F1 25 72 36 E5 48-A6 A6 C0 8D C7 67 C9 C8 : &#8230;%r6.H&#8230;..g..<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000020 : DE 6F C1 D2 23 C3 37 66-9B 0F 7A 00 00 16 00 04 : .o..#.7f..z&#8230;..<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000030 : 00 05 00 0A 00 09 00 64-00 62 00 03 00 06 00 13 : &#8230;&#8230;.d.b&#8230;&#8230;<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000040 : 00 12 00 63 01 00<span>                               </span>: &#8230;c..<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Exiting Socket#49783578::Send() <span>    </span>-&gt; 70#70<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Socket#49783578::Receive()<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Data from Socket#49783578::Receive<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000000 : 16 03 01 00 2A<span>                                  </span>: &#8230;.*<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Exiting Socket#49783578::Receive() -&gt; 5#5<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Socket#49783578::Receive()<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Data from Socket#49783578::Receive<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000005 : 02 00 00 26 03 01 61 66-83 6C 31 45 99 CE EC 91 : &#8230;&amp;..af.l1E&#8230;.<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000015 : 32 C8 7D 7B 23 41 36 8B-04 C9 99 33 97 6E B3 9D : 2.}{#A6&#8230;.3.n..<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] 00000025 : 32 79 2C F7 93 94 00 00-04 00<span>                   </span>: 2y,&#8230;&#8230;.<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net.Sockets Verbose: 0 : [3048] Exiting Socket#49783578::Receive() -&gt; 42#42<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [3048] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = 6008b48:f6028, targetName = {<em>IP Address</em>}, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)<o:p></o:p></span></p>
<p style="margin-left: 1in;" class="MsoNormal"><span style="font-size: 9pt; font-family: 'Courier New';">System.Net Information: 0 : [3048] InitializeSecurityContext(In-Buffer length=47, Out-Buffer length=0, returned code=ContinueNeeded).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Also, consuming web services via SSL using X509 certificate does not require the web services being accessed via port 443 (standard SSL port), it can be any port as long as the URL is prefixed with https. Nor does it require the whole website being secured by SSL. It is perfectly fine for a non secure website to access a secure web service. The certificate in our scenario is used to authenticate the caller of a web service and the service being called does not care whether or not the call is originated from a secure website as long as the certificate presented is valid.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Even though calling web services can be done securely using X509 certificates without using WSE (see link at the end), it is highly recommended that WSE be used whenever it is possible because WSE implements more security features then just the initial handshake. <o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 10pt; font-family: Verdana;">Here are some related links you might find worth reading:<o:p></o:p></span></p>
<p><span style="font-size: 10pt; font-family: Verdana;"><a href="http://www.microsoft.com/downloads/details.aspx?familyid=C42E27AC-3409-40E9-8667-C748E422833F&amp;displaylang=en">Windows HTTP Services Certificate Configuration Tool (WinHttpCertCfg.exe)</a><o:p> </o:p></span></p>
<p><span style="font-size: 10pt; font-family: Verdana;"><a href="http://technet.microsoft.com/en-us/library/bb457027.aspx">Certificate Revocation and Status Checking</a> </span></p>
<p><span style="font-size: 10pt; font-family: Verdana;"><o:p> </o:p><a href="http://msdn2.microsoft.com/en-us/library/bfazk0tb.aspx">How to: Configure an XML Web Service for Windows Authentication</a>  </span></p>
<p><o:p> </o:p><a href="http://support.microsoft.com/kb/823177">PRB: &quot;System.Net.WebException. The Underlying Connection Was Closed. Could Not Establish Trust Relationship with Remote Server.&quot; </a><a href="http://support.microsoft.com/kb/823177">Error Message When You Upgrade the .NET Framework</a> <o:p> </o:p></p>
<p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wse3.0/html/6f194c77-6fe3-42a2-88ee-0599d4b4779c.asp">Web Services Enhancements 3.0<span>  </span></a><o:p></o:p><o:p> </o:p></p>
<p><a href="http://support.microsoft.com/kb/901183">How to call a Web service by using a client certificate for authentication in an ASP.NET Web application</a><o:p> </o:p></p>
<p><o:p></o:p><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHt14.asp">Building Secure ASP.NET Applications: Authentication, Authorization, and Secure Communication</a> <o:p></o:p></p>
<p><a href="http://msdn2.microsoft.com/en-us/library/w67h0dw7.aspx">Securing XML Web Services Created Using ASP.NET</a></p>
<p><o:p> </o:p><a href="http://msdn2.microsoft.com/en-us/library/9z52by6a.aspx">How to: Perform Custom Authentication Using SOAP Headers</a><span>  </span><o:p> </o:p></p>
<p><a href="http://msdn2.microsoft.com/en-us/library/ms996415.aspx">HTTP Security and ASP.NET Web Services</a></p>
<p><o:p> </o:p><a href="http://support.microsoft.com/kb/895971">How to send a client certificate by using the HttpWebRequest and HttpWebResponse classes in Microsoft Visual C# .NET</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2006/12/01/using-x509-certificate-with-web-service-in-aspnet/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Javascript Popup Window Problem in ASP.Net 2.0</title>
		<link>http://www.kerrywong.com/2006/11/15/javascript-popup-window-problem-in-aspnet-20/</link>
		<comments>http://www.kerrywong.com/2006/11/15/javascript-popup-window-problem-in-aspnet-20/#comments</comments>
		<pubDate>Wed, 15 Nov 2006 11:17:31 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://dimension/2006/11/15/javascript-popup-window-problem-in-aspnet-20/</guid>
		<description><![CDATA[Recently, I found an interesting problem when using server controls (e.g. asp:button) to launch javascript popup windows. For example, the following code should be able to pop up a window (the javascript for popUp() is omitted here): &#60;asp:Button ID=&#8221;Button1&#8243; runat=&#8221;server&#8221; CausesValidation=&#8221;False&#8221; OnClientClick=&#8221;javascript:popUp()&#8221; Text=&#8221;Test&#8221; /&#62; And it works as long as you don’t have a default [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I found an interesting problem when using server controls (e.g. asp:button) to launch javascript popup windows.<span id="more-116"></span> For example, the following code should be able to pop up a window (the javascript for popUp() is omitted here):</p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New'">asp</span><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">:</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New'">Button</span><span style="font-size: 10pt; font-family: 'Courier New'"><font color="#000000"> </font><span style="color: red">ID</span><span style="color: blue">=&#8221;Button1&#8243;</span><font color="#000000"> </font><span style="color: red">runat</span><span style="color: blue">=&#8221;server&#8221;</span><font color="#000000"> </font><span style="color: red">CausesValidation</span><span style="color: blue">=&#8221;False&#8221;</span><font color="#000000"> </font><span style="color: red">OnClientClick</span><span style="color: blue">=&#8221;javascript:popUp()&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 'Courier New'"><span><font color="#000000">            </font></span><span style="color: red">Text</span><span style="color: blue">=&#8221;Test&#8221;</span><font color="#000000"> </font><span style="color: blue">/&gt;<o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><o:p><font color="#000000" face="Times New Roman" size="3"> </font></o:p></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-family: Verdana"><font color="#000000">And it works as long as you don’t have a default button or default focus on the form. If you add either a default button or a default focus or both to the form, the popup window will become a pop under window (i.e. it stays underneath the window from which the popup is launched).<o:p></o:p></font></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New'">form</span><span style="font-size: 10pt; font-family: 'Courier New'"><font color="#000000"> </font><span style="color: red">id</span><span style="color: blue">=&#8221;form1&#8243;</span><font color="#000000"> </font><span style="color: red">runat</span><span style="color: blue">=&#8221;server&#8221;</span><font color="#000000"> </font><span style="color: red">defaultbutton</span><span style="color: blue">=&#8221;Button1&#8243;</span><font color="#000000"> </font><span style="color: red">defaultfocus</span><span style="color: blue">=&#8221;TextBox1&#8243;&gt;<o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><font color="#000000"><span style="font-family: Verdana">So, how to solve this problem? Well, basically there are two ways. The first one is to use an HTML input element instead of the asp:Button control:</span><span style="font-size: 10pt; color: blue; font-family: Verdana"><o:p></o:p></span></font></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'">&lt;</span><span style="font-size: 10pt; color: maroon; font-family: 'Courier New'">input</span><span style="font-size: 10pt; font-family: 'Courier New'"><font color="#000000"> </font><span style="color: red">id</span><span style="color: blue">=&#8221;Button1&#8243;</span><font color="#000000"> </font><span style="color: red">type</span><span style="color: blue">=&#8221;button&#8221;</span><font color="#000000"> </font><span style="color: red">value</span><span style="color: blue">=&#8221;Test&#8221;</span><font color="#000000"> </font><span style="color: red">onclick</span><span style="color: blue">=&#8221;javascript: popUp()&#8221;/&gt;<o:p></o:p></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: blue; font-family: 'Courier New'"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt"><span style="font-family: Verdana"><font color="#000000">And the second approach is (as we did before ASP.Net 2.0) to incorporate some client side javascript to set default button and default focus instead of relying on the defaultbutton and defaultfocus attributes in the form tag.<o:p></o:p></font></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2006/11/15/javascript-popup-window-problem-in-aspnet-20/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
