<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DUMDQXc6eSp7ImA9WhRVEko.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434</id><updated>2012-01-11T02:24:30.911-08:00</updated><category term="C#" /><category term="Seo" /><category term="Globalization" /><category term="xml" /><category term="LINQ" /><category term="css" /><category term="javascript" /><category term="State Management" /><category term="configuration" /><category term="Captcha" /><category term="UserControl" /><category term="Validation" /><category term="Master page" /><category term="asp.net" /><category term="COM" /><category term="Data Control" /><category term="Handler" /><category term="Ajax" /><category term="Web Control" /><title>.NET World</title><subtitle type="html">Find solutions to .NET problems.</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>83</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/BitsNPieces-Aspnet" /><feedburner:info uri="bitsnpieces-aspnet" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><entry gd:etag="W/&quot;DEMHR3w_eip7ImA9WhdXEUQ.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-6766041423446889104</id><published>2011-08-24T07:33:00.001-07:00</published><updated>2011-08-24T07:33:56.242-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-08-24T07:33:56.242-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="Validation" /><title>Validating a range of dates using RangeValidator</title><content type="html">&lt;p&gt;RangeValidator is a &lt;a href="http://shahed-kazi.blogspot.com/search/label/Validation"&gt;validation control&lt;/a&gt; provided by asp.net. This control can be used to validate a range of string, date, integer and double values. In this post, I will show how to use the RangeValidator to validate a range of date using today’s date as the maximum value.&lt;/p&gt; &lt;p&gt;The following is the RangeValidator markup.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6461c4a0-1a1d-4990-b464-787eb5eb3430" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;RangeValidator markup&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;RangeValidator&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;RV1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ControlToValidate&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Date&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;MinimumValue&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;01/01/2000&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ErrorMessage&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;*&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;RangeValidator&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The &lt;a href="http://shahed-kazi.blogspot.com/2009/02/declaratively-use-aspnet-textbox.html"&gt;TextBox&lt;/a&gt; control is added where the users will enter the value. The &lt;a title="programmatically add button control" href="http://shahed-kazi.blogspot.com/2009/07/dynamically-add-button-control-to.html"&gt;Button&lt;/a&gt; control is added to test validation. The RangeValidator is added – its ControlToValidate property is set to TextBox’s id, data type is set to Date, the minimum value is set to an arbitrary date. The maximum value is set in the Page_Load event as shown below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:582f62e8-e705-4964-8647-bb61b799a6b5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Page_Load event&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    RV1.MaximumValue = &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;.Today.ToShortDateString();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The maximum value expects a value of type string, so the string format of today’s date is passed as the MaximumValue.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-6766041423446889104?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/6766041423446889104/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=6766041423446889104" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6766041423446889104?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6766041423446889104?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/08/validating-range-of-dates-using.html" title="Validating a range of dates using RangeValidator" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUcCQnY8fyp7ImA9WhdSF0U.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-2415940532121953886</id><published>2011-07-27T09:11:00.001-07:00</published><updated>2011-07-27T09:11:03.877-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-27T09:11:03.877-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="xml" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>Transform object to xml using linq</title><content type="html">&lt;p&gt;Using Linq, it’s easily possible to query various types of data like sql database, xml, objects like DataTable, ArrayList. It’s also possible to transform data into other formats like xml using linq. In this article, I will demonstrate how to query an System.Collections.ArrayList collection and then to save the data in xml format.&lt;/p&gt; &lt;p&gt;At first, I will create an ArrayList object and add Person objects to it. The code to do is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:23917567-afe9-4560-9cd9-f867b518457f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Adding objects to ArrayList&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#2b91af"&gt;ArrayList&lt;/span&gt; list = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ArrayList&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Scooby&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Doo&amp;quot;&lt;/span&gt;, DateOfBirth= &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;(2000, 12, 31), Height=160 });&lt;/li&gt; &lt;li&gt;list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Bugs&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Bunny&amp;quot;&lt;/span&gt;, DateOfBirth = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;(1999, 2, 7), Height = 170 });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Peter&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Pan&amp;quot;&lt;/span&gt;, DateOfBirth = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;(2001, 10, 31), Height = 150 });&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The Person class has 4 public properties of different data types – string, DateTime and int as shown below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:71df475a-e7b0-49ef-aafa-2559dd024d92" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Person class&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1.CSClass&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; FirstName { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; LastName { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt; DateOfBirth { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; Height { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ToString()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; FirstName + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + LastName;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;Person objects are added to the ArrayList list which can be queried using linq as shown in my previous post on &lt;a href="http://shahed-kazi.blogspot.com/2011/05/how-to-query-arraylist-with-linq.html"&gt;query ArrayList using Linq&lt;/a&gt;. While querying the list, System.Xml.Linq.XElement objects can be created which can be saved as xml using System.Xml.Linq.XDocument’s Save() method. The code to show this is below.&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6c97ca9e-c139-4033-8f6d-2065be87b187" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Transform ArrayList to xml&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; TransformToXml()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;ArrayList&lt;/span&gt; list = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ArrayList&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Scooby&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Doo&amp;quot;&lt;/span&gt;, DateOfBirth= &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;(2000, 12, 31), Height=160 });&lt;/li&gt; &lt;li&gt;    list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Bugs&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Bunny&amp;quot;&lt;/span&gt;, DateOfBirth = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;(1999, 2, 7), Height = 170 });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Peter&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Pan&amp;quot;&lt;/span&gt;, DateOfBirth = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;(2001, 10, 31), Height = 150 });&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; q = &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; p &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; list&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;orderby&lt;/span&gt; p.FirstName&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;orderby&lt;/span&gt; p.LastName&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; p;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; query = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Persons&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; p &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; list&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                                &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;,&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;, p.FirstName),&lt;/li&gt; &lt;li&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt;, p.LastName),&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;DateOfBirth&amp;quot;&lt;/span&gt;, p.DateOfBirth),&lt;/li&gt; &lt;li&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Height&amp;quot;&lt;/span&gt;, p.Height)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                )&lt;/li&gt; &lt;li&gt;                                );&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt; doc = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt;(query);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    doc.Save(Server.MapPath(&lt;span style="color:#a31515"&gt;&amp;quot;transform.xml&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; query2 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Persons&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; p &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; list&lt;/li&gt; &lt;li&gt;                                &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Person&amp;quot;&lt;/span&gt;,&lt;/li&gt; &lt;li&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;FirstName&amp;quot;&lt;/span&gt;, p.FirstName),&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;LastName&amp;quot;&lt;/span&gt;, p.LastName),&lt;/li&gt; &lt;li&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;DateOfBirth&amp;quot;&lt;/span&gt;, p.DateOfBirth),&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Height&amp;quot;&lt;/span&gt;, p.Height)&lt;/li&gt; &lt;li&gt;                                )&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                                );&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt; doc2 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt;(query2);&lt;/li&gt; &lt;li&gt;    doc2.Save(Server.MapPath(&lt;span style="color:#a31515"&gt;&amp;quot;transform2.xml&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;As can be seen, the ArrayList object is queried for Person objects. This is not really needed for the code to work but is an example on how objects can be queried from ArrayList. &lt;/p&gt; &lt;p&gt;Using another query, “query”, XElement objects are created. The names of the XElement – that is, the names of the xml elements are arbitrary and the value of the elements are obtained from the Person object. The XElement objects are then added to XDocument object and saved as xml using XDocument.Save() method. The resultant xml looks like below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c788f095-8441-412b-a52e-9ff0dcef7bd5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Xml from Linq to ArrayList&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;?&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Persons&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Scooby&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Doo&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;2000-12-31T00:00:00&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;160&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Bugs&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Bunny&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;1999-02-07T00:00:00&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;170&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Peter&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Pan&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;2001-10-31T00:00:00&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;150&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Persons&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;One thing to note is that the xml declaration is automatically added. DateTime field – DateOfBirth is automatically converted to its string representation.&lt;/p&gt; &lt;p&gt;The xml produced previously contains elements only. The Linq query could be easily updated to change the data to be the attributes of xml elements. This is shown in another query – “query2”. System.Xml.Linq.XAttribute objects are created similarly instead of XElement objects and added to XElement object which is then added to XDocument object and saved as xml. The resultant xml file looks like below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1b6777a3-87c0-4490-87a5-68234b0463e1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Xml from Linq to ArrayList&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;?&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Persons&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Scooby&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Doo&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;2000-12-31T00:00:00&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;160&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; /&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Bugs&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Bunny&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1999-02-07T00:00:00&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;170&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; /&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Person&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;FirstName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Peter&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;LastName&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Pan&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;DateOfBirth&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;2001-10-31T00:00:00&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;Height&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;150&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; /&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Persons&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The data looks similar to the previous xml except that they are now attributes of “Person” element instead of child elements.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-2415940532121953886?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/2415940532121953886/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=2415940532121953886" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/2415940532121953886?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/2415940532121953886?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/07/transform-object-to-xml-using-linq.html" title="Transform object to xml using linq" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0UHRXk9fSp7ImA9WhZaEUo.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-4955672137278141049</id><published>2011-06-27T03:40:00.001-07:00</published><updated>2011-06-27T03:40:34.765-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-27T03:40:34.765-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="xml" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>Create XML using Linq to XML</title><content type="html">&lt;p&gt;Xml stands for e&lt;span style="background-color: yellow"&gt;X&lt;/span&gt;tensible &lt;span style="background-color: yellow"&gt;M&lt;/span&gt;arkup &lt;span style="background-color: yellow"&gt;L&lt;/span&gt;anguage that is designed to store and transfer data. An example xml file is below.&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d8fca0f2-d106-42a3-b1e3-269aeed9e74e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Sample Xml file&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;standalone&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;no&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;?&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;!--&lt;/span&gt;&lt;span style="color:#008000"&gt;demo on how to create xml file&lt;/span&gt;&lt;span style="color:#0000ff"&gt;--&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Country&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Australia&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;capital&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Canberra&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;New South Wales&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Victoria&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Country&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Country&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;2&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;India&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;capital&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;New Delhi&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Maharashtra&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Rajasthan&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;state&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Country&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Country&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;2&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Bangladesh&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;capital&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;Dhaka&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;&amp;gt;&amp;lt;![CDATA[&lt;/span&gt;&lt;span style="color:#808080"&gt;Bangladesh doesn&amp;#39;t have any states.&lt;/span&gt;&lt;span style="color:#0000ff"&gt;]]&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Country&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;root&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt; &lt;p&gt;Xml file starts the xml declaration as in the first line. It supports comments similar to html comments as shown in line 2. It can have elements, can have attributes and can contain CData that is data that can be considered as plain text. Line 4 shows an example of element and attributes – Country is an element and id, name and capital are attributes. The value 1, Australia, etc are the values of the attributes.&lt;/p&gt; &lt;p&gt;In .NET, Linq to Xml makes it easy to create and parse xml documents. It’s easy to create xml, add declarations, add elements, attributes and also add data appropriately. The necessary classes are within the System.Xml.Linq namespace. Linq to Xml has XDocument class with xml documents can be created and loaded. XComment class can be used to add comments. XElement can be used to create elements and XAttribute classes can be used to add attributes to elements. XCData class can be used to add data in cdata format. An example on how to create xml using Linq to Xml is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9bc39438-8e03-4f92-b731-fd3236ac8ebf" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Create xml using Linq&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; CreateXml()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt; doc = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XDocument&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XDeclaration&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;utf-8&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;no&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XComment&lt;/span&gt; comment = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XComment&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;demo on how to create xml file&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    doc.Add(comment);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; el = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;root&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p1 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Country&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;1&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Australia&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;capital&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Canberra&amp;quot;&lt;/span&gt;) });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p1city1 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;state&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;New South Wales&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p1city2 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;state&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Victoria&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    p1.Add(p1city1);&lt;/li&gt; &lt;li&gt;    p1.Add(p1city2);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p2 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Country&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;2&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;India&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;capital&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;New Delhi&amp;quot;&lt;/span&gt;) });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p2city1 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;state&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Maharashtra&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p2city2 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;state&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Rajasthan&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    p2.Add(p2city1);&lt;/li&gt; &lt;li&gt;    p2.Add(p2city2);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt; p3 = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XElement&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Country&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;id&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;3&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Bangladesh&amp;quot;&lt;/span&gt;), &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XAttribute&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;capital&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Dhaka&amp;quot;&lt;/span&gt;) });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    p3.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;XCData&lt;/span&gt;(&lt;span style="color:#a31515"&gt;&amp;quot;Bangladesh doesn&amp;#39;t have any states.&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    el.Add(p1);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    el.Add(p2);&lt;/li&gt; &lt;li&gt;    el.Add(p3);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    doc.Add(el);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    doc.Save(Server.MapPath(&lt;span style="color:#a31515"&gt;&amp;quot;country.xml&amp;quot;&lt;/span&gt;));            &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;As can be seen, once the XElement, XComment, XAttribute objects are created, they got to be added to parent object. Once the necessary data has been added, the xml file can be saved using XDocument’s Save method as in line 34 above.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-4955672137278141049?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/4955672137278141049/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=4955672137278141049" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4955672137278141049?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4955672137278141049?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/06/create-xml-using-linq-to-xml.html" title="Create XML using Linq to XML" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C08GSH07fSp7ImA9WhZbEk4.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-6939247022293642719</id><published>2011-06-16T06:39:00.001-07:00</published><updated>2011-06-16T06:43:49.305-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-16T06:43:49.305-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>Query DataTable using Linq</title><content type="html">&lt;p&gt;In this example, I will show how to use Linq to query &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.datatable.aspx" target="_blank"&gt;DataTable&lt;/a&gt;. A DataTable is a table of data stored in memory. Since Linq can query data of IEnumerable type, the data rows are declared as as IEnumerable so that Linq can query the data.&lt;/p&gt; &lt;p&gt;I have already setup the web application and have added a page. I will now define the schema of the DataTable and then create it. The code for creating the DataTable is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:380769be-de08-455c-aa3e-053ba2ab2682" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Create DataTable&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt; GetDataTable()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt; dt = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;//add columns&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    dt.Columns.Add(&lt;span style="color:#a31515"&gt;&amp;quot;ID&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Int32&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;    dt.Columns.Add(&lt;span style="color:#a31515"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    dt.Columns.Add(&lt;span style="color:#a31515"&gt;&amp;quot;DateOfBirth&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#008000"&gt;//add rows of data&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { 1, &lt;span style="color:#a31515"&gt;&amp;quot;Bugs Bunny&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;.Now});&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { 2, &lt;span style="color:#a31515"&gt;&amp;quot;Daffy Duck&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;.Now });&lt;/li&gt; &lt;li&gt;    dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { 3, &lt;span style="color:#a31515"&gt;&amp;quot;Scooby Doo&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af"&gt;DateTime&lt;/span&gt;.Now });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; dt;        &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;As can be seen, columns of type int, string and DateTime are added to the DataTable. Then, rows of data are added. This DataTable is later used for querying using Linq.&lt;/p&gt; &lt;p&gt;As mentioned earlier, to query DataTable using Linq, it has to be of type System.Collections.Generic.IEnumerable . To do this, DataTable’s &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx" target="_blank"&gt;AsEnumerable&lt;/a&gt; method can be used. This method converts the standard DataTable to a DataTable of IEnumerable type. The method for converting and querying the DataTable is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:82675c5a-c19a-4de4-a441-6810da4bbfba" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Query DataTable&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; QueryDataTable(&lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt; dt)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; q = &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DataRow&lt;/span&gt; row &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; dt.AsEnumerable()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; row;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; col = dt.Columns.Count;&lt;span style="color:#008000"&gt;//no of columns&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;DataRow&lt;/span&gt; row &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; q)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; col; i++)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            L1.Text += row[i].ToString() + &lt;span style="color:#a31515"&gt;&amp;quot;&amp;amp;nbsp;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        L1.Text += &lt;span style="color:#a31515"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;At first, I have converted the DataTable to of type IEnumerable and have selected all the rows. Then, I have queried each of the DataRow and retrieved the values for each of the columns. L1 is a Literal control that has been declared on the aspx page.&lt;/p&gt; &lt;p&gt;Please note that, I have simply added the values returned in string variables, ideally, a System.Text.StringBuilder object should be used to achieve this.&lt;/p&gt; &lt;p&gt;The above methods can then be called from Page_Load or other methods as shown below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f23b55ce-2c3e-450c-a62b-89ed873e193a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Page_Load event&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    QueryDataTable(GetDataTable());&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-6939247022293642719?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/6939247022293642719/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=6939247022293642719" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6939247022293642719?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6939247022293642719?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/06/query-datatable-using-linq.html" title="Query DataTable using Linq" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkUDR3s4eyp7ImA9WhZUEkk.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-2150850140400129792</id><published>2011-06-04T20:24:00.001-07:00</published><updated>2011-06-04T20:24:36.533-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-06-04T20:24:36.533-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Access anchor link from code-behind</title><content type="html">&lt;p&gt;Html anchor links are a way to navigate within a page. The anchor can be considered as a bookmark within a page, which when clicked takes the user to that section of a page. An anchor to a certain section of a page can be set in the following way.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4d19b4a3-4cbc-49f5-bcdc-a4e69c27ac13" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&amp;lt;a name=anchor&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;div&amp;gt;content&amp;lt;/div&amp;gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The name attribute of the “a” element determines the anchor / bookmark to the page. The anchor can be accessed by setting the link like below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c15e8a92-569f-4990-b804-e29bc6362d5d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #fff; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0; padding: 0 0 0 5px;"&gt; &lt;li&gt;&amp;lt;a href=#anchor&amp;gt;Anchor&amp;lt;/a&amp;gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;However, the anchor link is a client side thing and cannot be accessed from server-side. In this post, I am going to show how the anchor link can be accessed from server-side using hidden field and javascript. The following is the code for the aspx page.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:6979bcd9-e8fc-4d9c-ae43-194c0285bc35" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;aspx page&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;body&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;form&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;form1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;a&lt;/span&gt; &lt;span style="color:#ff0000"&gt;name&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;anchor&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;a&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Some content &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;a&lt;/span&gt; &lt;span style="color:#ff0000"&gt;href&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;#anchor&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onclick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;SetUrl(&amp;#39;anchor&amp;#39;)&amp;quot;&amp;gt;&lt;/span&gt;click here&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;a&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;HiddenField&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;HF1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;B1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Check Anchor&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;OnClick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;GetAnchor&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Literal&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;L1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Literal&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        SetUrl(&lt;span style="color:#800000"&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;form&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;body&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The page is simple, contains a HiddenField, Button and Literal control to display the anchor, if any. At the bottom of the page, before the closing form tag is a javascript method, SetUrl(“”) that sets the anchor in the hidden field. The code for javascript method is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:5dac0cfb-a1b9-48f3-8ae8-74baecbcf92c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;JavaScript code&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;function&lt;/span&gt; SetUrl(val) {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (val == &lt;span style="color:#800000"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; location.href.indexOf(&lt;span style="color:#800000"&gt;&amp;quot;#&amp;quot;&lt;/span&gt;) &amp;gt; -1) {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= HF1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value = location.href; &lt;span style="color:#006400"&gt;//set value of HiddenField&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (val == &lt;span style="color:#800000"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; location.href.indexOf(&lt;span style="color:#800000"&gt;&amp;quot;#&amp;quot;&lt;/span&gt;) == -1) {&lt;/li&gt; &lt;li&gt;            document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= HF1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value = &lt;span style="color:#800000"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= HF1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value = val;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        alert(val);&lt;/li&gt; &lt;li&gt;    }    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The javascript method, checks if there is a value for the anchor when the link is clicked. If not and if it does not contain the “#” character in the url, it does not set the value of the anchor in the HiddenField. Otherwise, it either sets the value when clicked or sets the url if it contains “#” character.&lt;/p&gt; &lt;p&gt;Once the HiddenField value is set, the value can be accessed from the server side. In this example, when the button is clicked, the value is obtained and set as the Text property of the Literal control. Here is the code for Button click event.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:67fe0106-3d6f-42ed-93c0-fcd4a8c848d5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Button click event&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; GetAnchor(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; anchor = HF1.Value;&lt;span style="color:#008000"&gt;//get url from hidden field&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (anchor.Contains(&lt;span style="color:#a31515"&gt;&amp;quot;#&amp;quot;&lt;/span&gt;))&lt;span style="color:#008000"&gt;//check if it an anchor&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] list = anchor.Split(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;char&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;#39;#&amp;#39;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;            L1.Text = &lt;span style="color:#a31515"&gt;&amp;quot;anchor name is :&amp;quot;&lt;/span&gt; + list[1];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (anchor != &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            L1.Text = anchor;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            L1.Text = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The code finds the value of the HiddenField and displays it in the Literal control.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-2150850140400129792?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/2150850140400129792/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=2150850140400129792" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/2150850140400129792?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/2150850140400129792?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/06/access-anchor-link-from-code-behind.html" title="Access anchor link from code-behind" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;Ck8MR34-fSp7ImA9WhZVF0o.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-696247077060698709</id><published>2011-05-30T08:54:00.001-07:00</published><updated>2011-05-30T08:54:46.055-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-30T08:54:46.055-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><category scheme="http://www.blogger.com/atom/ns#" term="LINQ" /><title>How to query an ArrayList with LINQ</title><content type="html">&lt;p&gt;In this example, I will show how to query an ArrayList using Linq. Since ArrayList are non-generic IEnumerable collections, the type of variable in the collection must be explicitly declared in the linq query.&lt;/p&gt; &lt;p&gt;I already have a web application setup, so, I will just add a class called Person. This is a very simple class containing only 2 properties – FirstName and LastName. The code for the class is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:feb66f9a-b92a-4958-9580-7a719789977c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Person Class&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1.CSClass&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; FirstName { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; LastName { &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;; &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;; }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;override&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ToString()&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; FirstName + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + LastName;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;I will then add a web form where I will add an ArrayList, add Person objects to the collection and then query the collection using Linq. The code for this is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b148492e-3a8b-4faf-9f9b-541e0418a767" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Query ArrayList with LINQ&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web.UI;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web.UI.WebControls;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; WebApplication1.CSClass;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;LinqText&lt;/span&gt; : System.Web.UI.&lt;span style="color:#2b91af"&gt;Page&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            GetList();&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; GetList()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;ArrayList&lt;/span&gt; list = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;ArrayList&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Scooby&amp;quot;&lt;/span&gt;, LastName=&lt;span style="color:#a31515"&gt;&amp;quot;Doo&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;            list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Bugs&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Bunny&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            list.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; { FirstName = &lt;span style="color:#a31515"&gt;&amp;quot;Peter&amp;quot;&lt;/span&gt;, LastName = &lt;span style="color:#a31515"&gt;&amp;quot;Pan&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; q = &lt;span style="color:#0000ff"&gt;from&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; p &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; list&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;orderby&lt;/span&gt; p .FirstName&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;orderby&lt;/span&gt; p.LastName&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;select&lt;/span&gt; p;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; br = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Person&lt;/span&gt; p &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; q)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                L1.Text += p.ToString() + br;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Note that, in the linq query, the object type is specified. The query will not work unless it is specified.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-696247077060698709?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/696247077060698709/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=696247077060698709" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/696247077060698709?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/696247077060698709?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/05/how-to-query-arraylist-with-linq.html" title="How to query an ArrayList with LINQ" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CEcMQH4-eSp7ImA9WhZXFkU.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-1117813275921071048</id><published>2011-05-06T04:41:00.001-07:00</published><updated>2011-05-06T04:41:21.051-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-05-06T04:41:21.051-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Device Filtering In Asp.Net</title><content type="html">&lt;p&gt;Asp.Net allows customization of properties of web server controls depending on the browser that is rendering the control. Well, this can be done by using javascript and css as well, by detecting the browser using javascript and then applying the correct css styles. Asp.Net makes it very easy to do without creating javascript or css.&lt;/p&gt; &lt;p&gt;The attributes of the controls need to contain the browser name to allow device filtering. For example, to customize the ForeColor property in Internet Explorer, the attribute need to be renamed to ie:ForeColor. For Mozilla, it will be Mozilla:ForeColor and for Pocket Internet Explorer, it’s PIE:ForeColor. &lt;/p&gt; &lt;p&gt;The following is sample code showing the attributes in a Label and TextBox control.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1ee34438-8e58-4417-8a54-285d91bd9850" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Sampele code&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;L1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ie&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Internet explorer&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Mozilla&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;mozilla&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ie&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;BackColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Red&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Mozilla&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;BackColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Green&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ie&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;ForeColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;White&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T3&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ie&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;IE&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Mozilla&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;mozilla&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ie&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;BackColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Gray&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Mozilla&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#ff0000"&gt;BackColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Black&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ForeColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;White&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;As can be seen, even the Text property can be customized. That is, the text on the controls would be different depending on the browser.&lt;/p&gt; &lt;p&gt;One important to note is that these properties can also be set declaratively meaning the properties cannot be accessed and / or modified from code behind. &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-1117813275921071048?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/1117813275921071048/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=1117813275921071048" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/1117813275921071048?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/1117813275921071048?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/05/device-filtering-in-aspnet.html" title="Device Filtering In Asp.Net" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CUEFQHY8fSp7ImA9WhZTF0U.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-296572629044705635</id><published>2011-03-22T01:46:00.001-07:00</published><updated>2011-03-22T01:46:51.875-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-22T01:46:51.875-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Captcha" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="Handler" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Creating a Captcha Control – Part 5</title><content type="html">&lt;p&gt;In the previous articles, I showed &lt;a href="http://shahed-kazi.blogspot.com/search/label/Captcha"&gt;how to create the captcha control.&lt;/a&gt; In this post, I will write about the handler that generates the image based on the dynamic random text generated by the GetRandomText method of the CaptchaControl usercontrol. &lt;/p&gt; &lt;p&gt;To generate the image, the handler needs to send its output as an image type. To get random text, it needs to access the value from the Session. The handler uses the System.Drawing.Bitmap class to create the image. The width of the image is dynamically based on the number of characters of the random text. The handler uses the background image path property to set the background image and uses the font colour, font size and font family to write the text on the image. The RotateTransform method of the System.Drawing.Graphics class is used to slightly rotate the text on the image.&amp;nbsp; the yPos method is used to randomly position the characters. The output image is then send through the stream.&lt;/p&gt; &lt;p&gt;The code for the class is below and can also be downloaded from CodePlex at &lt;a title="http://captchadotnet.codeplex.com/" href="http://captchadotnet.codeplex.com/"&gt;http://captchadotnet.codeplex.com/&lt;/a&gt; .&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:06316af8-ec21-4ab9-969a-ead3f222b751" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;CaptchaHandler&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Drawing;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; CaptchaApp&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; Summary description for CaptchaHandler&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#808080"&gt;///&lt;/span&gt;&lt;span style="color:#008000"&gt; &lt;/span&gt;&lt;span style="color:#808080"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CaptchaHandler&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;IHttpHandler&lt;/span&gt;, System.Web.SessionState.&lt;span style="color:#2b91af"&gt;IReadOnlySessionState&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt; cc;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ProcessRequest(&lt;span style="color:#2b91af"&gt;HttpContext&lt;/span&gt; context)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            context.Response.ContentType = &lt;span style="color:#a31515"&gt;&amp;quot;image/jpeg&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (context.Session != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (context.Session[&lt;span style="color:#a31515"&gt;&amp;quot;CaptchaClass&amp;quot;&lt;/span&gt;] != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                {&lt;/li&gt; &lt;li&gt;                    cc = (&lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt;)context.Session[&lt;span style="color:#a31515"&gt;&amp;quot;CaptchaClass&amp;quot;&lt;/span&gt;];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                }&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                {&lt;/li&gt; &lt;li&gt;                    cc = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                }&lt;/li&gt; &lt;li&gt;            }           &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; text = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(context.Session[&lt;span style="color:#a31515"&gt;&amp;quot;captcha&amp;quot;&lt;/span&gt;] !=&lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;                text = (&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;)context.Session[&lt;span style="color:#a31515"&gt;&amp;quot;captcha&amp;quot;&lt;/span&gt;];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; imageWidth = &lt;span style="color:#2b91af"&gt;Convert&lt;/span&gt;.ToInt32(((cc.FontSize + 8) * text.Length));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; imageHeight = &lt;span style="color:#2b91af"&gt;Convert&lt;/span&gt;.ToInt32(cc.FontSize * 2.5);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;Bitmap&lt;/span&gt; bmp = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Bitmap&lt;/span&gt;(imageWidth, imageHeight);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; Grph = &lt;span style="color:#2b91af"&gt;Graphics&lt;/span&gt;.FromImage(bmp);&lt;/li&gt; &lt;li&gt;            Grph.FillRectangle(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SolidBrush&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;Color&lt;/span&gt;.Lavender), 0, 0, bmp.Width, bmp.Height);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; grp = &lt;span style="color:#2b91af"&gt;Graphics&lt;/span&gt;.FromImage(bmp);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;Image&lt;/span&gt; background = &lt;span style="color:#2b91af"&gt;Image&lt;/span&gt;.FromFile(&lt;span style="color:#2b91af"&gt;HttpContext&lt;/span&gt;.Current.Server.MapPath(cc.BackgroundImagePath));&lt;/li&gt; &lt;li&gt;            grp.DrawImage(background, &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Rectangle&lt;/span&gt;(0, 0, bmp.Width, bmp.Height));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            Grph.CompositingQuality = System.Drawing.Drawing2D.&lt;span style="color:#2b91af"&gt;CompositingQuality&lt;/span&gt;.HighQuality;&lt;/li&gt; &lt;li&gt;            Grph.SmoothingMode = System.Drawing.Drawing2D.&lt;span style="color:#2b91af"&gt;SmoothingMode&lt;/span&gt;.HighQuality;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; xPos = 10;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;Font&lt;/span&gt; f = cc.GetFont();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;char&lt;/span&gt;[] textArray = text.ToCharArray();&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; yPosition = 0;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;Random&lt;/span&gt; r = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Random&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; textArray.Length; i++)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (i % 2 == 0)&lt;/li&gt; &lt;li&gt;                    Grph.RotateTransform(5);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    Grph.RotateTransform(-5);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;                yPosition = (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;)(r.NextDouble() * 10);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;                Grph.DrawString(textArray[i].ToString(), f, &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SolidBrush&lt;/span&gt;(&lt;span style="color:#2b91af"&gt;Color&lt;/span&gt;.FromName(cc.TextColor)), xPos, yPosition);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                xPos += 20;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;            &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.&lt;span style="color:#2b91af"&gt;ImageFormat&lt;/span&gt;.Jpeg);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; IsReusable&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;get&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; yPos()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;Random&lt;/span&gt; r = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Random&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;)(r.NextDouble() * 10);&lt;/li&gt; &lt;li&gt;        }                &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The handler also implements the System.Web.SessionState.IReadOnlySessionState interface beside the IHttpHandler. This is necessary so that the handler can access the Session of the current context.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-296572629044705635?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/296572629044705635/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=296572629044705635" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/296572629044705635?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/296572629044705635?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-5.html" title="Creating a Captcha Control – Part 5" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkAMSXozeSp7ImA9WhZTF0U.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-8857744810142734884</id><published>2011-03-22T00:59:00.001-07:00</published><updated>2011-03-22T00:59:48.481-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-22T00:59:48.481-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Captcha" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Creating a Captcha Control – Part 4</title><content type="html">&lt;p&gt;In one of the previous posts – &lt;a title="Captcha Control - user control" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-2.html"&gt;Creating a Captcha Control – Part 2&lt;/a&gt; – I was talking about Captcha class that contains some of the properties of the control like text colour, font size, font family and background image path. The class basically defines the setter and the getter for few of the properties. The code for the class is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3e6f8dac-1bbf-4e21-bc50-709f1432d5a9" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Captcha Class&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; CaptchaApp&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; FontFamily&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; fontFamily; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;value&lt;/span&gt; != &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty &amp;amp;&amp;amp; &lt;span style="color:#0000ff"&gt;value&lt;/span&gt; != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.fontFamily = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.fontFamily = &lt;span style="color:#a31515"&gt;&amp;quot;Arial&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; FontSize&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; fontSize; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;value&lt;/span&gt; &amp;lt;= 10 || &lt;span style="color:#0000ff"&gt;value&lt;/span&gt; &amp;gt;= 24)&lt;/li&gt; &lt;li&gt;                        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.fontSize = 16;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                        &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.fontSize = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                }&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                {&lt;/li&gt; &lt;li&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.fontSize = 16;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                }&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; TextColor&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; textColor; }&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty || &lt;span style="color:#0000ff"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.textColor = &lt;span style="color:#a31515"&gt;&amp;quot;Black&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.textColor = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; BackgroundImagePath&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; backgroundImagePath; }&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.backgroundImagePath = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; System.Drawing.&lt;span style="color:#2b91af"&gt;Font&lt;/span&gt; GetFont()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; System.Drawing.&lt;span style="color:#2b91af"&gt;Font&lt;/span&gt;(FontFamily, (&lt;span style="color:#0000ff"&gt;float&lt;/span&gt;)FontSize);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; fontSize;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; fontFamily;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; backgroundImagePath;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; textColor;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The full code for the control can be downloaded from CodePlex at &lt;a title="http://captchadotnet.codeplex.com/" href="http://captchadotnet.codeplex.com/"&gt;http://captchadotnet.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-8857744810142734884?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/8857744810142734884/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=8857744810142734884" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8857744810142734884?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8857744810142734884?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-4.html" title="Creating a Captcha Control – Part 4" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CU4DQXc4eSp7ImA9WhZTEko.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-414912864724100017</id><published>2011-03-16T04:12:00.001-07:00</published><updated>2011-03-16T04:12:50.931-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-16T04:12:50.931-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="COM" /><category scheme="http://www.blogger.com/atom/ns#" term="Captcha" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Creating a Captcha Control – Part 3</title><content type="html">&lt;h2&gt;Adding Audio&lt;/h2&gt; &lt;p&gt;In my previous 2 articles – &lt;a title="create a captcha control" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-1.html"&gt;Creating a Captcha Control – Part 1&lt;/a&gt; and &lt;a title="creating a captcha control" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-2.html"&gt;Creating a Captcha Control – Part 2&lt;/a&gt; – I have created the user control for the captcha control. The control also allows functionality to play audio to read the text on the captcha.&lt;/p&gt; &lt;p&gt;To achieve the audio feature, I have used a COM library – Microsoft Speech Object Library. To add the COM library, I added a reference to Microsoft Speech Object Library (%Windir%\System32\Speech\Common\Sapi.dll). To add a reference, right-click on the Reference folder, click Add Reference, click COM tab and add the library. &lt;/p&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_teNP-jBYa1Q/TYCbHzp14gI/AAAAAAAAAQA/VkvhkwMj09A/s1600-h/captcha-add-reference%5B3%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="captcha-add-reference" border="0" alt="captcha-add-reference" src="http://lh6.ggpht.com/_teNP-jBYa1Q/TYCbL9siBpI/AAAAAAAAAQE/VvNKHgCivB4/captcha-add-reference_thumb%5B1%5D.jpg?imgmax=800" width="561" height="484"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Once the reference is added, the SpeechLib namespace is imported as shown below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:68a1d9c2-91a9-426d-bace-fa234da0da9c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Adding reference to SpeechLib&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; SpeechLib;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Then, the Speak method in SpVoice class is used to play the audio as per below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a8a8c766-b1a7-4be6-9dca-d8e6ad47ee6a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;ReadCaptcha method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ReadCaptcha(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;SpVoice&lt;/span&gt; voice = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;SpVoice&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;char&lt;/span&gt;[] text = ((&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;)ViewState[&lt;span style="color:#a31515"&gt;&amp;quot;captcha&amp;quot;&lt;/span&gt;]).ToCharArray();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; text.Length; i++)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        voice.Speak(text[i].ToString(), &lt;span style="color:#2b91af"&gt;SpeechVoiceSpeakFlags&lt;/span&gt;.SVSFDefault);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;As can be seen, I am reading out each of the text and not all the text at once. The reason is, SpVoice class will try to read it as a word instead of individual characters otherwise.&lt;/p&gt; &lt;p&gt;In the following article, I will show how to create and use the handler to generate the image. The whole code for the captcha control can be downloaded from CodePlex at &lt;a title="download captcha control" href="http://captchadotnet.codeplex.com/"&gt;http://captchadotnet.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-414912864724100017?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/414912864724100017/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=414912864724100017" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/414912864724100017?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/414912864724100017?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-3.html" title="Creating a Captcha Control – Part 3" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/_teNP-jBYa1Q/TYCbL9siBpI/AAAAAAAAAQE/VvNKHgCivB4/s72-c/captcha-add-reference_thumb%5B1%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkECQ38-eip7ImA9WhZTEko.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-395153849315046250</id><published>2011-03-16T03:17:00.001-07:00</published><updated>2011-03-16T03:17:42.152-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-16T03:17:42.152-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="UserControl" /><category scheme="http://www.blogger.com/atom/ns#" term="Captcha" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Creating a Captcha Control – Part 2</title><content type="html">&lt;p&gt;In my last article – &lt;a title="Creating a Captcha Control" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-1.html"&gt;Creating a Captcha Control – Part 1&lt;/a&gt;, I discussed on the front end of the user control that creates the captcha. In this article, I will discuss on the code-behind of the control – that is, what properties are set and how and what methods are used.&lt;/p&gt; &lt;p&gt;The following code shows the properties that are used.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ec5ae966-b90d-407d-ad4f-09856358b418" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;User Control - Properties&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;#region&lt;/span&gt; field initialisation&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt; cc;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; captchaLength;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; fontSize;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; fontFamily;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; backgroundImagePath;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; textColor;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; successMessage;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; errorMessage;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; characterSet;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;#endregion&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Then, the setters and getters for these properties are created as per below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fa48d5a8-93fc-47fc-8ae3-d7d675b2b210" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;User Control - Setter / Getter&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 3em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;#region&lt;/span&gt; GetterSetter&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; TestButtonText&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; B1.Text; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; { B1.Text = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; SpeakButtonText&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; BSpeak.Text; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; { BSpeak.Text = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ReLoadButtonText&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; B2.Text; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; { B2.Text = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; SuccessMessage&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; successMessage; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; { successMessage = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; ErrorMessage&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; errorMessage; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt; { errorMessage = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; CaptchaLength&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; captchaLength; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; k = &lt;span style="color:#2b91af"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:#0000ff"&gt;value&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (k &amp;lt; 5 || k &amp;gt; 10)&lt;/li&gt; &lt;li&gt;                captchaLength = 6;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                captchaLength = k;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            captchaLength = 6;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; FontFamily&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; fontFamily; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;value&lt;/span&gt; != &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty &amp;amp;&amp;amp; &lt;span style="color:#0000ff"&gt;value&lt;/span&gt; != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            fontFamily = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            fontFamily = &lt;span style="color:#a31515"&gt;&amp;quot;Arial&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; FontSize&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; fontSize; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            fontSize = &lt;span style="color:#2b91af"&gt;Convert&lt;/span&gt;.ToInt32(&lt;span style="color:#0000ff"&gt;value&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (fontSize &amp;lt;= 10 &amp;amp;&amp;amp; fontSize &amp;gt;= 24)&lt;/li&gt; &lt;li&gt;                fontSize = 16;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            fontSize = 16;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; BackgroundImagePath&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; backgroundImagePath; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (System.IO.&lt;span style="color:#2b91af"&gt;File&lt;/span&gt;.Exists(Server.MapPath(&lt;span style="color:#0000ff"&gt;value&lt;/span&gt;)))&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            backgroundImagePath = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            backgroundImagePath = System.Configuration.&lt;span style="color:#2b91af"&gt;ConfigurationManager&lt;/span&gt;.AppSettings[&lt;span style="color:#a31515"&gt;&amp;quot;defaultImagePath&amp;quot;&lt;/span&gt;];&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; TextColor&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; textColor; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty || &lt;span style="color:#0000ff"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            textColor = &lt;span style="color:#a31515"&gt;&amp;quot;Black&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            textColor = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; CharacterSet&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; characterSet; }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;set&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;value&lt;/span&gt; == &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt; || &lt;span style="color:#0000ff"&gt;value&lt;/span&gt; == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            characterSet = &lt;span style="color:#a31515"&gt;&amp;quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            characterSet = &lt;span style="color:#0000ff"&gt;value&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;#endregion&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The setters and getters are quite straight forward. The captcha length property defaults to 6 in case the value entered is not a number and if the value is less than 6 or greater than 10.&lt;/p&gt; &lt;p&gt;The default font family is set to Arial.&lt;/p&gt; &lt;p&gt;The default font size is set to 16. If fonts are too small – less than 10px – or too large – greater than 24px – the font is automatically set to 16. &lt;/p&gt; &lt;p&gt;The default colour is set to black.&lt;/p&gt; &lt;p&gt;The default character set consists of all letters in uppercase and number from 1 to 9. I have not included lower case letters and 0 as these are sometimes confusing.&lt;/p&gt; &lt;p&gt;The default background image is set within the web.config file. Note that the background image is expected to be within the solution and would require “~” notation to work properly.&lt;/p&gt; &lt;p&gt;Now, in the Page_Load event shown below, I am setting the default values and loading the captcha.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:eaa77058-7dc3-4289-aac6-7f8845fe791c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Page_Load event&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    B1.Text = TestButtonText;&lt;/li&gt; &lt;li&gt;    B2.Text = ReLoadButtonText;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    BSpeak.Text = SpeakButtonText;&lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    SetValues();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    cc = GetCaptchaClass();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (!IsPostBack)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        LoadCaptcha();&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;As can be seen, the Button’s text are set a SetValues method is called which sets values for other properties and the method – LoadCaptcha is called if it is not a postback. Another class Captcha is set to method GetCaptchaClass which initiates a Captcha object. I will talk about the Captcha object in a little while.&lt;/p&gt; &lt;p&gt;The SetValues method is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:41664321-a260-41c0-839e-22afb485b23c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;SetValues method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; SetValues()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (CharacterSet == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        CharacterSet = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(CaptchaLength == 0)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        CaptchaLength = 6;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(BackgroundImagePath == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;        BackgroundImagePath = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(FontFamily == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        FontFamily = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;            &lt;/li&gt; &lt;li&gt;    &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(FontSize == 0)&lt;/li&gt; &lt;li&gt;        FontSize = 0.0;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt;(TextColor == &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        TextColor = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The LoadCaptcha method is interesting – it creates random text and adds it ViewState and Session, adds the Captcha object to Session and sets the path of the Image control to CaptchaHandler.ashx handler. The code for this method is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3c20f027-139b-4027-98c4-3f2cc077b18c" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;LoadCaptcha method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; LoadCaptcha()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; text = GetRandomText();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    ViewState.Add(&lt;span style="color:#a31515"&gt;&amp;quot;captcha&amp;quot;&lt;/span&gt;, text);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Session.Add(&lt;span style="color:#a31515"&gt;&amp;quot;CaptchaClass&amp;quot;&lt;/span&gt;, cc);&lt;span style="color:#008000"&gt;//add captcha object to Session&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    Session.Add(&lt;span style="color:#a31515"&gt;&amp;quot;captcha&amp;quot;&lt;/span&gt;, text);&lt;span style="color:#008000"&gt;//add captcha text to session&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Im1.ImageUrl = &lt;span style="color:#a31515"&gt;&amp;quot;CaptchaHandler.ashx&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The random text for the captcha is generated by GetRandomText method which basically creates random text based on the CharacterSet property. The code for this method is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c4e2cfbc-f7c9-4d3e-b76e-4a62c4efaaa0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;GetRandomText  method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; GetRandomText()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;char&lt;/span&gt;[] letters = CharacterSet.ToCharArray();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; text = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;Random&lt;/span&gt; r = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Random&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; num = -1;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.CaptchaLength; i++)&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        num = (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt;)(r.NextDouble() * (letters.Length - 1));&lt;/li&gt; &lt;li&gt;        text += letters[num].ToString();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; text;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The Captcha class basically properties for the captcha text. The GetCaptchaClass method initiates the Captcha class and set its properties. The code for this method is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:66fc68af-088f-436a-ab25-8a9f86002919" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;GetCaptchaClass  method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt; GetCaptchaClass()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (Session[&lt;span style="color:#a31515"&gt;&amp;quot;CaptchaClass&amp;quot;&lt;/span&gt;] != &lt;span style="color:#0000ff"&gt;null&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        cc = (&lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt;)Session[&lt;span style="color:#a31515"&gt;&amp;quot;CaptchaClass&amp;quot;&lt;/span&gt;];&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        cc = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Captcha&lt;/span&gt;();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    cc.FontSize = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.FontSize;&lt;/li&gt; &lt;li&gt;    cc.FontFamily = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.FontFamily;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    cc.BackgroundImagePath = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.BackgroundImagePath;&lt;/li&gt; &lt;li&gt;    cc.TextColor = &lt;span style="color:#0000ff"&gt;this&lt;/span&gt;.TextColor;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; cc;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;That’s pretty much all the methods for the user control. When the Reload Button is clicked the LoadAnother method is run that simply calls the LoadCaptcha method as per below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:8d6a24da-d039-42c4-87a3-3424589cf848" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;LoadAnother method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; LoadAnother(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    LoadCaptcha();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;When the Test button is clicked, the ValidateCaptcha method is run that compares the text entered with the text in the ViewState. The code is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e00f30de-e39d-4e85-9c31-682a63a0ba53" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;ValidateCaptcha method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ValidateCaptcha(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; text = T1.Text;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (text == (&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;)ViewState[&lt;span style="color:#a31515"&gt;&amp;quot;captcha&amp;quot;&lt;/span&gt;])&lt;/li&gt; &lt;li&gt;        LStatus.Text = SuccessMessage;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;else&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        LStatus.Text = ErrorMessage;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;In my next article, I will explain how the reading out the captcha text works. The captcha control is also available for download from CodePlex at &lt;a title="download captcha control" href="http://captchadotnet.codeplex.com/"&gt;http://captchadotnet.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-395153849315046250?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/395153849315046250/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=395153849315046250" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/395153849315046250?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/395153849315046250?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-2.html" title="Creating a Captcha Control – Part 2" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DUIARnY7cCp7ImA9WhZTEks.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-4045798375245583411</id><published>2011-03-16T02:22:00.000-07:00</published><updated>2011-03-16T02:25:47.808-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-03-16T02:25:47.808-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="UserControl" /><category scheme="http://www.blogger.com/atom/ns#" term="Captcha" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Creating a Captcha Control – Part 1</title><content type="html">&lt;p&gt;The word CAPTCHA means “&lt;strong&gt;C&lt;/strong&gt;ompletely &lt;strong&gt;A&lt;/strong&gt;utomated &lt;strong&gt;P&lt;/strong&gt;ublic &lt;strong&gt;T&lt;/strong&gt;uring to tell &lt;strong&gt;C&lt;/strong&gt;omputers and &lt;strong&gt;H&lt;/strong&gt;umans &lt;strong&gt;A&lt;/strong&gt;part”.Captcha is commonly used in registration forms and other forms to disallow spiders and / or bots to automatically submit a form. Captcha is usually some text on an image that can be read by humans but unreadable by computers. An example image of a captcha is below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_teNP-jBYa1Q/TYCCEwlMVSI/AAAAAAAAAP4/ViMceZ_3fFg/s1600-h/Captcha%5B2%5D.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Captcha" border="0" alt="Captcha" src="http://lh5.ggpht.com/_teNP-jBYa1Q/TYCCGCG86yI/AAAAAAAAAP8/Ucj1i-CMG8I/Captcha_thumb.jpg?imgmax=800" width="244" height="51"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Creating a captcha controls involves dynamically generating some text, converting the text into an image, passing the image to the client, pass on the text in some other format so it could be tested against. In this particular instance, I have created a captcha control through which we control properties like font size, colour, background image, character set to choose the characters from and number of characters to display. It will also control additional properties for setting text for various buttons like Test button, Reload button and success and error message. The control also contains option for reading out the text for accessibility reasons.&lt;/p&gt; &lt;p&gt;The control could be used like below:&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:bdefca66-7e8f-4549-8643-133f144826ce" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Using the control&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;uc1&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;CaptchaControl&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;CaptchaControl1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;TestButtonText&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Try&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;BackgroundImagePath&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;~/images/captcha2.png&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#ff0000"&gt;ErrorMessage&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Try Another Image&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;SuccessMessage&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;good job&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CaptchaLength&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;10&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CharacterSet&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ABCDEFG123456&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;&lt;span style="color:#ff0000"&gt;ReLoadButtonText&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Reload&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;FontSize&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;20&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;FontFamily&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Trebuchet MS&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;TextColor&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Blue&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt; &lt;p&gt;After a new project is setup in Visual Studio, a user control is added to the project. The user control simply contains an Image control for displaying captcha, a TextBox control where a user would enter text for comparison, a Label control that displays status – correct or incorrect text entered, three Button controls – one for testing the user, one for reloading the captcha text in case the text cannot be read by the user and another button for reading out the captcha text. It’s important that the text could be read out as it helps visually impaired users to enter the information.&lt;/p&gt; &lt;p&gt;Code for the user control is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:0c6d66a8-01f2-4d33-8238-0fbfb1d029d9" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;User Control - aspx&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffff00"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;@&lt;/span&gt; &lt;span style="color:#800000"&gt;Control&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Language&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CodeBehind&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;CaptchaControl.ascx.cs&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Inherits&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;CaptchaApp.CaptchaControl&amp;quot;&lt;/span&gt; &lt;span style="background:#ffff00"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Image&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Im1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;B1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Test&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;OnClick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ValidateCaptcha&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;LStatus&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;B2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Load Another Image&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;OnClick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;LoadAnother&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;BSpeak&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Speak&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;OnClick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ReadCaptcha&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;As can be seen, the ImageUrl property for the Image control is not set. It will be set from code-behind.&lt;/p&gt; &lt;p&gt;In my next post, I will discuss how I have created the various properties of the control through the code-behind of this user control.&lt;/p&gt; &lt;p&gt;The source code of the captcha control can be downloaded from CodePlex&amp;nbsp; at &lt;a title="Download Captcha Control" href="http://captchadotnet.codeplex.com/"&gt;http://captchadotnet.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-4045798375245583411?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/4045798375245583411/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=4045798375245583411" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4045798375245583411?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4045798375245583411?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/03/creating-captcha-control-part-1.html" title="Creating a Captcha Control – Part 1" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/_teNP-jBYa1Q/TYCCGCG86yI/AAAAAAAAAP8/Ucj1i-CMG8I/s72-c/Captcha_thumb.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkQBRH4ycCp7ImA9Wx9bFEs.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-1959256427482998532</id><published>2011-02-23T04:28:00.000-08:00</published><updated>2011-02-23T04:32:35.098-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-02-23T04:32:35.098-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="Seo" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Asp.Net, JavaScript &amp; Meta Redirects</title><content type="html">&lt;p&gt;There are few different ways to redirect a web page to another page in asp.net. Redirection can be achieved by using JavaScript, meta tags and by using asp.net methods as well. In this blog post, I will show how to setup redirection using each method and what are the pros and cons of it.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;JavaScript:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Adding redirect using JavaScript involves only one line of code. The following is the code added in a content page.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ce8940c3-1d58-4359-8511-1289abe731c5" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Redirect using JavaScript&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffff00"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;@&lt;/span&gt; &lt;span style="color:#800000"&gt;Page&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Title&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Language&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;MasterPageFile&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;~/Site.Master&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CodeBehind&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Redirection.aspx.cs&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Inherits&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;WebApplication1.Redirection&amp;quot;&lt;/span&gt; &lt;span style="background:#ffff00"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;HeadContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;   &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    window.location = &lt;span style="color:#800000"&gt;&amp;quot;default.aspx&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MainContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;When the page is run, content from the master page is loaded&amp;nbsp; first and then the page redirects to new page using HTTP status code of 200. The HttpFox report from FireFox is below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh5.ggpht.com/_teNP-jBYa1Q/TWT-NVg3ymI/AAAAAAAAAPM/zHQVRwym2no/s1600-h/javascript-httpfox%5B4%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="javascript-httpfox" border="0" alt="javascript-httpfox" src="http://lh4.ggpht.com/_teNP-jBYa1Q/TWT-OQSa7VI/AAAAAAAAAPQ/W-CG4DSK3_8/javascript-httpfox_thumb%5B2%5D.png?imgmax=800" width="582" height="203"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;As can be seen from Http watch report, the page is loaded first and then the page is redirected. The problem with this approach is that redirect would not work if JavaScript is disabled by the user and also search engines are fooled to think that the page is ok when it is not.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;META Tags:&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Secondly, meta tags can be used to redirect pages. An example code is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:46350eae-9f18-4887-a670-1e91bbc3361f" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Redirect using Meta tags&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffff00"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;@&lt;/span&gt; &lt;span style="color:#800000"&gt;Page&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Title&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Language&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;MasterPageFile&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;~/Site.Master&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CodeBehind&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Redirection.aspx.cs&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Inherits&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;WebApplication1.Redirection&amp;quot;&lt;/span&gt; &lt;span style="background:#ffff00"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;HeadContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;meta&lt;/span&gt; &lt;span style="color:#ff0000"&gt;http-equiv&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;refresh&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;0;url=default.aspx&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MainContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;The Http watch report is below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_teNP-jBYa1Q/TWT-P3x0pEI/AAAAAAAAAPU/LefGlKZI3LY/s1600-h/meta-httpwatch%5B5%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="meta-httpwatch" border="0" alt="meta-httpwatch" src="http://lh5.ggpht.com/_teNP-jBYa1Q/TWT-RJNIpTI/AAAAAAAAAPY/GmSUu29sdhQ/meta-httpwatch_thumb%5B3%5D.png?imgmax=800" width="564" height="231"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;As can be seen, the css file is loaded first and then the page redirects using Http status code of 200. Similar to the JavaScript approach,&amp;nbsp; the problems are that someone can stops meta refreshes from browser settings and search engines are fooled to think that the page is valid.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;ASP.Net Redirect&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;In asp.net, pages can be redirected using 2 methods. The first method is by using the method – Response.Redirect(string url). Example code is below.&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:09b382ba-005e-4a8e-a372-de024abbb362" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Response.Redirect(string url)&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Redirection&lt;/span&gt; : System.Web.UI.&lt;span style="color:#2b91af"&gt;Page&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            Response.Redirect(&lt;span style="color:#a31515"&gt;&amp;quot;default.aspx&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt; &lt;p&gt;Http watch report is below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_teNP-jBYa1Q/TWT-SXaY7TI/AAAAAAAAAPc/fxnaJNB_P1U/s1600-h/asp-net-302-httpwatch%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="asp-net-302-httpwatch" border="0" alt="asp-net-302-httpwatch" src="http://lh3.ggpht.com/_teNP-jBYa1Q/TWT-TuR8YsI/AAAAAAAAAPg/M5b1ywRX5d8/asp-net-302-httpwatch_thumb%5B1%5D.png?imgmax=800" width="644" height="249"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;As can be seen from the code, the page is redirected straight way using http status code of 302. This is a server side redirect and cannot be controlled by the browser. Well, a user may stop loading the page by clicking the Stop button in the browser but I am not counting that.&lt;/p&gt; &lt;p&gt;The main drawback of this approach is that it uses a 302 redirect. This means it is a temporary redirect which is bad for SEO.&lt;/p&gt; &lt;p&gt;Previously, asp.net redirect could be used differently to allow 301 redirect. 301 http status codes means the page has moved permanently and is search engine friendly. Here is a sample code.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:9588332d-74d3-4482-b41e-86bdb284cc30" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;asp.net 301 redirect&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Redirection&lt;/span&gt; : System.Web.UI.&lt;span style="color:#2b91af"&gt;Page&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            Response.Status = &lt;span style="color:#a31515"&gt;&amp;quot;301 Moved Permanently&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            Response.AddHeader(&lt;span style="color:#a31515"&gt;&amp;quot;Location&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;default.aspx&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Http watch report is below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh6.ggpht.com/_teNP-jBYa1Q/TWT-UoFL5YI/AAAAAAAAAPk/FaPv4WgIMYI/s1600-h/asp-net-301-httpwatch%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="asp-net-301-httpwatch" border="0" alt="asp-net-301-httpwatch" src="http://lh5.ggpht.com/_teNP-jBYa1Q/TWT-Vz1R60I/AAAAAAAAAPo/tgbiEgy5Org/asp-net-301-httpwatch_thumb%5B1%5D.png?imgmax=800" width="644" height="251"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;In the new release of .NET 4.0, Microsoft has added a new method Response.RedirectPermanent(string url). This method also allows for 301 redirects. Sample code is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e68221d4-44bf-4a8f-a802-dcd032c226d1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;RedirectPermanent(string url)&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Redirection&lt;/span&gt; : System.Web.UI.&lt;span style="color:#2b91af"&gt;Page&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            Response.RedirectPermanent(&lt;span style="color:#a31515"&gt;&amp;quot;default.aspx&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The http watch report produced when the page is run is below.&lt;/p&gt; &lt;p&gt;&lt;a href="http://lh4.ggpht.com/_teNP-jBYa1Q/TWT-XL6TobI/AAAAAAAAAPs/sR4RflRqmyk/s1600-h/asp-net-301-permanent-httpwatch%5B3%5D.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="asp-net-301-permanent-httpwatch" border="0" alt="asp-net-301-permanent-httpwatch" src="http://lh3.ggpht.com/_teNP-jBYa1Q/TWT-YUINKrI/AAAAAAAAAPw/whadDWedtHQ/asp-net-301-permanent-httpwatch_thumb%5B1%5D.png?imgmax=800" width="644" height="249"&gt;&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-1959256427482998532?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/1959256427482998532/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=1959256427482998532" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/1959256427482998532?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/1959256427482998532?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2011/02/aspnet-javascript-meta-redirects.html" title="Asp.Net, JavaScript &amp;amp; Meta Redirects" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_teNP-jBYa1Q/TWT-OQSa7VI/AAAAAAAAAPQ/W-CG4DSK3_8/s72-c/javascript-httpfox_thumb%5B2%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C08DRnY-fSp7ImA9Wx9QGEs.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-7886718178982661757</id><published>2010-12-31T21:55:00.001-08:00</published><updated>2010-12-31T22:04:37.855-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-31T22:04:37.855-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="Web Control" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Textbox &amp; TextArea – Max number of words</title><content type="html">&lt;p&gt;In this post, I have demonstrated how to calculate the number of words using both JavaScript and C# code and also how to restrict a TextArea or a multiline TextBox control to contain the specified number of words. One of the challenge in writing this article is to determine what a word is. For the purpose of this post, I have considered any text to be a word that is separated by a space. The words may contain symbols but must contain alphabets. For example, &lt;em&gt;work’s&lt;/em&gt; is a word but &lt;em&gt;;’;&lt;/em&gt; is not.&lt;/p&gt; &lt;p&gt;On aspx page, a multiline TextBox control and a span element is first declared. The text to be manipulated will be obtained from the TextBox and the span will display the status. The code is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e29dff56-4a6e-449e-9aba-040510fd9e5e" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;aspx&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;TextMode&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MultiLine&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Rows&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onkeyup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;WordCount()&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T2Status&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;style&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt;color&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:Red;&amp;quot;&amp;gt;&lt;/span&gt;5 word(s) remaining.&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The TextBox contain a KeyUp event. This is a JavaScript event that checks the number of words entered in the TextBox control and updates the status in the span element. The JavaScript code is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:03ba25e8-039f-4a3b-b04e-e93df4c56e85" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;JavaScript - TextBox max words&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;function&lt;/span&gt; WordCount() {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; text = document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= T2.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; list = text.split(&lt;span style="color:#800000"&gt;&amp;quot; &amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; x; &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; upper; &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; lower;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; count = 0;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; max = 5;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#006400"&gt;//Use the following to count the number of words&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#006400"&gt;/*for (x in list) {&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#006400"&gt;if (list[x].toUpperCase() != list[x].toLowerCase()) {&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#006400"&gt;count = count +1;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#006400"&gt;}&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#006400"&gt;}*/&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; val = &lt;span style="color:#800000"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (x &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; list) {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (list[x].toUpperCase() != list[x].toLowerCase()) {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (count +1 &amp;lt;= max) {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    count = count + 1;&lt;/li&gt; &lt;li&gt;                    val += list[x];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#006400"&gt;//alert(&amp;quot;val: &amp;quot; + val + &amp;quot; list: &amp;quot; + list[x]);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;else&lt;/span&gt; {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (count &amp;lt;= max) {&lt;/li&gt; &lt;li&gt;                    val += list[x];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    &lt;span style="color:#006400"&gt;//alert(&amp;quot;val: &amp;quot; + val + &amp;quot; list: &amp;quot; + list[x]);&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (x != list.length -1) {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                val += &lt;span style="color:#800000"&gt;&amp;quot; &amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= T2.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value = val;&lt;/li&gt; &lt;li&gt;        document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;T2Status&amp;#39;&lt;/span&gt;).innerHTML = max - count + &lt;span style="color:#800000"&gt;&amp;quot; words remaining&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;In JavaScript code, I am first getting the string from the TextBox and then splitting it into an array. Then, I am converting each string in the array into both upper and lower case. Then, I am comparing the converted strings. If the string match, it’s a word, otherwise, not. At the same time, I am creating another string based on the values in the array till it hits the max number of words. This string is then used to populate the TextBox.&lt;/p&gt; &lt;p&gt;The same can be achieved using C# code but this will cause unnecessary postbacks to the server. However, it is advisable to check the data using C# to avoid a user submitting the data from a browser with JavaScript disabled.&lt;/p&gt; &lt;p&gt;I have written two methods – one to do a word count and want to limit the string to a maximum number of words. In this first method to do a word count, I have broken the string into a string array using space - “ “ - as the delimiter. Then, I have check if a particular string on the array contains an English letter. If it does, the counter is updated. The code for this method is below.&lt;/p&gt; &lt;p&gt;&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d68a994b-bc59-4d5e-b5ed-11fa0f68b903" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;C# - WordCount() method&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; WordCount(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; s)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; c = 0;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] split = {&lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;}&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] list = s.Split(split, &lt;span style="color:#2b91af"&gt;StringSplitOptions&lt;/span&gt;.RemoveEmptyEntries);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;char&lt;/span&gt;[] ch = {&lt;span style="color:#a31515"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;f&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;g&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;h&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;i&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;j&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;k&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;m&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;n&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;o&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;p&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;q&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;s&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;u&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;v&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;x&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;y&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;z&amp;#39;&lt;/span&gt;,&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#a31515"&gt;&amp;#39;A&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;B&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;D&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;E&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;F&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;G&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;H&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;I&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;J&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;K&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;L&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;M&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;N&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;O&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;P&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;Q&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;R&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;S&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;T&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;U&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;V&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;W&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;X&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;Z&amp;#39;}&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; val = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; list.Count(); i++)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (list[i].IndexOfAny(ch) &amp;gt;= 0)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            c++;                             &lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; c;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;&lt;/p&gt; &lt;p&gt;In the next method, I have reconstructed a string to limit it to maximum number of words specified. It uses the same logic as to count the number of words. A new string is created based on the values in the string array. The code for the method is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d3dba179-5f6f-47a3-bb60-110e23c8d2f0" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;C# - String, max words&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; MaxWord(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; s, &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; max)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; c = 0;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] split = { &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; };&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] list = s.Split(split, &lt;span style="color:#2b91af"&gt;StringSplitOptions&lt;/span&gt;.RemoveEmptyEntries);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;char&lt;/span&gt;[] ch = {&lt;span style="color:#a31515"&gt;&amp;#39;a&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;b&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;c&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;d&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;e&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;f&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;g&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;h&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;i&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;j&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;k&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;l&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;m&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;n&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;o&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;p&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;q&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;r&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;s&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;t&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;u&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;v&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;w&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;x&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;y&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;z&amp;#39;&lt;/span&gt;,&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#a31515"&gt;&amp;#39;A&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;B&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;C&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;D&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;E&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;F&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;G&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;H&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;I&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;J&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;K&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;L&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;M&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;N&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;O&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;P&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;Q&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;R&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;S&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;T&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;U&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;V&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;W&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;X&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;Y&amp;#39;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;#39;Z&amp;#39;}&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; val = &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;.Empty;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; list.Count(); i++)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (list[i].IndexOfAny(ch) &amp;gt;= 0)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                c++;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (c &amp;lt;= max)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            val += list[i] + &lt;span style="color:#a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; val;              &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-7886718178982661757?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/7886718178982661757/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=7886718178982661757" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/7886718178982661757?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/7886718178982661757?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/12/textbox-textarea-max-number-of-words.html" title="Textbox &amp;amp; TextArea – Max number of words" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0cDSHg-eyp7ImA9Wx9QFk8.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-1644026092380453684</id><published>2010-12-29T04:17:00.001-08:00</published><updated>2010-12-29T04:17:59.653-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-29T04:17:59.653-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="javascript" /><category scheme="http://www.blogger.com/atom/ns#" term="Web Control" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Max length of multiline TextBox and TextArea</title><content type="html">&lt;p&gt;It is a common scenario to have a textarea element&amp;nbsp; or multiline TextBox control in a html / asp.net page and the challenge is to limit the number of characters that can be entered and to display the remaining number of characters that can be entered. The textarea unlike the TextBox control does not support the maxlength property and thus cannot be limited. The solution is to use javascript to limit the number of characters.&lt;/p&gt; &lt;p&gt;In this example, I have added a TextBox control and added the onkeyup event for the control. The code for the page is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:107741eb-9b95-48d2-b9bf-4e839a317e4d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;aspx&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;TextMode&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MultiLine&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Rows&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onkeyup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T1_KeyUp()&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;T1Status&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;style&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color:#ff0000"&gt;color&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:Red;&amp;quot;&amp;gt;&lt;/span&gt;50 character(s) remaining.&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The span element in the page will display the number of remaining characters that can be entered in the TextBox.&lt;/p&gt; &lt;p&gt;The JavaScript code is below. The code sets the max length of the TextBox to 50 characters.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:16eb0e18-abe3-4a3b-a284-3e98246b0234" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;JavaScript function&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;function&lt;/span&gt; T1_KeyUp() {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; text = document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= T1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; len = text.length;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; max = 50;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (len &amp;gt; max) {&lt;/li&gt; &lt;li&gt;            text = text.substring(0, max);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            len = 50;&lt;/li&gt; &lt;li&gt;            document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= T1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value = text;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;T1Status&amp;#39;&lt;/span&gt;).innerHTML = max - len + &lt;span style="color:#800000"&gt;&amp;quot; character(s) remaining.&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Server side could also be used for this but it will cause unnecessary postbacks to the server. However, the actual text entered should be validated from the server end as JavaScript can be easily disabled by a user.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-1644026092380453684?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/1644026092380453684/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=1644026092380453684" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/1644026092380453684?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/1644026092380453684?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/12/max-length-of-multiline-textbox-and.html" title="Max length of multiline TextBox and TextArea" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;A0IBRXk7eCp7ImA9Wx9QFkw.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-8280247052753705091</id><published>2010-12-29T02:45:00.001-08:00</published><updated>2010-12-29T02:45:54.700-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-29T02:45:54.700-08:00</app:edited><title>Retrieve line break from TextBox and TextArea control</title><content type="html">&lt;p&gt;It is a common issue to recognise line breaks inside a textarea. Text is entered in a textarea or an asp.net multiline TextBox control and Enter key is pressed to add a new line to the text. This situation may arise when entering, for example, address or feedback in a textarea and the user wants to separate some text from the other.&lt;/p&gt; &lt;p&gt;When the text is obtained from code behind or from JavaScript, the line break needs to be obtained by checking the string entered in the textbox. This is needed as the browser fails to display the line break and instead requires a “&amp;lt;br/&amp;gt;” tag.&lt;/p&gt; &lt;p&gt;In this example, I have added a multiline TextBox control and used both JavaScript and server side code to replace the line break with a “&amp;lt;br/&amp;gt;” tag and to replace the carriage return with “&amp;amp;nbsp;&amp;amp;nbsp;”. The code for the page is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4aec1c9b-dc29-452e-a0fa-71713a240961" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;aspx page&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt; &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;TextBox1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Rows&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;3&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;TextMode&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MultiLine&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt; Server Click: &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Label1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; Js Click: &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;SpanJs&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt; &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt; &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onclick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button1_Click&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;     &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Continue&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt; &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onclientclick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;JsClick()&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;JS&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The code for JavaScript function JsClick() is below. The function retrieves the text from the TextBox control, performs string manipulation and displays the text in a span element.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:fa164f26-7d9e-482f-af95-619a05f1b59a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;JavScript function&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;function&lt;/span&gt; JsClick() {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; text = document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= TextBox1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;).value;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        text = text.replace(&lt;span style="color:#800000"&gt;&amp;quot;&amp;#92;n&amp;quot;&lt;/span&gt;, &lt;span style="color:#800000"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        text = text.replace(&lt;span style="color:#800000"&gt;&amp;quot;&amp;#92;r&amp;quot;&lt;/span&gt;, &lt;span style="color:#800000"&gt;&amp;quot;&amp;amp;nbsp;nbsp;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;SpanJs&amp;#39;&lt;/span&gt;).innerHTML = text;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;The code for Button’s OnClick event is below. Like JavaScript function, the method retrieves text from TextBox control, performs string manipulation and outputs the text in a Label control.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c4b2c683-48a9-4b06-997a-07ad1f77d10a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code-behind&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Button1_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; text = TextBox1.Text;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    text = text.Replace(&lt;span style="color:#a31515"&gt;&amp;quot;&amp;#92;n&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    text = text.Replace(&lt;span style="color:#a31515"&gt;&amp;quot;&amp;#92;r&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;&amp;amp;nbsp;&amp;amp;nbsp;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Label1.Text = text;&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-8280247052753705091?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/8280247052753705091/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=8280247052753705091" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8280247052753705091?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8280247052753705091?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/12/retrieve-line-break-from-textbox-and.html" title="Retrieve line break from TextBox and TextArea control" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DEMER34zcCp7ImA9Wx9QEUs.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-4112461973258336212</id><published>2010-12-22T06:31:00.001-08:00</published><updated>2010-12-23T20:53:26.088-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-12-23T20:53:26.088-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Web Control" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Create and Access Dynamic Controls in asp.net</title><content type="html">In asp.net, controls like Label, TextBox can be dynamically created. Controls are typically added to the page on Page_Load method.Dynamically created controls need to be recreated on each postback. Otherwise, the controls will be lost and cannot be accessed from code behind.&lt;br /&gt;
In this example, I will create dynamically add a TextBox and a Label control to the form an then access the values of these controls on Button click.&lt;br /&gt;
Code for aspx page is below.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:142035b2-ced0-4584-bf78-1fbcba1d51e0" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;&lt;div style="border: 1px solid rgb(0, 0, 128); color: black; font-family: 'Courier New',Courier,Monospace; font-size: 10pt;"&gt;&lt;div style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(0, 0, 128) none repeat scroll 0% 0%; color: white; font-family: Verdana,Tahoma,Arial,sans-serif; font-weight: bold; padding: 2px 5px;"&gt;aspx page&lt;/div&gt;&lt;div style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(221, 221, 221) none repeat scroll 0% 0%; overflow: auto;"&gt;&lt;ol style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 0%; margin: 0pt 0pt 0pt 2em; padding: 0pt 0pt 0pt 5px;"&gt;&lt;li&gt;&lt;span style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 0) none repeat scroll 0% 0%;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue;"&gt;@&lt;/span&gt; &lt;span style="color: maroon;"&gt;Page&lt;/span&gt; &lt;span style="color: red;"&gt;Title&lt;/span&gt;&lt;span style="color: blue;"&gt;=""&lt;/span&gt; &lt;span style="color: red;"&gt;Language&lt;/span&gt;&lt;span style="color: blue;"&gt;="C#"&lt;/span&gt; &lt;span style="color: red;"&gt;MasterPageFile&lt;/span&gt;&lt;span style="color: blue;"&gt;="~/Site.Master"&lt;/span&gt; &lt;span style="color: red;"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt; &lt;span style="color: red;"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue;"&gt;="AccessDynamicControl.aspx.cs"&lt;/span&gt; &lt;span style="color: red;"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue;"&gt;="WebApplication1.AccessDynamicControl"&lt;/span&gt; &lt;span style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 0) none repeat scroll 0% 0%;"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Content&lt;/span&gt; &lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="Content1"&lt;/span&gt; &lt;span style="color: red;"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color: blue;"&gt;="HeadContent"&lt;/span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Content&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Content&lt;/span&gt; &lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="Content2"&lt;/span&gt; &lt;span style="color: red;"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color: blue;"&gt;="MainContent"&lt;/span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Button&lt;/span&gt; &lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="Button1"&lt;/span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt; &lt;span style="color: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="Continue"&lt;/span&gt; &lt;span style="color: red;"&gt;onclick&lt;/span&gt;&lt;span style="color: blue;"&gt;="Button1_Click"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Content&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;A Label and a TextBox control are created on Page_Load method. As can be seen, the controls are added, each time the Page_Load method is run.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4eb91df7-1aa1-4ce8-9222-0469bc8a2e37" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;&lt;div style="border: 1px solid rgb(0, 0, 128); color: black; font-family: 'Courier New',Courier,Monospace; font-size: 10pt;"&gt;&lt;div style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(0, 0, 128) none repeat scroll 0% 0%; color: white; font-family: Verdana,Tahoma,Arial,sans-serif; font-weight: bold; padding: 2px 5px;"&gt;Page_Load method&lt;/div&gt;&lt;div style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(221, 221, 221) none repeat scroll 0% 0%; overflow: auto;"&gt;&lt;ol style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 0%; margin: 0pt 0pt 0pt 2.5em; padding: 0pt 0pt 0pt 5px;"&gt;&lt;li&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;{&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Label&lt;/span&gt; l = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;Label&lt;/span&gt;();&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;l.ID = &lt;span style="color: #a31515;"&gt;"Label1"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;l.Text = &lt;span style="color: #a31515;"&gt;"some text"&lt;/span&gt;;&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;TextBox&lt;/span&gt; t = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af;"&gt;TextBox&lt;/span&gt;();&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.ID = &lt;span style="color: #a31515;"&gt;"TB1"&lt;/span&gt;;&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Form.Controls.Add(l);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Form.Controls.Add(t);&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;When the Button is clicked, dynamically created Label and TextBox controls are accessed and the Text value of these controls are displayed on the page.&lt;br /&gt;
&lt;div class="wlWriterEditableSmartContent" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b4d6632f-e1c1-4166-bcc2-62662b348cdb" style="display: inline; float: none; margin: 0px; padding: 0px;"&gt;&lt;div style="border: 1px solid rgb(0, 0, 128); color: black; font-family: 'Courier New',Courier,Monospace; font-size: 10pt;"&gt;&lt;div style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(0, 0, 128) none repeat scroll 0% 0%; color: white; font-family: Verdana,Tahoma,Arial,sans-serif; font-weight: bold; padding: 2px 5px;"&gt;OnClick event&lt;/div&gt;&lt;div style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(221, 221, 221) none repeat scroll 0% 0%; overflow: auto;"&gt;&lt;ol style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(255, 255, 255) none repeat scroll 0% 0%; margin: 0pt 0pt 0pt 2em; padding: 0pt 0pt 0pt 5px;"&gt;&lt;li&gt;&lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Button1_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: #2b91af;"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;{&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;Label&lt;/span&gt; l2 = (&lt;span style="color: #2b91af;"&gt;Label&lt;/span&gt;)&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Form.FindControl(&lt;span style="color: #a31515;"&gt;"Label1"&lt;/span&gt;);&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color: #2b91af;"&gt;TextBox&lt;/span&gt; t2 = (&lt;span style="color: #2b91af;"&gt;TextBox&lt;/span&gt;)&lt;span style="color: blue;"&gt;this&lt;/span&gt;.Form.FindControl(&lt;span style="color: #a31515;"&gt;"TB1"&lt;/span&gt;);&lt;/li&gt;
&lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Response.Write(&lt;span style="color: #a31515;"&gt;"Label"&lt;/span&gt; + l2.Text + &lt;span style="color: #a31515;"&gt;" TextBox: "&lt;/span&gt; + t2.Text);&lt;/li&gt;
&lt;li style="-moz-background-clip: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-background-origin: -moz-initial; background: rgb(243, 243, 243) none repeat scroll 0% 0%;"&gt;}&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-4112461973258336212?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/4112461973258336212/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=4112461973258336212" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4112461973258336212?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4112461973258336212?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/12/create-and-access-dynamic-controls-in.html" title="Create and Access Dynamic Controls in asp.net" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0UHR3o8fyp7ImA9Wx9TGU4.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-3242128862025277515</id><published>2010-11-28T00:00:00.001-08:00</published><updated>2010-11-28T00:00:36.477-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-28T00:00:36.477-08:00</app:edited><title>Load ListBox items based on another ListBox’s selected item</title><content type="html">&lt;p&gt;In many web applications we want to load a control based on what is selected in another control. For example, loading a ListBox control or a DropDownList control based on what is selected in another ListBox or DropDownList control. The situation is not limited to only these controls but could also use RadioButtonList, CheckBoxList controls, etc.&lt;/p&gt; &lt;p&gt;In this example,&amp;nbsp; I have created a page that populates ListBox control with States value based on what Country is selected in another ListBox. I have added 2 ListBox controls – for State and Country, a Button and a Label control to the page. From code-behind page, I have created a DataTable that holds the country and corresponding state values. When an item is selected from Country ListBox, the State ListBox is populated. The AutoPostBack property for Country ListBox is set to true and SelectedIndexChanged event is implemented.&lt;/p&gt; &lt;p&gt;When the Button is clicked, the Label control’s Text property is populated to display the state and country that has been selected from the ListBox controls. The code for the aspx page and code-behind is below.&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:42462363-9c9f-43c1-a425-ae342174ba58" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;aspx page&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffff00"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;@&lt;/span&gt; &lt;span style="color:#800000"&gt;Page&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Title&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Language&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;MasterPageFile&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;~/Site.Master&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CodeBehind&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;LoadListboxFromDatatable.aspx.cs&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Inherits&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;WebApplication1.LoadListboxFromDatatable&amp;quot;&lt;/span&gt; &lt;span style="background:#ffff00"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;HeadContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MainContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ListBoxCountry&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;AutoPostBack&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;True&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#ff0000"&gt;onselectedindexchanged&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ListBoxCountry_SelectedIndexChanged&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Select:&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Australia&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;US&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;India&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ListBoxState&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onclick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button1_Click&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Show&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;LStatus&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Label&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b1da88e6-a6e3-476c-854d-c079b0f728f1" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code-behind&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Data;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li&gt;{&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;LoadListboxFromDatatable&lt;/span&gt; : System.Web.UI.&lt;span style="color:#2b91af"&gt;Page&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ListBoxCountry_SelectedIndexChanged(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; country = ListBoxCountry.SelectedValue;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            ListBoxState.Items.Clear();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;DataRow&lt;/span&gt; rows &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; GetStates().Rows)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (rows[0].ToString() == country)&lt;/li&gt; &lt;li&gt;                    ListBoxState.Items.Add(rows[1].ToString());&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt; GetStates()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt; dt = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DataTable&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            dt.Columns.Add(&lt;span style="color:#a31515"&gt;&amp;quot;Country&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;            dt.Columns.Add(&lt;span style="color:#a31515"&gt;&amp;quot;State&amp;quot;&lt;/span&gt;, &lt;span style="color:#0000ff"&gt;typeof&lt;/span&gt;(&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;US&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Los Angeles&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;US&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Virginia&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;US&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Ohio&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;Australia&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;NSW&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;Australia&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Victoria&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;Australia&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Tasmania&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;India&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Maharastra&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;India&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;West Bengal&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li&gt;            dt.Rows.Add(&lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color:#a31515"&gt;&amp;quot;India&amp;quot;&lt;/span&gt;, &lt;span style="color:#a31515"&gt;&amp;quot;Assam&amp;quot;&lt;/span&gt; });&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; dt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Button1_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            LStatus.Text = &lt;span style="color:#a31515"&gt;&amp;quot;Country: &amp;quot;&lt;/span&gt; + ListBoxCountry.SelectedValue + &lt;span style="color:#a31515"&gt;&amp;quot;; State: &amp;quot;&lt;/span&gt; + ListBoxState.SelectedValue;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-3242128862025277515?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/3242128862025277515/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=3242128862025277515" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/3242128862025277515?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/3242128862025277515?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/11/load-listbox-items-based-on-another.html" title="Load ListBox items based on another ListBox’s selected item" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;CkcHRHg5cCp7ImA9Wx9TE0w.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-8340525103821504226</id><published>2010-11-20T19:27:00.001-08:00</published><updated>2010-11-20T19:27:15.628-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-20T19:27:15.628-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="configuration" /><category scheme="http://www.blogger.com/atom/ns#" term="Web Control" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Asp.Net FileUpload control – use and configuration</title><content type="html">&lt;p&gt;The asp.net FileUpload control allows a user to browse and upload files to the web server. From developers perspective, it is as simple as dragging and dropping the FileUpload control to the aspx page. An extra control, like a Button control, or some other control is needed, to actually save the file.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:a890ba16-87cc-43ff-9d1e-6af076d57ad6" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;aspx page&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;FileUpload&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;FileUpload1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;B1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Save&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;OnClick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;B1_Click&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;By default, the FileUpload control allows a maximum of 4MB file to be uploaded and the execution timeout is 110 seconds. These properties can be changed from within the web.config file’s httpRuntime section. The maxRequestLength property determines the maximum file size that can be uploaded. The executionTimeout property determines the maximum time for execution. &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:3fce8533-9cf9-45e4-ba23-ab39fe9cd4af" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;web.config file&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;httpRuntime&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;maxRequestLength&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;8192&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;executionTimeout&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;220&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;From code behind, the mime type, size of the file, file name and the extension of the file can be obtained. The maximum file size that can be uploaded can be obtained and modified using the System.Web.Configuration.HttpRuntimeSection class.&lt;/p&gt; &lt;p&gt;Files can be alternatively saved using the System.IO.HttpFileCollection class. This collection class can be populated using the Request.Files property. The collection contains HttpPostedFile class which contains a reference to the class.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:e453f651-8cbe-4709-a863-fa7023159861" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code behind&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web.UI;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web.UI.WebControls;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.IO;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Configuration;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web.Configuration;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;partial&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;WebControls&lt;/span&gt; : System.Web.UI.&lt;span style="color:#2b91af"&gt;Page&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;//Using FileUpload control to upload and save files&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;protected&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; B1_Click(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (FileUpload1.HasFile &amp;amp;&amp;amp; FileUpload1.PostedFile.ContentLength &amp;gt; 0)&lt;/li&gt; &lt;li&gt;            {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#008000"&gt;//mime type of the uploaded file&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; mimeType = FileUpload1.PostedFile.ContentType;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;//size of the uploaded file&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; size = FileUpload1.PostedFile.ContentLength; &lt;span style="color:#008000"&gt;// bytes&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#008000"&gt;//extension of the uploaded file&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; extension = System.IO.&lt;span style="color:#2b91af"&gt;Path&lt;/span&gt;.GetExtension(FileUpload1.FileName);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;/li&gt; &lt;li&gt;                &lt;span style="color:#008000"&gt;//save file&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; path = Server.MapPath(&lt;span style="color:#a31515"&gt;&amp;quot;path&amp;quot;&lt;/span&gt;);                &lt;/li&gt; &lt;li&gt;                FileUpload1.SaveAs(path + FileUpload1.FileName);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#008000"&gt;//maximum file size allowed&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;HttpRuntimeSection&lt;/span&gt; rt = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;HttpRuntimeSection&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            rt.MaxRequestLength = rt.MaxRequestLength * 2;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;int&lt;/span&gt; length = rt.MaxRequestLength;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;       &lt;/li&gt; &lt;li&gt;            &lt;span style="color:#008000"&gt;//execution timeout&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;TimeSpan&lt;/span&gt; ts = rt.ExecutionTimeout;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;double&lt;/span&gt; secomds = ts.TotalSeconds;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#008000"&gt;//Using Request.Files to save files&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;private&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; AltSaveFile()&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;HttpFileCollection&lt;/span&gt; coll = Request.Files;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; coll.Count; i++)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#2b91af"&gt;HttpPostedFile&lt;/span&gt; file = coll[i];&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (file.ContentLength &amp;gt; 0)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    ;&lt;span style="color:#008000"&gt;//do something&lt;/span&gt;&lt;/li&gt; &lt;li&gt;            }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-8340525103821504226?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/8340525103821504226/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=8340525103821504226" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8340525103821504226?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8340525103821504226?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/11/aspnet-fileupload-control-use-and.html" title="Asp.Net FileUpload control – use and configuration" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkUFQ3s8eCp7ImA9Wx9TEkw.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-6577449284766510277</id><published>2010-11-19T16:50:00.001-08:00</published><updated>2010-11-19T16:50:12.570-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-19T16:50:12.570-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="Validation" /><title>Custom Validation Summary in Asp.Net</title><content type="html">&lt;p&gt;The Validation Summary control in asp.net displays error messages when the button is clicked. The other validation controls, like Required Field Valuators or Compare Valuators controls displays error messages when the controls it’s validating loses focus or changes. There is no built in control that displays a summary of all validation errors in a separate controls.&lt;/p&gt; &lt;p&gt;In this blog post, I will create a asp.net page, add couple of controls – Textbox and DropDownList and add validation controls for these. I will also add custom javascript that checks the validation controls are displaying error messages and populates the custom validation control accordingly.&lt;/p&gt; &lt;p&gt;From the javascript, I have checked whether the validation controls are showing any messages and populated the validation summary section. Also, OnBlur event of Textbox and onchange event of Dropdownlist is used to check for the errors.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:2e638d0b-382e-411c-9f12-df07bb959407" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="background:#ffff00"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#0000ff"&gt;@&lt;/span&gt; &lt;span style="color:#800000"&gt;Page&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Title&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Language&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;C#&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;MasterPageFile&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;~/Site.Master&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;CodeBehind&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Validation.aspx.cs&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Inherits&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;WebApplication1.Validation&amp;quot;&lt;/span&gt; &lt;span style="background:#ffff00"&gt;%&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;HeadContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt; &lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;function&lt;/span&gt; ShowError() {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; msg = document.getElementById(&lt;span style="color:#800000"&gt;&amp;quot;spanError&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; reqV = document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= RequiredFieldValidator1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;var&lt;/span&gt; comV = document.getElementById(&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color:#800000"&gt;= CompareValidator1.ClientID &lt;/span&gt;&lt;span style="background:#ffff00;color:#800000"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color:#800000"&gt;&amp;#39;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        msg.innerHTML = &lt;span style="color:#800000"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (reqV.style.visibility == &lt;span style="color:#800000"&gt;&amp;quot;visible&amp;quot;&lt;/span&gt;)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            msg.innerHTML += &lt;span style="color:#800000"&gt;&amp;quot;Textbox1 is required&amp;quot;&lt;/span&gt; + &lt;span style="color:#800000"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;if&lt;/span&gt; (comV.style.visibility == &lt;span style="color:#800000"&gt;&amp;quot;visible&amp;quot;&lt;/span&gt;)&lt;/li&gt; &lt;li&gt;            msg.innerHTML += &lt;span style="color:#800000"&gt;&amp;quot;Drop down list is required&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                         &lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;script&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Content2&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ContentPlaceHolderID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;MainContent&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;TextBox1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onblur&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ShowError()&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;TextBox&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;RequiredFieldValidator&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;RequiredFieldValidator1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#ff0000"&gt;ControlToValidate&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;TextBox1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ErrorMessage&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;*&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;RequiredFieldValidator&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;DropDownList&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;DropDownList1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;onchange&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ShowError()&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Select:&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;ListItem&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;DropDownList&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;CompareValidator&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;CompareValidator1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li&gt;        &lt;span style="color:#ff0000"&gt;ControlToValidate&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;DropDownList1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ErrorMessage&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;*&amp;quot;&lt;/span&gt; &lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#ff0000"&gt;Operator&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;NotEqual&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ValueToCompare&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Select:&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;CompareValidator&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Button&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;Text&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;Button&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;OnClientClick&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;ShowError()&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Validation Summary:&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;br&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt; &lt;span style="color:#ff0000"&gt;id&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;spanError&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;span&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;div&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;Content&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-6577449284766510277?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/6577449284766510277/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=6577449284766510277" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6577449284766510277?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6577449284766510277?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/11/custom-validation-summary-in-aspnet.html" title="Custom Validation Summary in Asp.Net" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;Dk8GQ3g_fCp7ImA9Wx5aGE8.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-337053830799485194</id><published>2010-11-15T04:40:00.001-08:00</published><updated>2010-11-15T04:40:22.644-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-15T04:40:22.644-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="configuration" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Custom handler in asp.net using C#</title><content type="html">&lt;p&gt;Http handlers in asp.net implement the System.Web.IHTTPHandler interface and provides low level Request and Response API to respond to the incoming Http requests. Using this method, we can setup a different file extension instead of .aspx to handle the request. &lt;/p&gt; &lt;p&gt;In this example, I will create a simple handler and use .example as the extension to call the page. First, I will add a class that implements the IHTTPHandler class as below.&lt;/p&gt; &lt;p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:7132e794-1b88-48de-a2b0-f3bf85ad621b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Linq;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Web;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;using&lt;/span&gt; System.Text;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;namespace&lt;/span&gt; WebApplication1&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;CustomHandler&lt;/span&gt; : &lt;span style="color:#2b91af"&gt;IHttpHandler&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    {&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;bool&lt;/span&gt; IsReusable&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#0000ff"&gt;get&lt;/span&gt; { &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#0000ff"&gt;false&lt;/span&gt;; }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; ProcessRequest(&lt;span style="color:#2b91af"&gt;HttpContext&lt;/span&gt; context)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#2b91af"&gt;HttpRequest&lt;/span&gt; request = context.Request;&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;HttpResponse&lt;/span&gt; response = context.Response;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            response.Write(Html());&lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; Html()&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        {&lt;/li&gt; &lt;li&gt;            &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt; sb = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;StringBuilder&lt;/span&gt;();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            sb.Append(&lt;span style="color:#a31515"&gt;&amp;quot;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Custom Handler&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;            sb.Append(&lt;span style="color:#a31515"&gt;&amp;quot;&amp;lt;body&amp;gt;Test page to demonstrate custom handler&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; sb.ToString();        &lt;/li&gt; &lt;li&gt;        }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;The IsReusable() and ProcessRequest(HttpContext context) methods need to be implemented as part of implementing the interface. The HttpContext class contains reference to the Request and Response objects. To send custom html as the response, another method Html() is written that return some html content as string.&lt;/p&gt; &lt;p&gt;To be able to use this code and use .example as the file extension, the &amp;lt;system.web&amp;gt; section of web.config file needs to be updated. The code is shown below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:76350024-33be-452c-9dd5-f8c20da92d57" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; max-height: 300px; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;httpHandlers&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;add&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;verb&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;*&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;path&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;*.example&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;type&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;WebApplication1.CustomHandler&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;httpHandlers&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;When any page with file name, for example, example.example is called on the browser, custom html is returned as defined in&amp;nbsp; ProcessRequest method in CustomHandler class.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-337053830799485194?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/337053830799485194/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=337053830799485194" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/337053830799485194?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/337053830799485194?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/11/custom-handler-in-aspnet-using-c.html" title="Custom handler in asp.net using C#" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkUGQXg5eCp7ImA9Wx5aF0w.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-766609761608165222</id><published>2010-11-13T21:57:00.001-08:00</published><updated>2010-11-13T21:57:00.620-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-13T21:57:00.620-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="Web Control" /><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><title>Using asp.net AdRotator control</title><content type="html">&lt;p&gt;The asp.net AdRotator control is used to dynamically display advertisements on the web page. Advertisements are basically images that can be clicked and changes when page is refreshed. The AdRotator control requires an xml file to be used as the data source. The following properties can be specified for the control.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;ImageUrl – Source URL of the image.&lt;/li&gt; &lt;li&gt;NavigateUrl – URL to which the page will be redirected once the image is clicked.&lt;/li&gt; &lt;li&gt;AlternateText – The text that will be used as the alt attribute for the image.&lt;/li&gt; &lt;li&gt;Impressions – A number that determines the number of times that the image will be displayed.&lt;/li&gt; &lt;li&gt;Keyword – The category for the advertisements.&lt;/li&gt; &lt;li&gt;Height – Height of the image.&lt;/li&gt; &lt;li&gt;Width – Width of the image.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The following is a sample code that is used to display the ads dynamically.&lt;/p&gt; &lt;p&gt;On .aspx page,&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1ae96d42-fcea-42b8-98eb-7c721560383d" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;AdRotator&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;AdRotator1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;DataSourceID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;XmlDataSource1&amp;quot;&lt;/span&gt; &lt;span style="color:#0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;XmlDataSource&lt;/span&gt; &lt;span style="color:#ff0000"&gt;ID&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;XmlDataSource1&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;runat&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;server&amp;quot;&lt;/span&gt; &lt;span style="color:#ff0000"&gt;DataFile&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&amp;quot;~/Ads.xml&amp;quot;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#800000"&gt;asp&lt;/span&gt;&lt;span style="color:#0000ff"&gt;:&lt;/span&gt;&lt;span style="color:#800000"&gt;XmlDataSource&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Sample code for the xml is below.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b97d4f5b-3002-4fdd-a7a0-9da74c5c648a" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color:#a31515"&gt;xml&lt;/span&gt;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;version&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; &lt;/span&gt;&lt;span style="color:#ff0000"&gt;encoding&lt;/span&gt;&lt;span style="color:#0000ff"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color:#0000ff"&gt; ?&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Advertisements&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Ad&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ImageUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;images/ad1.png&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;ImageUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;NavigateUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Default.aspx&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;NavigateUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;AlternateText&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Ad1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;AlternateText&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Impressions&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;60&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Impressions&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Keyword&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Ad1 Ad1&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Keyword&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Ad&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Ad&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;ImageUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;images/ad2.png&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;ImageUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;NavigateUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Default2.aspx&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;NavigateUrl&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;AlternateText&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Ad2&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;AlternateText&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Impressions&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;40&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Impressions&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#a31515"&gt;Keyword&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;Ad2 Ad2&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Keyword&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;  &lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Ad&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:#a31515"&gt;Advertisements&lt;/span&gt;&lt;span style="color:#0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-766609761608165222?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/766609761608165222/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=766609761608165222" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/766609761608165222?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/766609761608165222?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/11/using-aspnet-adrotator-control.html" title="Using asp.net AdRotator control" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;DkEESXkzeyp7ImA9Wx5aE00.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-6929086565205119731</id><published>2010-11-09T04:10:00.001-08:00</published><updated>2010-11-09T04:10:08.783-08:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-11-09T04:10:08.783-08:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="asp.net" /><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Monitor file system in .NET using C#</title><content type="html">&lt;p&gt;The System.IO.FileSystemWatcher class can be used to monitor a particular path/directory for any modification to the files. The modification includes creation, deletion, renaming, file name changes and file size changes in the particular directory or to any sub-directory. The EnableRaisingEvents property of FileSystemWatcher&amp;nbsp; class must be set to true.&lt;/p&gt; &lt;p&gt;The&amp;nbsp; properties of the file that needs to be monitored can be set by using the NotifyFilter class. The IncludeSubdirectories property needs to be set to true if sub-directories needs to be monitored.&lt;/p&gt; &lt;p&gt;The Changed, Created and Deleted events can be handled by the FileSystemEventHandler and the Renamed event can be handled by the RenamedEventHandler. It is a good idea to decide want what to monitor as otherwise the log file will keep growing.&lt;/p&gt; &lt;p&gt;In this particular example, I will monitor the App_Data folder in a web application and write the logs in a text file. The code is written in Global.asax file.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f52fcf6e-95b9-4184-a377-4d3eba5b5dba" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;void&lt;/span&gt; Application_Start(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;EventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;// Code that runs on application startup&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; path = Server.MapPath(&lt;span style="color:#a31515"&gt;&amp;quot;App_Data&amp;quot;&lt;/span&gt;);&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#2b91af"&gt;FileSystemWatcher&lt;/span&gt; fs = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FileSystemWatcher&lt;/span&gt;(path);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    fs.EnableRaisingEvents = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;; &lt;span style="color:#008000"&gt;// must be set to true&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    fs.IncludeSubdirectories = &lt;span style="color:#0000ff"&gt;true&lt;/span&gt;;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;//fs.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    fs.Changed += &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FileSystemEventHandler&lt;/span&gt;(fs_Changed);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    fs.Renamed += &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;RenamedEventHandler&lt;/span&gt;(fs_Renamed);&lt;/li&gt; &lt;li&gt;    fs.Created += &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FileSystemEventHandler&lt;/span&gt;(fs_Changed);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    fs.Deleted += &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;FileSystemEventHandler&lt;/span&gt;(fs_Changed);&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    Application.Add(&lt;span style="color:#a31515"&gt;&amp;quot;watcher&amp;quot;&lt;/span&gt;, fs);            &lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;static&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; fs_Renamed(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;RenamedEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; path = &lt;span style="color:#a31515"&gt;@&amp;quot;C:&amp;#92;Users&amp;#92;username&amp;#92;Desktop&amp;#92;lognet.txt&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;File&lt;/span&gt;.AppendAllText(path, e.Name + &lt;span style="color:#a31515"&gt;&amp;quot;-&amp;quot;&lt;/span&gt; + e.ChangeType);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;void&lt;/span&gt; fs_Changed(&lt;span style="color:#0000ff"&gt;object&lt;/span&gt; sender, &lt;span style="color:#2b91af"&gt;FileSystemEventArgs&lt;/span&gt; e)&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; path = &lt;span style="color:#a31515"&gt;@&amp;quot;C:&amp;#92;Users&amp;#92;username&amp;#92;Desktop&amp;#92;lognet.txt&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#2b91af"&gt;File&lt;/span&gt;.AppendAllText(path, e.Name + &lt;span style="color:#a31515"&gt;&amp;quot; - &amp;quot;&lt;/span&gt; + e.ChangeType);&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-6929086565205119731?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/6929086565205119731/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=6929086565205119731" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6929086565205119731?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/6929086565205119731?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/11/monitor-file-system-in-net-using-c.html" title="Monitor file system in .NET using C#" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C0IFQHg-fyp7ImA9Wx5WFE8.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-4339813195602984095</id><published>2010-09-25T05:44:00.001-07:00</published><updated>2010-09-25T06:31:51.657-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-25T06:31:51.657-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>List files and pictures in directories using C#</title><content type="html">&lt;p&gt;It is quite simple to list all files in a directory in C# using the System.IO.Directory class. This class contains the GetFiles(string path) method that returns all filenames as a string array within the path specified. It also contains the overloaded GetFiles(string path, string match) method that returns all filenames in a string array in the path and criteria (match) specified.&lt;/p&gt; &lt;p&gt;In the following example, I have demonstrated how to get all files in a directory and how to get all pictures from a particular directory.&lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:b78911aa-67d1-4eab-9624-476dfb931671" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DirectoryListing&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;{&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; path = &lt;span style="color:#a31515"&gt;@&amp;quot;C:&amp;#92;Users&amp;#92;shahed.kazi&amp;#92;Desktop&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;//Get all files in desktop&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] GetAllFiles()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; &lt;span style="color:#2b91af"&gt;Directory&lt;/span&gt;.GetFiles(path);&lt;/li&gt; &lt;li&gt;    }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;    &lt;span style="color:#008000"&gt;//Get pictures from desktop&lt;/span&gt;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    &lt;span style="color:#0000ff"&gt;public&lt;/span&gt; &lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[] GetAllPictures()&lt;/li&gt; &lt;li&gt;    {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[]&amp;gt; list = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;[]&amp;gt;();&lt;/li&gt; &lt;li&gt;        list.Add(&lt;span style="color:#2b91af"&gt;Directory&lt;/span&gt;.GetFiles(path, &lt;span style="color:#a31515"&gt;&amp;quot;*.bmp&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        list.Add(&lt;span style="color:#2b91af"&gt;Directory&lt;/span&gt;.GetFiles(path, &lt;span style="color:#a31515"&gt;&amp;quot;*.png&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;        list.Add(&lt;span style="color:#2b91af"&gt;Directory&lt;/span&gt;.GetFiles(path, &lt;span style="color:#a31515"&gt;&amp;quot;*.gif&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        list.Add(&lt;span style="color:#2b91af"&gt;Directory&lt;/span&gt;.GetFiles(path, &lt;span style="color:#a31515"&gt;&amp;quot;*.jpg&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li&gt;        list.Add(&lt;span style="color:#2b91af"&gt;Directory&lt;/span&gt;.GetFiles(path, &lt;span style="color:#a31515"&gt;&amp;quot;*.jpeg&amp;quot;&lt;/span&gt;));&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt; filename = &lt;span style="color:#0000ff"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color:#0000ff"&gt;string&lt;/span&gt;&amp;gt;();&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        &lt;span style="color:#0000ff"&gt;for&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; list.Count; i++)&lt;/li&gt; &lt;li&gt;        {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            &lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#0000ff"&gt;string&lt;/span&gt; s &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; list[i])&lt;/li&gt; &lt;li&gt;                filename.Add(s);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;        }&lt;/li&gt; &lt;li&gt;        &lt;span style="color:#0000ff"&gt;return&lt;/span&gt; filename.ToArray();&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;    }&lt;/li&gt; &lt;li&gt;}&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-4339813195602984095?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/4339813195602984095/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=4339813195602984095" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4339813195602984095?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/4339813195602984095?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/09/get-files-and-pictures-using-c.html" title="List files and pictures in directories using C#" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;D0IHQ3o5eip7ImA9Wx5WEk4.&quot;"><id>tag:blogger.com,1999:blog-2826546522402239434.post-8330251826699583150</id><published>2010-09-23T02:52:00.001-07:00</published><updated>2010-09-23T02:52:12.422-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2010-09-23T02:52:12.422-07:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="C#" /><title>Get Drive Information in Windows Using C#</title><content type="html">&lt;p&gt;It is easy to get the drive information using C#. Information like available free space, name and volume label of the drive, total size of the drive can be easily obtained. Below is a sample code to demonstrate how the drive information can be obtained. System.IO.DriveInfo class has been used to retrieve drive information. Please note that the code is written within a try-catch block since exceptions may arise while using IO. &lt;/p&gt; &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:c6c404e7-3f7c-459d-841f-a8fa613b7669" class="wlWriterEditableSmartContent"&gt; &lt;div style="border: #000080 1px solid; color: #000; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt; &lt;div style="background: #000080; color: #fff; font-family: Verdana, Tahoma, Arial, sans-serif; font-weight: bold; padding: 2px 5px"&gt;Code Snippet&lt;/div&gt; &lt;div style="background: #ddd; overflow: auto"&gt; &lt;ol style="background: #ffffff; margin: 0 0 0 2.5em; padding: 0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff"&gt;foreach&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;DriveInfo&lt;/span&gt; di &lt;span style="color:#0000ff"&gt;in&lt;/span&gt; &lt;span style="color:#2b91af"&gt;DriveInfo&lt;/span&gt;.GetDrives())&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            {&lt;/li&gt; &lt;li&gt;                &lt;span style="color:#0000ff"&gt;string&lt;/span&gt; br = &lt;span style="color:#a31515"&gt;&amp;quot;&amp;lt;br/&amp;gt;&amp;quot;&lt;/span&gt;;&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;try&lt;/span&gt;&lt;/li&gt; &lt;li&gt;                {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;available free space: &amp;quot;&lt;/span&gt; + di.AvailableFreeSpace + br);&lt;/li&gt; &lt;li&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;drive format: &amp;quot;&lt;/span&gt; + di.DriveFormat + br);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;drive type: &amp;quot;&lt;/span&gt; + di.DriveType + br);&lt;/li&gt; &lt;li&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;is ready: &amp;quot;&lt;/span&gt; + di.IsReady + br);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;name: &amp;quot;&lt;/span&gt; + di.Name + br);&lt;/li&gt; &lt;li&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;root directory: &amp;quot;&lt;/span&gt; + di.RootDirectory + br);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;total free space: &amp;quot;&lt;/span&gt; + di.TotalFreeSpace + br);&lt;/li&gt; &lt;li&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;total size: &amp;quot;&lt;/span&gt; + di.TotalSize + br);&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    Response.Write(&lt;span style="color:#a31515"&gt;&amp;quot;volume label: &amp;quot;&lt;/span&gt; + di.VolumeLabel + br);&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                &lt;span style="color:#0000ff"&gt;catch&lt;/span&gt; (&lt;span style="color:#2b91af"&gt;Exception&lt;/span&gt; ex)&lt;/li&gt; &lt;li&gt;                {&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;                    Response.Write(ex.Message);&lt;/li&gt; &lt;li&gt;                }&lt;/li&gt; &lt;li style="background: #f3f3f3"&gt;            }&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2826546522402239434-8330251826699583150?l=shahed-kazi.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel="replies" type="application/atom+xml" href="http://shahed-kazi.blogspot.com/feeds/8330251826699583150/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=2826546522402239434&amp;postID=8330251826699583150" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8330251826699583150?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2826546522402239434/posts/default/8330251826699583150?v=2" /><link rel="alternate" type="text/html" href="http://shahed-kazi.blogspot.com/2010/09/get-drive-information-in-windows-using.html" title="Get Drive Information in Windows Using C#" /><author><name>Shahed Kazi</name><uri>http://www.blogger.com/profile/15429299667326709900</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="16" height="16" src="http://img2.blogblog.com/img/b16-rounded.gif" /></author><thr:total>0</thr:total></entry></feed>

