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

<channel>
	<title>High On MicroSoft</title>
	<atom:link href="http://www.highonmicrosoft.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.highonmicrosoft.com</link>
	<description>Just another WordPress weblog</description>
	<pubDate>Mon, 06 Jul 2009 11:40:33 +0000</pubDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Enabe Tracing for the Page Level and Application Level in ASP.net</title>
		<link>http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html</link>
		<comments>http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html#comments</comments>
		<pubDate>Mon, 06 Jul 2009 11:33:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[.net tracing]]></category>

		<category><![CDATA[application level tracing]]></category>

		<category><![CDATA[asp application level tracing]]></category>

		<category><![CDATA[asp configuration option]]></category>

		<category><![CDATA[asp page level tracing]]></category>

		<category><![CDATA[asp tracing]]></category>

		<category><![CDATA[asp tracing at application level]]></category>

		<category><![CDATA[asp.net application level tracing]]></category>

		<category><![CDATA[asp.net configuration option]]></category>

		<category><![CDATA[asp.net page level tracing]]></category>

		<category><![CDATA[asp.net Trace]]></category>

		<category><![CDATA[ASP.NET trace information]]></category>

		<category><![CDATA[asp.net tracing]]></category>

		<category><![CDATA[ASP.NET Web application]]></category>

		<category><![CDATA[ASP.NET XML configuration]]></category>

		<category><![CDATA[c# application level tracing]]></category>

		<category><![CDATA[c# configuration option]]></category>

		<category><![CDATA[c# page level tracing]]></category>

		<category><![CDATA[c# tracing]]></category>

		<category><![CDATA[c#.net tracing]]></category>

		<category><![CDATA[config.web]]></category>

		<category><![CDATA[configuration]]></category>

		<category><![CDATA[Page Directives]]></category>

		<category><![CDATA[page level tracing]]></category>

		<category><![CDATA[pageoutput]]></category>

		<category><![CDATA[requestlimit]]></category>

		<category><![CDATA[SortByTimeSortByCategory]]></category>

		<category><![CDATA[Trace]]></category>

		<category><![CDATA[trace enabled]]></category>

		<category><![CDATA[trace.axd]]></category>

		<category><![CDATA[tracemode]]></category>

		<category><![CDATA[tracing at application level]]></category>

		<category><![CDATA[tracing at page level]]></category>

		<category><![CDATA[Web application]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2672</guid>
		<description><![CDATA[

Introduction
Enable the tracing at page level using the Page Directives
Enable the Tracing at Application Level using the Configuration Option ( config.web ) 
References:



Introduction
Tracing is the new functionality added to the Asp.net. It allows you to view Diagnostic Information about a single request for an ASP.NET page, simply by enabling it for your page or application [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html#toc-enable-the-tracing-at-page-level-using-the-page-directives"><strong>Enable the tracing at page level using the Page Directives</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html#toc-enable-the-tracing-at-application-level-using-the-configuration-option-configweb"><strong>Enable the Tracing at Application Level using the Configuration Option ( config.web ) </strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html#toc-references"><strong>References:</strong></a></li>
</ol>
</div>
<p><span id="more-2672"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>Tracing is the new functionality added to the Asp.net. It allows you to view Diagnostic Information about a single request for an ASP.NET page, simply by enabling it for your page or application level.</p>
<p>Other smart thing about tracing is, they are automatically removed from the application when it is deployed to production servers. The trace information can be viewed in two ways:</p>
<ul>
<li>One way is, ASP.NET appends the trace information at the end of the Page</li>
<li>Other is, saved to the application&#8217;s trace.axd file</li>
</ul>
<p>In this tutorial I will show how to enable the tracing at page level and application level in the following sections as follows:</p>
<ul>
<li>Enable the tracing at Page Level using the Page Directives</li>
<li>Enable the tracing at Application Level using the Configuration Option</li>
</ul>
<h2 id="toc-enable-the-tracing-at-page-level-using-the-page-directives"><strong>Enable the tracing at page level using the Page Directives</strong></h2>
<p>Page directives are very useful, these are instructions that ASP.NET uses when processing a request for an ASP.NET resource. They can also be used to override or apply configuration settings for an ASP.NET page. <strong>Note that, Page directives must be the first element on a page. </strong></p>
<p>We can add the page directives using the &lt;%@  %&gt;. as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Page Trace=<span class="st0">&quot;true&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
</ol>
</div>
<h2 id="toc-enable-the-tracing-at-application-level-using-the-configuration-option-configweb"><strong>Enable the Tracing at Application Level using the Configuration Option ( config.web ) </strong></h2>
<p>ASP.NET uses XML configuration file called <strong>config.web</strong> to set configuration settings for ASP.NET applications. Configuration file sets the default ASP.NET configuration, and in Beta 1 is known as the root configuration file. Changes made within this file affect all of our ASP.NET Web application.</p>
<p>To add application level tracing, first open the config.web file and find the &lt;trace&gt; as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;configuration&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;trace</div>
</li>
<li class="li1">
<div class="de1">enabled=<span class="st0">&quot;true&quot;</span></div>
</li>
<li class="li2">
<div class="de2">requestlimit=<span class="st0">&quot;10&quot;</span></div>
</li>
<li class="li1">
<div class="de1">pageoutput=<span class="st0">&quot;true&quot;</span></div>
</li>
<li class="li1">
<div class="de1">tracemode=<span class="st0">&quot;SortByTime&quot;</span></div>
</li>
<li class="li1">
<div class="de1">/&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/configuration&gt;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>Enabled argument enables the tracing by setting its value as true. The other interesting option is, we can sort the trace output by time and Category by setting the pageoutput argument to SortByTime  and  SortByCategory <strong>respectively</strong>. </p>
<h2 id="toc-references"><strong>References:</strong></h2>
<p><a href="http://msdn.microsoft.com/en-us/library/ms972204.aspx ">http://msdn.microsoft.com/en-us/library/ms972204.aspx </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/how-to-enabe-tracing-for-the-page-level-and-application-level-in-asp.net.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Various ways to Prevent Users, To GoBack to the Previous Pages</title>
		<link>http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html</link>
		<comments>http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#comments</comments>
		<pubDate>Mon, 06 Jul 2009 11:04:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[asp sessions]]></category>

		<category><![CDATA[asp tutorials]]></category>

		<category><![CDATA[asp.net go back to the previous page]]></category>

		<category><![CDATA[asp.net HttpCachePolicy]]></category>

		<category><![CDATA[asp.net HttpCachePolicy Class]]></category>

		<category><![CDATA[asp.net sessions]]></category>

		<category><![CDATA[asp.net tutorials]]></category>

		<category><![CDATA[Attributes.Add]]></category>

		<category><![CDATA[c# go back to the previous page]]></category>

		<category><![CDATA[c# HttpCachePolicy]]></category>

		<category><![CDATA[c# sessions]]></category>

		<category><![CDATA[C# tutorials]]></category>

		<category><![CDATA[c#.net HttpCachePolicy class]]></category>

		<category><![CDATA[c#.net sessions]]></category>

		<category><![CDATA[c#.net tutorials]]></category>

		<category><![CDATA[DateTime.Now.AddDays]]></category>

		<category><![CDATA[DHTML]]></category>

		<category><![CDATA[Disable ToolBar]]></category>

		<category><![CDATA[Drawbacks]]></category>

		<category><![CDATA[history.forward]]></category>

		<category><![CDATA[history.go]]></category>

		<category><![CDATA[history.length]]></category>

		<category><![CDATA[html]]></category>

		<category><![CDATA[HTTP headers]]></category>

		<category><![CDATA[HttpCacheability.NoCache]]></category>

		<category><![CDATA[HttpCachePolicy]]></category>

		<category><![CDATA[HttpCachePolicy Class]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[javascript Attributes.Add]]></category>

		<category><![CDATA[javascript go back to the previous page]]></category>

		<category><![CDATA[javascript location]]></category>

		<category><![CDATA[javascript location.replace]]></category>

		<category><![CDATA[javascript replace]]></category>

		<category><![CDATA[javascript self]]></category>

		<category><![CDATA[location.replace]]></category>

		<category><![CDATA[onLoad]]></category>

		<category><![CDATA[page init]]></category>

		<category><![CDATA[Page_Init]]></category>

		<category><![CDATA[Prevent Users To GoBack to the Previous Pages]]></category>

		<category><![CDATA[previous page]]></category>

		<category><![CDATA[Redirect]]></category>

		<category><![CDATA[Remove the tool bar of the browser]]></category>

		<category><![CDATA[Response]]></category>

		<category><![CDATA[Response.Cache.SetCacheability]]></category>

		<category><![CDATA[Response.Cache.SetExpires]]></category>

		<category><![CDATA[Response.End]]></category>

		<category><![CDATA[Response.Redirect]]></category>

		<category><![CDATA[sessions ToString]]></category>

		<category><![CDATA[this.href]]></category>

		<category><![CDATA[ToString]]></category>

		<category><![CDATA[window.close]]></category>

		<category><![CDATA[window.history]]></category>

		<category><![CDATA[window.history.forward]]></category>

		<category><![CDATA[Window.open]]></category>

		<category><![CDATA[window.opener = self]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2668</guid>
		<description><![CDATA[

Introduction
Using the Java Script history.go and History Forward Method
Remove the page from history using the java script
Remove the tool bar of the Browser
Using Sessions
Using the HttpCachePolicy Class
Conclusion



Introduction
Recently I worked on a banking project in asp.net. Here, I came across a situation like, “we should not allow the user to go back to the previous page”. [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-using-the-java-script-historygo-and-history-forward-method"><strong>Using the Java Script history.go and History Forward Method</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-remove-the-page-from-history-using-the-java-script"><strong>Remove the page from history using the java script</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-remove-the-tool-bar-of-the-browser"><strong>Remove the tool bar of the Browser</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-using-sessions"><strong>Using Sessions</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-using-the-httpcachepolicy-class"><strong>Using the HttpCachePolicy Class</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html#toc-conclusion"><strong>Conclusion</strong></a></li>
</ol>
</div>
<p><span id="more-2668"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>Recently I worked on a banking project in asp.net. Here, I came across a situation like, “we should not allow the user to go back to the previous page”. At first glance, we feel like doing this is very simple. But, believe me, it is really hell. Problem with this is that there is no way of truly disabling the back button.</p>
<p>As I suffered with this issue, after a good research I thought of investing some time to write this tutorial to help some one who is like me. In this tutorial, I will explain different ways to prevent user going back and I will try list out their drawbacks also. But, remember this, I am saying again, there is no way to truly disable the back button.</p>
<p>For the sake this tutorial, we will create two pages called firstpage.aspx and other as secondpage.aspx. On the Fristpage.aspx add a Hyperlink then, catch the on_click event of the HyperLink and add the following line to it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected void Redirectlink1_Click<span class="br0">&#40;</span>…….<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">Response</span>.<span class="kw3">Redirect</span><span class="br0">&#40;</span><span class="st0">&quot;SecondPage.aspx&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Now, when the user clicks on the link, he will be re-directed to the second page.</p>
<p>Using the above two pages I will explain the different approaches by dividing them into following sections:</p>
<ul>
<li>Using the java script history.go and history forward method</li>
<li>Remove the page from history using the java script</li>
<li>Remove the tool bar of the browser</li>
<li>Using sessions</li>
<li>Using the HttpCachePolicy Class</li>
</ul>
<h2 id="toc-using-the-java-script-historygo-and-history-forward-method"><strong>Using the Java Script history.go and History Forward Method</strong></h2>
<p>We can use java script client side code to prevent users going back. The history.go is first kind of these methods, add this on the page load of the  tag as follows:</p>
<p><strong>FirstPage.aspx</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;body <span class="kw3">onLoad</span>=<span class="st0">&quot;if(history.length&gt;0)history.go(+1)&quot;</span>&gt;</div>
</li>
</ol>
</div>
<p>Other java script method called as history.forward can be as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;script language=<span class="st0">&quot;JavaScript&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;!&#8211;</div>
</li>
<li class="li1">
<div class="de1">javascript:window.<span class="me1">history</span>.<span class="kw3">forward</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//&#8211;&gt;</span></div>
</li>
<li class="li2">
<div class="de2">&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>Now, every time when the user goes backs to the FirstPage, it will be automatically redirected to second page.</p>
<p><strong>Drawbacks </strong>of this approach are, these are implemented on the client and in JavaScript, if the user disables the java script then this approach will not work.</p>
<h2 id="toc-remove-the-page-from-history-using-the-java-script"><strong>Remove the page from history using the java script</strong></h2>
<p>Instead of the above methods, we can directly remove the page from the history. Now, even if the users press the back button as the page is not their history, user cannot go to the previous page. Looks interesting right!! And it is simple also as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">javascript:location.<span class="me1">replace</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">href</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p><strong>Drawbacks </strong>are same as the first approach that we are using on the client and in JavaScript, if the user disables the java script then this approach will not work.</p>
<h2 id="toc-remove-the-tool-bar-of-the-browser"><strong>Remove the tool bar of the Browser</strong></h2>
<p>We can remove the tool bar of browser by opening a new window and referring it to self. No tool bar means no buttons, no navigation etc. this approach may help in some situations. This can be done as follows:</p>
<p>First to create the new window and refer to self , we have add the following lines of code, I am placing them in a function as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;script language =javascript&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> DisableToolBar<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">window.<span class="kw3">open</span><span class="br0">&#40;</span><span class="st0">&#8216;SmallWindow.aspx&#8217;</span>,<span class="st0">&#8217;smallwindow&#8217;</span>,<span class="st0">&#8216;toolbar=false;target=_parent&#8217;</span>,<span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">// just close the window and don&#8217;t ask to close it</span></div>
</li>
<li class="li2">
<div class="de2">window.<span class="me1">opener</span> = self;</div>
</li>
<li class="li1">
<div class="de1">window.<span class="kw3">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>And I calling this function from the Firstpage.aspx of link’s on_click event as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected void Redirectlink1_Click<span class="br0">&#40;</span>…….<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;Redirectlink1.<span class="me1">Attributes</span>.<span class="me1">Add</span> <span class="br0">&#40;</span><span class="st0">&quot;onclick&quot;</span>, <span class="st0">&quot;CreateWindow()&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h2 id="toc-using-sessions"><strong>Using Sessions</strong></h2>
<p>Till now, all approaches are on the client side, for these kinds of securities we cannot trust them. Suppose, if the user remembers the URL and enters the URL directly then, the above approaches will not work. So the other option is keeping these securities on the server side using the sessions.</p>
<p>We should keep track of the user page as session variables and when the user request for the page, first we have to check in the session variables whether the user already visited that page, if not, then only allow him to view that page as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span><span class="kw1">Session</span><span class="br0">&#91;</span><span class="st0">&quot;FirstTimeToPage&quot;</span><span class="br0">&#93;</span>.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">Length</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">Session</span><span class="br0">&#91;</span><span class="st0">&quot;FirstTimeToPage&quot;</span><span class="br0">&#93;</span> = <span class="kw1">string</span>.<span class="me1">Empty</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">Response</span>.<span class="kw3">Redirect</span> <span class="st0">&quot;/Login.aspx&quot;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;<span class="kw1">Response</span>.<span class="kw1">End</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><strong>Drawbacks</strong> of this method are, if we have lot of pages on the server then the memory used by session increases for every user. Another drawback is, if the user disables or do not support cookies, then this will not work.</p>
<h2 id="toc-using-the-httpcachepolicy-class"><strong>Using the HttpCachePolicy Class</strong></h2>
<p>In asp.net there is a class called HttpCachePolicy which contains methods for setting cache-specific HTTP headers and for controlling the ASP.NET page output cache. We can use this function in our asp pages to prevent caching, so that their will no pages to go back.(Smart One, Right!!) This can be implemented usually in the page_init as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Protected void Page_Init<span class="br0">&#40;</span>object Sender, EventArgs e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Response</span>.<span class="me1">Cache</span>.<span class="me1">SetCacheability</span><span class="br0">&#40;</span>HttpCacheability.<span class="me1">NoCache</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">Response</span>.<span class="me1">Cache</span>.<span class="me1">SetExpires</span><span class="br0">&#40;</span>DateTime.<span class="kw3">Now</span>.<span class="me1">AddDays</span><span class="br0">&#40;</span><span class="nu0">-1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<h2 id="toc-conclusion"><strong>Conclusion</strong></h2>
<p>I explained varies ways to prevent the users to go back to the previous page. But, every approach has limitations. Keep one thing in mind that there is no perfect approach to solve this problem.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/various-ways-to-prevent-users-to-goback-to-the-previous-pages.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>ASP.net Communication Between Parent And Child Windows</title>
		<link>http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html</link>
		<comments>http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 11:27:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[asp.net child window]]></category>

		<category><![CDATA[ASP.net Communication Between Parent And Child Windows]]></category>

		<category><![CDATA[asp.net tutorial]]></category>

		<category><![CDATA[Attributes]]></category>

		<category><![CDATA[Attributes.Add]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[c#.net]]></category>

		<category><![CDATA[Child windows]]></category>

		<category><![CDATA[document]]></category>

		<category><![CDATA[document.getElementById]]></category>

		<category><![CDATA[EventArgs]]></category>

		<category><![CDATA[getElementById]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[object]]></category>

		<category><![CDATA[OnClick]]></category>

		<category><![CDATA[open child window]]></category>

		<category><![CDATA[Page_Load]]></category>

		<category><![CDATA[protected]]></category>

		<category><![CDATA[Window.open]]></category>

		<category><![CDATA[Window.open javascript]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2663</guid>
		<description><![CDATA[

Introduction
Open the child window
Update the child window text box from the parent window
Update the Parent window text box from the child window



Introduction
Recently for one my asp project, I have to open pop windows from the parent window and have to set values from child window to parent and vice versa. Of course, doing this kind [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html#toc-open-the-child-window"><strong>Open the child window</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html#toc-update-the-child-window-text-box-from-the-parent-window"><strong>Update the child window text box from the parent window</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html#toc-update-the-parent-window-text-box-from-the-child-window"><strong>Update the Parent window text box from the child window</strong></a></li>
</ol>
</div>
<p><span id="more-2663"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>Recently for one my asp project, I have to open pop windows from the parent window and have to set values from child window to parent and vice versa. Of course, doing this kind of things is very simple. But, I did not found enough tutorial on this so thought of investing some time to write tutorial on this topic.</p>
<p>In this tutorial their will be a textbox and button controls on the parent and child windows, if we enter a value and press button, the value in the text box will be displayed/updated in the other window.</p>
<p>Parent window have a textbox and two button control, where one button control open the child window and other button updates the text box value to the child window (with the caption update the child window). Now in the page load function, we will add code to call the javascipt functions as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected void Page_Load<span class="br0">&#40;</span>object sender, EventArgs e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;this.<span class="me1">OpenChildWindowButton</span>.<span class="me1">Attributes</span>.<span class="me1">Add</span><span class="br0">&#40;</span><span class="st0">&quot;onclick&quot;</span>, openChildWindow<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;this.<span class="me1">UpdateChildWindowButton</span>.<span class="me1">Attributes</span>.<span class="me1">Add</span><span class="br0">&#40;</span><span class="st0">&quot;onclick&quot;</span>, UpdatechildWindow<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Child windows also have one textbox and one button controls. And we are adding following code on the <strong>page_load</strong> event:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">protected void Page_Load<span class="br0">&#40;</span>object sender, EventArgs e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;this.<span class="me1">UpdateParentwindowButton</span>.<span class="me1">Attributes</span>.<span class="me1">Add</span><span class="br0">&#40;</span><span class="st0">&quot;onclick&quot;</span>, <span class="st0">&quot;UpdateParentWindow()&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>As you observed there are three functions, <strong>openChildWindow(), UpdatechildWindow(), UpdateParentWindow()</strong>, these functions are coded in the javascirpt.  I will explain them in the following sections:</p>
<ul>
<li>Open the child window</li>
<li>Update the child window text box from the parent window</li>
<li>Update the Parent window text box from the child window</li>
</ul>
<h2 id="toc-open-the-child-window"><strong>Open the child window</strong></h2>
<p>We called a java script function in the parent window to open the child window, that is openChildWindow() as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;script language=javascript&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">Var</span> childwindow;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">function</span> OpenChildWindow<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">Childwindow = Window.<span class="kw3">open</span><span class="br0">&#40;</span>“Childwindow.<span class="me1">aspx</span>”<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">&lt;/script&gt;</span></div>
</li>
</ol>
</div>
<p><strong>Window.open</strong> javascript function opens the child window. Here we opened child window with ChildWindow.aspx.</p>
<h2 id="toc-update-the-child-window-text-box-from-the-parent-window"><strong>Update the child window text box from the parent window</strong></h2>
<p>To update the child window from the parent, we need access to the child window and as well, access to child window controls. We can do that as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;script language=javascript&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">Var</span> childwindow;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">function</span> UpdatechildWindow<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;childwindow.<span class="me1">form1</span>.<span class="me1">textbox1</span>.<span class="me1">value</span> = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&quot;TextBox1&quot;</span><span class="br0">&#41;</span>.<span class="me1">value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;</div>
</li>
</ol>
</div>
<p>We are access the child window using the variable “childwindow” variable which we initialized when are opening the child window in the OpenChildWindow () function.</p>
<h2 id="toc-update-the-parent-window-text-box-from-the-child-window"><strong>Update the Parent window text box from the child window</strong></h2>
<p>When we open the child window from the parent using the window.open, a new property is created in the child that is window.opener property. With this property we can access the parent window or the creator window from the child window.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;script language=javascript&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> UpdateParentWindow<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">Window.<span class="me1">opener</span>.<span class="me1">textbox1</span>.<span class="me1">value</span> = document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">&quot;TextBox1&quot;</span><span class="br0">&#41;</span>.<span class="me1">value</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&lt;/script&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/asp.net-communication-between-parent-and-child-windows.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Dynamically Changing the Style Sheets of the Controls</title>
		<link>http://www.highonmicrosoft.com/dynamically-changing-the-style-sheets-of-the-controls.html</link>
		<comments>http://www.highonmicrosoft.com/dynamically-changing-the-style-sheets-of-the-controls.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 10:04:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[asp.net Changing the Style Sheets]]></category>

		<category><![CDATA[asp.net css]]></category>

		<category><![CDATA[asp.net Styles sheets]]></category>

		<category><![CDATA[asp:Button]]></category>

		<category><![CDATA[asp:Label]]></category>

		<category><![CDATA[button]]></category>

		<category><![CDATA[c#.net Changing the Style Sheets]]></category>

		<category><![CDATA[Changing the Style Sheets of the Controls]]></category>

		<category><![CDATA[color]]></category>

		<category><![CDATA[css]]></category>

		<category><![CDATA[css files]]></category>

		<category><![CDATA[css Styles sheets]]></category>

		<category><![CDATA[CssClass]]></category>

		<category><![CDATA[CssClass property]]></category>

		<category><![CDATA[Dynamically Changing the Style Sheets of the Controls]]></category>

		<category><![CDATA[font-weight]]></category>

		<category><![CDATA[html]]></category>

		<category><![CDATA[html Changing the Style Sheets]]></category>

		<category><![CDATA[html tags]]></category>

		<category><![CDATA[OnClick]]></category>

		<category><![CDATA[runat]]></category>

		<category><![CDATA[style property]]></category>

		<category><![CDATA[Style Sheets of the Controls]]></category>

		<category><![CDATA[Styles sheets]]></category>

		<category><![CDATA[text/css]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2658</guid>
		<description><![CDATA[

Introduction
Change the style of label control using the CssClass property
Hide or display the label control using the style property



Introduction
HTML tags define the content of the document. To format the content, new HTML tags and attributes are introduced, but, later on, it become hard to manage them. As we all know that, it is always good [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/dynamically-changing-the-style-sheets-of-the-controls.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/dynamically-changing-the-style-sheets-of-the-controls.html#toc-change-the-style-of-label-control-using-the-cssclass-property"><strong>Change the style of label control using the CssClass property</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/dynamically-changing-the-style-sheets-of-the-controls.html#toc-hide-or-display-the-label-control-using-the-style-property"><strong>Hide or display the label control using the style property</strong></a></li>
</ol>
</div>
<p><span id="more-2658"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>HTML tags define the content of the document. To format the content, new HTML tags and attributes are introduced, but, later on, it become hard to manage them. As we all know that, it is always good to separate the document content from its presentation, this can be achieved using the style sheets.</p>
<p>Styles sheets define HOW HTML elements are to be displayed, just like the font tag and the color attribute in HTML 3.2. Styles are normally saved in external .css files.</p>
<p>Now, the topic is about controls. Some times we may need to change the appearance of controls dynamically at run time. For example, at runtime I want to apply style sheets to a control to change it appearance.</p>
<p>By keeping these kinds of requirements in mind, Microsoft developed in addition to the appearance properties (like Bold, size) and style objects (like FontInfo), two new properties which allow you to manipulate CSS styles more directly as follows:</p>
<ol>
<li>CssClass</li>
<li>Style</li>
</ol>
<p>In this tutorial I will try to explain how to use these two properties with examples in the following sections:</p>
<ul>
<li>Change the style of label control using the CssClass Property</li>
<li>Hide or Display the label Control using the Style Property</li>
</ul>
<h2 id="toc-change-the-style-of-label-control-using-the-cssclass-property"><strong>Change the style of label control using the CssClass property</strong></h2>
<p>First we will look at the HTML part of the code, so that we can discuss about it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;html  &gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;head id=&quot;Head2&quot; runat=&quot;server&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;title&gt;CssClass Property Example&lt;/title&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;style type=&quot;text/css&quot;&gt;
</div>
</li>
<li class="li2">
<div class="de2">.CssStyle1
</div>
</li>
<li class="li1">
<div class="de1">{
</div>
</li>
<li class="li1">
<div class="de1">font: 10pt Verdana;
</div>
</li>
<li class="li1">
<div class="de1">font-weight:700;
</div>
</li>
<li class="li1">
<div class="de1">color: Green;
</div>
</li>
<li class="li2">
<div class="de2">}
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">.CssStyle2
</div>
</li>
<li class="li1">
<div class="de1">{
</div>
</li>
<li class="li1">
<div class="de1">font: 15pt Times;
</div>
</li>
<li class="li2">
<div class="de2">font-weight:250;
</div>
</li>
<li class="li1">
<div class="de1">color: Blue;
</div>
</li>
<li class="li1">
<div class="de1">}
</div>
</li>
<li class="li1">
<div class="de1">&lt;/style&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/head&gt;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;div&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&lt;h3&gt;Style Property of a Web Control&lt;/h3&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:Label id=&quot;Label1&quot; Text=&quot;This is a label control.&quot;
</div>
</li>
<li class="li1">
<div class="de1">CssClass=&quot;CssClass1&quot;
</div>
</li>
<li class="li1">
<div class="de1">runat=&quot;server&quot;/&gt;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;p&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:Button id=&quot;Button1&quot; Text=&quot;Click to hide or unhide the label&quot;
</div>
</li>
<li class="li1">
<div class="de1">OnClick=&quot;Button1_Click&quot; runat=&quot;server&quot;/&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/p&gt;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/div&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/form&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p>Did you observed that with in the &lt;style &gt; tags there is a css style which contains <strong>.CssStyle1</strong>, <strong>.CssStyle2</strong> and with in the body we have label and button controls, where Label control have CssClass property set with the “CssClass1” style sheet and the button control’s “onlick” event have a call back function called “Button_click”.</p>
<p>Now, when the user press the button, “Button_Click” event is fired and in this function we change the style sheet of the label control dynamically using the CssClass property as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;script runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">Sub</span> Button1_Click<span class="br0">&#40;</span><span class="kw1">ByVal</span> sender <span class="kw3">As</span> Object, <span class="kw1">ByVal</span> e <span class="kw3">As</span> EventArgs<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">If</span> Label1.<span class="me1">CssClass</span> = <span class="st0">&quot;CssStyle1&quot;</span> <span class="kw1">Then</span></div>
</li>
<li class="li1">
<div class="de1">Label1.<span class="me1">CssClass</span> = <span class="st0">&quot;CssStyle2&quot;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">Else</span></div>
</li>
<li class="li1">
<div class="de1">Label1.<span class="me1">CssClass</span> = <span class="st0">&quot;CssStyle1&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">End</span> <span class="kw1">If</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">End</span> <span class="kw2">Sub</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;/script&gt;</span></div>
</li>
</ol>
</div>
<p>How simple it is right!! Using the style properties is also simple. I will explain it in the below section.</p>
<h2 id="toc-hide-or-display-the-label-control-using-the-style-property"><strong>Hide or display the label control using the style property</strong></h2>
<p>HTML part of the code is same as the above but, in this example we will use style property of the control as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;html&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;head id=&quot;Head2&quot; runat=&quot;server&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;title&gt;Style Property Example&lt;/title&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/head&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;div&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;h3&gt;Style Property of a Web Control&lt;/h3&gt;
</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:Label id=&quot;Label1&quot; Text=&quot;This is a label control.&quot;
</div>
</li>
<li class="li1">
<div class="de1">BorderStyle=&quot;Solid&quot; runat=&quot;server&quot;/&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;p&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;asp:Button id=&quot;Button1&quot; Text=&quot;Click to hide or unhide the label&quot;
</div>
</li>
<li class="li1">
<div class="de1">OnClick=&quot;Button1_Click&quot; runat=&quot;server&quot;/&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/p&gt;
</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/div&gt;
</div>
</li>
<li class="li2">
<div class="de2">&lt;/form&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;
</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p>The &lt;head&gt; tag just have title and with in body of the html we have a label and button control.Where the button control “onclick” event have a call back function called “Button_click”.</p>
<p>In the Button_click function, we will show or hide the label control using the style property as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;script runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">Sub</span> Button1_Click<span class="br0">&#40;</span>sender <span class="kw3">As</span> Object, e <span class="kw3">As</span> EventArgs<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">If</span> Label1.<span class="me1">Style</span><span class="br0">&#40;</span><span class="st0">&quot;visibility&quot;</span><span class="br0">&#41;</span> = <span class="st0">&quot;hidden&quot;</span> <span class="kw1">Then</span></div>
</li>
<li class="li1">
<div class="de1">Label1.<span class="me1">Style</span><span class="br0">&#40;</span><span class="st0">&quot;visibility&quot;</span><span class="br0">&#41;</span> = <span class="st0">&quot;show&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Else</span></div>
</li>
<li class="li1">
<div class="de1">Label1.<span class="me1">Style</span><span class="br0">&#40;</span><span class="st0">&quot;visibility&quot;</span><span class="br0">&#41;</span> = <span class="st0">&quot;hidden&quot;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">End</span> <span class="kw1">If</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">End</span> <span class="kw2">Sub</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;/script&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/dynamically-changing-the-style-sheets-of-the-controls.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Dynamically Loading and Accessing the Properties of the User Control</title>
		<link>http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html</link>
		<comments>http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 09:39:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[add]]></category>

		<category><![CDATA[asp.net user control]]></category>

		<category><![CDATA[c# user control]]></category>

		<category><![CDATA[c#.net user control]]></category>

		<category><![CDATA[Dynamically Loading and Accessing the Properties of the User Control]]></category>

		<category><![CDATA[LoadControl]]></category>

		<category><![CDATA[loading the user control]]></category>

		<category><![CDATA[loading user control]]></category>

		<category><![CDATA[panel]]></category>

		<category><![CDATA[PlaceHolder]]></category>

		<category><![CDATA[Properties of User Control]]></category>

		<category><![CDATA[Register]]></category>

		<category><![CDATA[TagPrefix]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2653</guid>
		<description><![CDATA[

Introduction
Loading the user Controls Dynamically
Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need
Accessing the properties of the dynamically loaded User Control 



Introduction
User control are powerful, they can divide the code and helps us to reuse them. Normally we can use the UserControl’s in two ways:

Placing the user control by [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html#toc-loading-the-user-controls-dynamically"><strong>Loading the user Controls Dynamically</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html#toc-using-the-placeholders-and-panel-controls-to-display-the-user-controls-where-ever-you-need"><strong>Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html#toc-accessing-the-properties-of-the-dynamically-loaded-user-control"><strong>Accessing the properties of the dynamically loaded User Control </strong></a></li>
</ol>
</div>
<p><span id="more-2653"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>User control are powerful, they can divide the code and helps us to reuse them. Normally we can use the UserControl’s in two ways:</p>
<ol>
<li><strong>Placing the user control by dragging and dropping it to a page at the design time</strong>:  If you drag and drop the user control, it adds following two important lines to your code:    &lt;%@ Register TagPrefix=&#8221;uc1&#8243; TagName=&#8221;HeaderUserControl&#8221; Src=&#8221;example.ascx&#8221; %&gt;   This includes the example user control from the example.ascx.    The example control is then positioned on the page using the following tag:
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;uc1:ExampleUserControl id=<span class="st0">&quot;Example1&quot;</span> runat=<span class="st0">&quot;server&quot;</span>&gt;&lt;/uc1:ExampleUserControl&gt;</div>
</li>
</ol>
</div>
</li>
<li><strong>Other is, loading them dynamically when ever needed:</strong><br />
Some times you may need to load the user control at runtime or dynamically. For this purpose there is a method called LoadControl.  This function has a straightforward syntax - it takes a single argument - the virtual path to the user control page.In this tutorial I will try to explain in detail about loading user controls dynamically.</li>
</ol>
<p>Loading the user controls is simple but, it is not easy to make the dynamically loaded controls working properly, if we missed some key points. So, in this tutorial, I will try to explain the things by dividing it into following sections:</p>
<ul>
<li>Loading the user Controls Dynamically</li>
<li>Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need</li>
<li>Accessing the properties of the dynamically loaded User Control</li>
</ul>
<h2 id="toc-loading-the-user-controls-dynamically"><strong>Loading the user Controls Dynamically</strong></h2>
<p>As I said, there is method called LoadControl(), which loads the usercontrols dynamically as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">ExampleUserControl uc = LoadControl<span class="br0">&#40;</span><span class="st0">&quot;ExampleUserControl.ascx&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>LoadControl takes just single argument that is the virtual path of the user control. Some people asked me on forums like, why cannot we use new operator to create an instance of the user control? The answer is simple, if we create an instance of the user control only an instance of the control is created but, not all the child controls on the UserControl.</p>
<p>Once, we load the user control, it should be added to page control collection as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; Controls.<span class="me1">Add</span><span class="br0">&#40;</span>uc<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<h2 id="toc-using-the-placeholders-and-panel-controls-to-display-the-user-controls-where-ever-you-need"><strong>Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need</strong></h2>
<p>Above section, helped us to load the control and add it to the controls collectins. But, the drawback is that, we cannot control, where the UserControl actually appear on the page. So, I prefer, always using PlaceHolders and Panel controls. We can add them to the page by just dragging and dropping it on page. Adding the loaded control to them is simple as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">PlaceHolderright.<span class="me1">Controls</span>.<span class="me1">Add</span><span class="br0">&#40;</span>uc<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">PanelRight.<span class="me1">Controls</span>.<span class="me1">Add</span><span class="br0">&#40;</span>uc<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<h2 id="toc-accessing-the-properties-of-the-dynamically-loaded-user-control"><strong>Accessing the properties of the dynamically loaded User Control </strong></h2>
<p>I accept that, their will be multiple ways do to a same job. Here, I am accessing the properties of the UserControl by casting the UserControl to its class as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;Control uc = LoadControl<span class="br0">&#40;</span><span class="st0">&quot;ExampleUserControl.ascx&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp;ExampleUserControl example1 = <span class="br0">&#40;</span>ExampleUserControl<span class="br0">&#41;</span>uc;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;example1.<span class="me1">xyzproperty</span> = <span class="kw2">true</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;PlaceHolderright.<span class="me1">Controls</span>.<span class="me1">Add</span><span class="br0">&#40;</span>example1<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p><strong>Note that</strong>, the above code should be placed in the code-behind file of web form. </p>
<p>Above code snippets, first loads the usercontrol, casts the loaded usercontrol generic one to the example control and then, we are accessing the properties of controls. At last, we are adding the example1 to controls collection.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/dynamically-loading-and-accessing-the-properties-of-the-user-control.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Popluate the DropDownList inside the DataGrid</title>
		<link>http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html</link>
		<comments>http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 09:17:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[Add DropDownList controls to templates]]></category>

		<category><![CDATA[add DropDownList to the DataGrid]]></category>

		<category><![CDATA[Adding the DropDownList controls to templates]]></category>

		<category><![CDATA[asp:DropDownList]]></category>

		<category><![CDATA[Bind the Datagrid Control]]></category>

		<category><![CDATA[Bind the Datagrid Control to the Data Source]]></category>

		<category><![CDATA[bound columns]]></category>

		<category><![CDATA[Cells]]></category>

		<category><![CDATA[data-bound control]]></category>

		<category><![CDATA[DataBind]]></category>

		<category><![CDATA[Datagrid]]></category>

		<category><![CDATA[dataset]]></category>

		<category><![CDATA[DataTextField]]></category>

		<category><![CDATA[DataValueField]]></category>

		<category><![CDATA[DropDownlist]]></category>

		<category><![CDATA[DropDownList control]]></category>

		<category><![CDATA[DropDownList from the Database]]></category>

		<category><![CDATA[DropDownList xml]]></category>

		<category><![CDATA[Edit template]]></category>

		<category><![CDATA[FindControl]]></category>

		<category><![CDATA[How to add DropDownList to the DataGrid]]></category>

		<category><![CDATA[IsPostBack]]></category>

		<category><![CDATA[Items.Add]]></category>

		<category><![CDATA[ListItem]]></category>

		<category><![CDATA[Load]]></category>

		<category><![CDATA[MapPath]]></category>

		<category><![CDATA[Page.IsPostBack]]></category>

		<category><![CDATA[Page_Load]]></category>

		<category><![CDATA[Popluate DropDownList]]></category>

		<category><![CDATA[Popluate the DropDownList]]></category>

		<category><![CDATA[Popluate the DropDownList inside the DataGrid]]></category>

		<category><![CDATA[Populate the DropDownList]]></category>

		<category><![CDATA[Populate the DropDownList from the Database]]></category>

		<category><![CDATA[Populate the DropDownList with XML file]]></category>

		<category><![CDATA[SelectNodes]]></category>

		<category><![CDATA[SelectSingleNode]]></category>

		<category><![CDATA[SqlDataAdapter]]></category>

		<category><![CDATA[tabular format]]></category>

		<category><![CDATA[template column]]></category>

		<category><![CDATA[xml load]]></category>

		<category><![CDATA[XmlDocument]]></category>

		<category><![CDATA[XmlNodeList]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2649</guid>
		<description><![CDATA[

Introduction
How to add the DropDownList to the DataGrid
Bind the Datagrid Control to the Data Source
Populate the DropDownList with XML file
Populate the DropDownList from the Database



Introduction
DataGrid is very useful control. It is a full-featured data-bound control that displays data in tabular format, and provides the ability to sort, select, edit, and delete records from its associated [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html#toc-how-to-add-the-dropdownlist-to-the-datagrid"><strong>How to add the DropDownList to the DataGrid</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html#toc-bind-the-datagrid-control-to-the-data-source"><strong>Bind the Datagrid Control to the Data Source</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html#toc-populate-the-dropdownlist-with-xml-file"><strong>Populate the DropDownList with XML file</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html#toc-populate-the-dropdownlist-from-the-database"><strong>Populate the DropDownList from the Database</strong></a></li>
</ol>
</div>
<p><span id="more-2649"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>DataGrid is very useful control. It is a full-featured data-bound control that displays data in tabular format, and provides the ability to sort, select, edit, and delete records from its associated data source.</p>
<p>On other end, DropDownList control lists the values in the drop down fashion. Some times, we have to use DropDownList controls in the DataGrid . Suppose, to select the country of the employee or user, it is good if we have drop down lists which lists all the countries.</p>
<p>In this tutorial I will try to explain the following things:</p>
<ul>
<li>How to add the DropDownList to the DataGrid</li>
<li>Bind the Datagrid Control to the Data Source</li>
<li>Populate the DropDownList with XML file</li>
<li>Populate the DropDownList from the Database</li>
</ul>
<h2 id="toc-how-to-add-the-dropdownlist-to-the-datagrid"><strong>How to add the DropDownList to the DataGrid</strong></h2>
<p>Adding the DropDownList to the Datagrid is simple, first add template columns to DataGrid and then, add the DropDownList control to the template. I will try to explain in step by step manner as follows:</p>
<ol>
<li> Drag and drop DataGrid onto the web form.</li>
<li> Right click on DataGrid -&gt; select Property Builder.</li>
<li> In Property Builder, select Columns.</li>
<li> Add two bound columns and</li>
<li> Add one template column. (Remember to add bound columns first and than the template column.)</li>
<li> Uncheck Create columns automatically.</li>
<li> In the same window, click on the bound column appearing on the right window and set its header text to &#8220;Employee ID&#8221; and DataField to &#8220;EmpID&#8221;. Click on the second bound column appearing in the right window and set header text to &#8220;Employee Name&#8221; and DataField to &#8220;EmpName&#8221;</li>
</ol>
<p><em><strong>Adding the DropDownList controls to templates</strong></em></p>
<ol>
<li> Now, to add the DropDownList, simple right click on DataGrid and select the option Edit template.</li>
<li> In the item template, just drag and drop a DropDownList in item template.</li>
</ol>
<h2 id="toc-bind-the-datagrid-control-to-the-data-source"><strong>Bind the Datagrid Control to the Data Source</strong></h2>
<p>Above create Datagrid control has three columns EmpId, EmpName and Countries. Leave the countries column for now, I will explain it another two sections. Now, we need fill the EmpId and EmpName columns from the data base table employee.  To fill the data grid from the data base, it is best to go with binding to the data source as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">private</span> void Page_Load<span class="br0">&#40;</span>object sender, System.<span class="me1">EventArgs</span> e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>!Page.<span class="me1">IsPostBack</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp;BindData<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">public</span> void BindData<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;SqlDataAdapter ad = <span class="kw2">new</span> SqlDataAdapter<span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM employee&quot;</span>,myConnection<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;DataSet ds = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ad.<span class="me1">Fill</span><span class="br0">&#40;</span>ds,<span class="st0">&quot;employee&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;DataGrid1.<span class="me1">DataSource</span> = ds;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;DataGrid1.<span class="me1">DataBind</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>We are creating the dataset and filling it with the result of adapter (ad). Now, the important thing is we are setting the data source to the dataset (ds) and we are calling DataBind() method to bind the DataSet with DataGrid. The Datagrid control automatically populates the grid control with records of the employee table.</p>
<h2 id="toc-populate-the-dropdownlist-with-xml-file"><strong>Populate the DropDownList with XML file</strong></h2>
<p>We have populated the empid and empname from the database. Now, the other column is the countries, we have an xml file called countires.xml as follows:<br />
<strong>Countries.xml</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;utf-8&quot;</span> <span class="re2">?&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;details<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countryname<span class="re2">&gt;</span></span></span>USA<span class="sc3"><span class="re1">&lt;/countryname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countryname<span class="re2">&gt;</span></span></span>China<span class="sc3"><span class="re1">&lt;/countryname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/details<span class="re2">&gt;</span></span></span></div>
</li>
</ol>
</div>
<p>We have to get country details from this xml file and populate the DropDownList in the DataGrid. There is name space called System.XML which helps us to parse and work with the XML files as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">XmlDocument doc = <span class="kw2">new</span> XmlDocument<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">doc.<span class="me1">Load</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span><span class="st0">&quot;countries.xml&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">XmlNodeList nodeList = doc.<span class="me1">SelectNodes</span><span class="br0">&#40;</span><span class="st0">&quot;details/countries&quot;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>We are creating the xml document and loading the xml file into it. At last, we are getting the list of nodes from the xml file.</p>
<p>Now, the question is how to access and populate the DropDownList in the Datagrid. It is simple,</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">private</span> void Page_Load<span class="br0">&#40;</span>object sender, System.<span class="me1">EventArgs</span> e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="kw1">if</span><span class="br0">&#40;</span>!Page.<span class="me1">IsPostBack</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp;BindData<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp;FillCountries<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">public</span> void BindData<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;SqlDataAdapter ad = <span class="kw2">new</span> SqlDataAdapter<span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM employee&quot;</span>,myConnection<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;DataSet ds = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;ad.<span class="me1">Fill</span><span class="br0">&#40;</span>ds,<span class="st0">&quot;employee&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;DataGrid1.<span class="me1">DataSource</span> = ds;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;DataGrid1.<span class="me1">DataBind</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">public</span> void    FillCountries<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;XmlDocument doc = <span class="kw2">new</span> XmlDocument<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;doc.<span class="me1">Load</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span><span class="st0">&quot;countries.xml&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;XmlNodeList nodeList = doc.<span class="me1">SelectNodes</span><span class="br0">&#40;</span><span class="st0">&quot;details/countries&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;foreach<span class="br0">&#40;</span>XmlNode node <span class="kw1">in</span> nodeList<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;<span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp;ListItem Litem = <span class="kw2">new</span> ListItem<span class="br0">&#40;</span>node.<span class="me1">SelectSingleNode</span><span class="br0">&#40;</span><span class="st0">&quot;countryname&quot;</span><span class="br0">&#41;</span>.<span class="me1">InnerText</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#40;</span><span class="br0">&#40;</span>DropDownList<span class="br0">&#41;</span>sampleGrid.<span class="me1">Items</span><span class="br0">&#91;</span>num<span class="br0">&#93;</span>.<span class="me1">Cells</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>.<span class="me1">FindControl</span><span class="br0">&#40;</span><span class="st0">&quot;countires&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">Items</span>.<span class="me1">Add</span> <span class="br0">&#40;</span>Litem<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>FillCountries() method reads the xml file and adds new list item to DataGrid DropDownList control.</p>
<h2 id="toc-populate-the-dropdownlist-from-the-database"><strong>Populate the DropDownList from the Database</strong></h2>
<p>Compared to xml approach this database is where simple. Just get the dataset and set this dataset as datasource property of the DropdownList control as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw1">private</span> void Page_Load<span class="br0">&#40;</span>object sender, System.<span class="me1">EventArgs</span> e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>!Page.<span class="me1">IsPostBack</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; BindData<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; FillCountries<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">public</span> void BindData<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; SqlDataAdapter ad = <span class="kw2">new</span> SqlDataAdapter<span class="br0">&#40;</span><span class="st0">&quot;SELECT * FROM employee&quot;</span>,myConnection<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; DataSet ds = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; ad.<span class="me1">Fill</span><span class="br0">&#40;</span>ds,<span class="st0">&quot;employee&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; DataGrid1.<span class="me1">DataSource</span> = ds;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; DataGrid1.<span class="me1">DataBind</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">public</span> DataSet &nbsp;FillCountries<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;SqlDataAdapter ad = <span class="kw2">new</span> SqlDataAdapter<span class="br0">&#40;</span><span class="st0">&quot;SELECT name FROM &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Countries&quot;</span>, myConnection<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;DataSet ds = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp;ad.<span class="me1">Fill</span><span class="br0">&#40;</span>ds,<span class="st0">&quot;Categories&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp;return ds;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>We have another important step here, search for the DropDownList in the aspx HTML page and modify it has follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:DropDownList id=<span class="st0">&quot;countryList&quot;</span> runat=<span class="st0">&quot;server&quot;</span></div>
</li>
<li class="li1">
<div class="de1">DataSource=<span class="st0">&quot;&lt;%# FillCountries %&gt;&quot;</span> DataTextField=<span class="st0">&quot;Description&quot;</span></div>
</li>
<li class="li1">
<div class="de1">DataValueField=<span class="st0">&quot;Description&quot;</span> &gt;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
<p>See the data source property of the DropDownList, we are assigning it with the return data set from the FillCountries method. As we set the DataSource property, DropDownList automatically,   fetches and fills the countries details as list items in the DropDownList control.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/how-to-popluate-the-dropdownlist-inside-the-datagrid.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>How to Popluate a DropDownList Controls with XML files</title>
		<link>http://www.highonmicrosoft.com/how-to-popluate-a-dropdownlist-controls-with-xml-files.html</link>
		<comments>http://www.highonmicrosoft.com/how-to-popluate-a-dropdownlist-controls-with-xml-files.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 08:53:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[asp page]]></category>

		<category><![CDATA[asp.net foreach]]></category>

		<category><![CDATA[c# foreach]]></category>

		<category><![CDATA[c#.net foreach]]></category>

		<category><![CDATA[Countries.xml]]></category>

		<category><![CDATA[custom markup language]]></category>

		<category><![CDATA[doc.load]]></category>

		<category><![CDATA[DropDownlist]]></category>

		<category><![CDATA[DropDownlist bind]]></category>

		<category><![CDATA[eXtended Markup Language]]></category>

		<category><![CDATA[foreach]]></category>

		<category><![CDATA[How to Popluate a DropDownList Controls with XML files]]></category>

		<category><![CDATA[InnerText]]></category>

		<category><![CDATA[Items.Add]]></category>

		<category><![CDATA[ListItem]]></category>

		<category><![CDATA[Load]]></category>

		<category><![CDATA[MapPath]]></category>

		<category><![CDATA[page]]></category>

		<category><![CDATA[Popluate DropDownList]]></category>

		<category><![CDATA[Popluate DropDownList control]]></category>

		<category><![CDATA[Popluate DropDownList with xml]]></category>

		<category><![CDATA[Popluate DropDownList with xml files]]></category>

		<category><![CDATA[SelectNodes]]></category>

		<category><![CDATA[SelectSingleNode]]></category>

		<category><![CDATA[System.XML]]></category>

		<category><![CDATA[System.XML namespace]]></category>

		<category><![CDATA[xml]]></category>

		<category><![CDATA[Xml Data Source control]]></category>

		<category><![CDATA[XML datasource control]]></category>

		<category><![CDATA[XmlDatasource]]></category>

		<category><![CDATA[XmlDatasource bind]]></category>

		<category><![CDATA[XmlDocument]]></category>

		<category><![CDATA[XmlNodeList]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2645</guid>
		<description><![CDATA[

Introduction
Using the Xml Data Source control
Using System.XML namespace methods



Introduction
eXtended Markup Language(XML) is a specification for create custom markup languages. In simple, xml allows you to define our own tags to represent the data in what ever way we want. Below is the simple XML file,
Countries.xml



&#60;?xml version=&#34;1.0&#34; encoding=&#34;utf-8&#34; ?&#62;


&#60;details&#62;


&#60;countries&#62;


&#60;countryname&#62;USA&#60;/countryname&#62;


&#60;/countries&#62;


&#60;countries&#62;


&#60;countryname&#62;China&#60;/countryname&#62;


&#60;/countries&#62;


&#60;/details&#62;


&#8230;&#8230;&#8230;


&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;



The xml file contains countries information “country name” [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-a-dropdownlist-controls-with-xml-files.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-a-dropdownlist-controls-with-xml-files.html#toc-using-the-xml-data-source-control"><strong>Using the Xml Data Source control</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/how-to-popluate-a-dropdownlist-controls-with-xml-files.html#toc-using-systemxml-namespace-methods"><strong>Using System.XML namespace methods</strong></a></li>
</ol>
</div>
<p><span id="more-2645"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>eXtended Markup Language(XML) is a specification for create custom markup languages. In simple, xml allows you to define our own tags to represent the data in what ever way we want. Below is the simple XML file,<br />
<strong>Countries.xml</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;utf-8&quot;</span> <span class="re2">?&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;details<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countryname<span class="re2">&gt;</span></span></span>USA<span class="sc3"><span class="re1">&lt;/countryname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;countryname<span class="re2">&gt;</span></span></span>China<span class="sc3"><span class="re1">&lt;/countryname<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/countries<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/details<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2">&#8230;&#8230;&#8230;</div>
</li>
<li class="li1">
<div class="de1">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</div>
</li>
</ol>
</div>
<p>The xml file contains countries information “country name” within the  tag. We use this xml file thru out this tutorial. Populating the DropDownList control using xml files can be done in different ways in this tutorial, I will try explain the following ways populate it:</p>
<ul>
<li>Using the <strong>Xml Data Source</strong> control</li>
<li>Using<strong> System.XML namespace</strong> methods</li>
</ul>
<h2 id="toc-using-the-xml-data-source-control"><strong>Using the Xml Data Source control</strong></h2>
<p>This is very simple, create a DropDownlist and XmlDatasource controls and just bind them. I will try to give the detailed steps of how to do this:</p>
<ul>
<li> Drop a DropDownlist control onto your ASP page.</li>
<li> Drop an XML datasource control onto your ASP page.</li>
<li> Copy the above countries.xml file into your project (preferably into your app_data folder).</li>
<li> Click on the XML datasource smart tag (the small arrow) and wire-up the datasource to your XML File <strong>Note</strong>: You have to do a refresh on your project files by Right Clicking on your folder or Root Folder in the solution explorer followed by refresh before the XML file will been seen by the XML datasource.</li>
<li> Bind the DropDownlist control datasource to your XML datasource</li>
<li> Enjoy, we have done our job!!</li>
</ul>
<h2 id="toc-using-systemxml-namespace-methods"><strong>Using System.XML namespace methods</strong></h2>
<p>I like doing the things programmatically, now, the question is how to populate the DropDownList control from the xml file programmatically. There is name space called System.XML which helps us to parse and work with the XML files. Now, first see the example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">XmlDocument doc = <span class="kw2">new</span> XmlDocument<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">doc.<span class="me1">Load</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span><span class="st0">&quot;countries.xml&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">XmlNodeList nodeList = doc.<span class="me1">SelectNodes</span><span class="br0">&#40;</span><span class="st0">&quot;details/countries&quot;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">foreach<span class="br0">&#40;</span>XmlNode node <span class="kw1">in</span> nodeList<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">DropDownList1.<span class="me1">Items</span>.<span class="me1">Add</span><span class="br0">&#40;</span><span class="kw2">new</span> ListItem<span class="br0">&#40;</span>node.<span class="me1">SelectSingleNode</span><span class="br0">&#40;</span><span class="st0">&quot;countryname&quot;</span><span class="br0">&#41;</span>.<span class="me1">InnerText</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The above code snippet is easy to understand. First we are creating a Xmldocument instance and loaded it with the Countries.xml file. Now, we want to parse the XML file and get the “country names”, to do this, we need nodelist. The for each loop iterates thru the nodelist’s and populates the DropDownList control by adding them as items to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/how-to-popluate-a-dropdownlist-controls-with-xml-files.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Binding XML and DataList Control to Create Menus</title>
		<link>http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html</link>
		<comments>http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 08:26:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[asp HyperLink]]></category>

		<category><![CDATA[asp.net namespaces]]></category>

		<category><![CDATA[Binding datalist]]></category>

		<category><![CDATA[binding datarepeator]]></category>

		<category><![CDATA[Binding dataset]]></category>

		<category><![CDATA[Binding menus]]></category>

		<category><![CDATA[binding xml]]></category>

		<category><![CDATA[c# namespaces]]></category>

		<category><![CDATA[c#.net namespaces]]></category>

		<category><![CDATA[cache]]></category>

		<category><![CDATA[Cache.Insert]]></category>

		<category><![CDATA[Container.DataItem]]></category>

		<category><![CDATA[data list control]]></category>

		<category><![CDATA[data list hyper control]]></category>

		<category><![CDATA[data sources]]></category>

		<category><![CDATA[DataBind]]></category>

		<category><![CDATA[DataBinder]]></category>

		<category><![CDATA[DataBinder.Eval]]></category>

		<category><![CDATA[DataList]]></category>

		<category><![CDATA[datalist cache]]></category>

		<category><![CDATA[datalist control]]></category>

		<category><![CDATA[datalist hyper control]]></category>

		<category><![CDATA[datalist menulist]]></category>

		<category><![CDATA[dataset]]></category>

		<category><![CDATA[dataset Cache]]></category>

		<category><![CDATA[DataSource]]></category>

		<category><![CDATA[Debug]]></category>

		<category><![CDATA[Encoding]]></category>

		<category><![CDATA[EventArgs]]></category>

		<category><![CDATA[FooterTemplate]]></category>

		<category><![CDATA[HeaderTemplate]]></category>

		<category><![CDATA[html]]></category>

		<category><![CDATA[html repeater]]></category>

		<category><![CDATA[hyper control]]></category>

		<category><![CDATA[hyperlink]]></category>

		<category><![CDATA[Import Namespace]]></category>

		<category><![CDATA[Import Namespaces]]></category>

		<category><![CDATA[ItemTemplate]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[MapPath]]></category>

		<category><![CDATA[Menu tag]]></category>

		<category><![CDATA[MenuItem]]></category>

		<category><![CDATA[menulist]]></category>

		<category><![CDATA[MENU_CACHE_KEY]]></category>

		<category><![CDATA[namespaces]]></category>

		<category><![CDATA[page]]></category>

		<category><![CDATA[Page_Load]]></category>

		<category><![CDATA[Page_Load event]]></category>

		<category><![CDATA[ReadXml]]></category>

		<category><![CDATA[repeater]]></category>

		<category><![CDATA[Repeater control]]></category>

		<category><![CDATA[runat]]></category>

		<category><![CDATA[sitemap]]></category>

		<category><![CDATA[Sitemap data sources]]></category>

		<category><![CDATA[system]]></category>

		<category><![CDATA[System.Data]]></category>

		<category><![CDATA[System.Web]]></category>

		<category><![CDATA[System.Web.Caching]]></category>

		<category><![CDATA[System.Web.Caching.CacheDependency]]></category>

		<category><![CDATA[url]]></category>

		<category><![CDATA[Using namespaces]]></category>

		<category><![CDATA[utf-8]]></category>

		<category><![CDATA[vb.net namespaces]]></category>

		<category><![CDATA[xml cache]]></category>

		<category><![CDATA[xml data sources]]></category>

		<category><![CDATA[xml example]]></category>

		<category><![CDATA[XML Menu]]></category>

		<category><![CDATA[XML Menu file]]></category>

		<category><![CDATA[xml menus]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2638</guid>
		<description><![CDATA[

Introduction
XML Menu file
Using NameSpaces
Creating the DataSet and Caching it
Create the data list control
Binding the DataSet to the Data List Control
Using the Data List’s Hyper Control to Display the Menu 



Introduction
Menus are used to navigate thru the web sites. There are many ways to create the menus, like, using menu control with XML or Sitemap data [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-xml-menu-file"><strong>XML Menu file</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-using-namespaces"><strong>Using NameSpaces</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-creating-the-dataset-and-caching-it"><strong>Creating the DataSet and Caching it</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-create-the-data-list-control"><strong>Create the data list control</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-binding-the-dataset-to-the-data-list-control"><strong>Binding the DataSet to the Data List Control</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html#toc-using-the-data-lists-hyper-control-to-display-the-menu"><strong>Using the Data List’s Hyper Control to Display the Menu </strong></a></li>
</ol>
</div>
<p><span id="more-2638"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>Menus are used to navigate thru the web sites. There are many ways to create the menus, like, using menu control with XML or Sitemap data sources etc. Besides these all, there is another control called as, data list control. The DataList control is like the Repeater control, used to display a repeated list of items that are bound to the control. However, the DataList control adds a table around the data items by default. The DataList control may be bound to a database table, an XML file, or another list of items. In this tutorial we bind the XML Menu file with data list control.</p>
<p>Behind binding and displaying the xml menu, we will create dataset and cache them for future use, so, that it increases speed. Keeping all these things in mind I divided the tutorial into following sections:</p>
<ul>
<li>XML menu file</li>
<li>Using namespaces</li>
<li>Creating the DataSet and Caching it</li>
<li>Create the Data List Control</li>
<li>Binding the dataset to the Data List Control</li>
<li>Using the Data List’s Hyper Control to Display the Menu</li>
</ul>
<h2 id="toc-xml-menu-file"><strong>XML Menu file</strong></h2>
<p>Below is the sample xml file which contains MenuItem tag and it has other two sub tags in it, called as text and URL. Menu tag is used to identify the individual menu and it sub tags, text and URL is used for menu text and menu URL respectively. See the sample xml file as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;?xml</span> <span class="re0">version</span>=<span class="st0">&quot;1.0&quot;</span> <span class="re0">encoding</span>=<span class="st0">&quot;utf-8&quot;</span> <span class="re2">?&gt;</span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Menu<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;MenuItem<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;Text<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">Add User</div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/Text<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Url<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">http://azamsharp.net/Admin/AddUser.aspx</div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/Url<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/MenuItem<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;MenuItem<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Text<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">Add Location</div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/Text<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;Url<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">http://azamsharp.net/Admin/AddLocation.aspx</div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/Url<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1"><span class="sc3"><span class="re1">&lt;/MenuItem<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li2">
<div class="de2"><span class="sc3"><span class="re1">&lt;/Menu<span class="re2">&gt;</span></span></span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p></p>
<h2 id="toc-using-namespaces"><strong>Using NameSpaces</strong></h2>
<p>In this tutorial we are using the dataset to retrieve the xml content and later we are binding the datasets with the data list. So to use the dataset, first we need to import the System.Data namespace as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System.Data&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
</ol>
</div>
<p><strong>System.Data namespace</strong> has a class called <strong>Dataset</strong>, which we are using in this tutorial.<br />
</p>
<h2 id="toc-creating-the-dataset-and-caching-it"><strong>Creating the DataSet and Caching it</strong></h2>
<p>First we create a dataset and load the XML file to it as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Page Language=<span class="st0">&quot;C#&quot;</span> Debug=<span class="st0">&quot;true&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System.Data&quot;</span><span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&lt;script runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">private</span> const <span class="kw1">string</span> XML_MENU_FILE = <span class="st0">&quot;MenuAdmin.xml&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">void Page_Load<span class="br0">&#40;</span>Object sender, EventArgs e<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">DataSet dsMenu = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">dsMenu.<span class="me1">ReadXml</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span>XML_MENU_FILE<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Menus are displayed every time when the user views the page. In the page_load function we creating dataset and reading the same file always. This increase the load time and we are unnecessary reading the xml file always which is an expensive operation. So, the better idea is to load file once and use it multiple times. This can be achieved  by using the caches as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Page Language=<span class="st0">&quot;C#&quot;</span> Debug=<span class="st0">&quot;true&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System.Data&quot;</span><span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&lt;script runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">private</span> const <span class="kw1">string</span> XML_MENU_FILE_PATH = <span class="st0">&quot;MenuAdmin.xml&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">private</span> const <span class="kw1">string</span> MENU_CACHE_KEY = <span class="st0">&quot;AdminMenu&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">void Page_Load<span class="br0">&#40;</span>Object sender, EventArgs e<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">DataSet dsMenu = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span>Cache<span class="br0">&#91;</span>MENU_CACHE_KEY<span class="br0">&#93;</span> != <span class="kw2">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">dsMenu = <span class="br0">&#40;</span>DataSet<span class="br0">&#41;</span> Cache<span class="br0">&#91;</span>MENU_CACHE_KEY<span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">dsMenu.<span class="me1">ReadXml</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span>XML_MENU_FILE_PATH<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">Cache.<span class="me1">Insert</span><span class="br0">&#40;</span>MENU_CACHE_KEY, dsMenu, <span class="kw2">new</span> System.<span class="me1">Web</span>.<span class="me1">Caching</span>.<span class="me1">CacheDependency</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span>MENU_FILE<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>See the above code, we modified it a little bit compared to it previous version. In this, first we are checking whether there is menu data set in cache array (with MENU_CACHE_KEY key), if it exists we are casting the Menu dataset from the cache array to our dataset “dsMenu”.</p>
<p>If the cache array does not have menu dataset key, we are loading the xml file into dataset “dsmenu” and inserting the dataset into the cache array for future use.</p>
<h2 id="toc-create-the-data-list-control"><strong>Create the data list control</strong></h2>
<p>We create a DataList in an .aspx page. See the below data list control, the contents are as follows:</p>
<ul>
<li>Data list takes the parameters id, which is used to identify the data list uniquely from the other controls and runat=”server” specifies that data list executes on the server.</li>
<li>&lt;HeaderTemplate&gt; element are rendered first and only once within the output</li>
<li>&lt;ItemTemplate&gt; element are repeated for each &#8220;record&#8221; in the DataSet,</li>
<li>&lt;FooterTemplate&gt; element are rendered once within the output</li>
</ul>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;html&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;body&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;form runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:DataList id=<span class="st0">&quot;menulist&quot;</span> runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li2">
<div class="de2">&lt;HeaderTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&#8230;</div>
</li>
<li class="li1">
<div class="de1">&lt;/HeaderTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;ItemTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&#8230;</div>
</li>
<li class="li2">
<div class="de2">&lt;/ItemTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;FooterTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&#8230;</div>
</li>
<li class="li1">
<div class="de1">&lt;/FooterTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/asp:DataList&gt;</div>
</li>
<li class="li2">
<div class="de2">&lt;/form&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
<p>We can add styles to the data list as parameters as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;asp:DataList id=<span class="st0">&quot;menulist&quot;</span></div>
</li>
<li class="li1">
<div class="de1">runat=<span class="st0">&quot;server&quot;</span></div>
</li>
<li class="li1">
<div class="de1">……………..</div>
</li>
<li class="li1">
<div class="de1">………………</div>
</li>
<li class="li2">
<div class="de2">………………</div>
</li>
<li class="li1">
<div class="de1">headerstyle-font-name=<span class="st0">&quot;Verdana&quot;</span></div>
</li>
<li class="li1">
<div class="de1">headerstyle-font-size=<span class="st0">&quot;12pt&quot;</span></div>
</li>
<li class="li1">
<div class="de1">headerstyle-horizontalalign=<span class="st0">&quot;center&quot;</span></div>
</li>
<li class="li1">
<div class="de1">headerstyle-font-bold=<span class="st0">&quot;True&quot;</span></div>
</li>
<li class="li2">
<div class="de2">itemstyle-backcolor=<span class="st0">&quot;#778899&quot;</span></div>
</li>
<li class="li1">
<div class="de1">itemstyle-forecolor=<span class="st0">&quot;#ffffff&quot;</span></div>
</li>
<li class="li1">
<div class="de1">footerstyle-font-size=<span class="st0">&quot;9pt&quot;</span></div>
</li>
<li class="li1">
<div class="de1">footerstyle-font-italic=<span class="st0">&quot;True&quot;</span>&gt;</div>
</li>
</ol>
</div>
<h2 id="toc-binding-the-dataset-to-the-data-list-control"><strong>Binding the DataSet to the Data List Control</strong></h2>
<p>We created the data list, data set and loaded the data set with xml, now, the other important step remain is, binding the data set with the data list control. This is done as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Page Language=<span class="st0">&quot;C#&quot;</span> Debug=<span class="st0">&quot;true&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System&quot;</span> <span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;%</span>@ Import Namespace=<span class="st0">&quot;System.Data&quot;</span><span class="kw2">%&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&lt;script runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">private</span> const <span class="kw1">string</span> XML_MENU_FILE_PATH = <span class="st0">&quot;MenuAdmin.xml&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">private</span> const <span class="kw1">string</span> MENU_CACHE_KEY = <span class="st0">&quot;AdminMenu&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">void Page_Load<span class="br0">&#40;</span>Object sender, EventArgs e<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">DataSet dsMenu = <span class="kw2">new</span> DataSet<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">if</span><span class="br0">&#40;</span>Cache<span class="br0">&#91;</span>MENU_CACHE_KEY<span class="br0">&#93;</span> != <span class="kw2">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">dsMenu = <span class="br0">&#40;</span>DataSet<span class="br0">&#41;</span> Cache<span class="br0">&#91;</span>MENU_CACHE_KEY<span class="br0">&#93;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">dsMenu.<span class="me1">ReadXml</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span>XML_MENU_FILE_PATH<span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">Cache.<span class="me1">Insert</span><span class="br0">&#40;</span>MENU_CACHE_KEY, dsMenu, <span class="kw2">new</span> System.<span class="me1">Web</span>.<span class="me1">Caching</span>.<span class="me1">CacheDependency</span><span class="br0">&#40;</span><span class="kw1">Server</span>.<span class="kw3">MapPath</span><span class="br0">&#40;</span>MENU_FILE<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">menulist.<span class="me1">DataSource</span>=dsMenu;</div>
</li>
<li class="li1">
<div class="de1">menulist.<span class="me1">DataBind</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p><strong>DataBind()</strong> method binds the data source (which in our case is a xml data set) with the data list.</p>
<h2 id="toc-using-the-data-lists-hyper-control-to-display-the-menu"><strong>Using the Data List’s Hyper Control to Display the Menu </strong></h2>
<p>Now, the final part comes into picture, try to remember the data list control contents, there is  element, which is repeated for each &#8220;record&#8221; in the DataSet. This is what we want to show the menu contents, Right!,</p>
<p>Menus are links, when the user clicks on it, we are moved to destined URL. So, for the purpose of this tutorial, we use Hyperlink control. It has text and NavigateUrl properties; we will set value of these properties from the xml data source as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&lt;html&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;body&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;form runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:DataList id=<span class="st0">&quot;menulist&quot;</span> runat=<span class="st0">&quot;server&quot;</span>&gt;</div>
</li>
<li class="li2">
<div class="de2">&lt;ItemTemplate&gt;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;asp:HyperLink Text = <span class="co1">&#8216;&lt;%# DataBinder.Eval(Container.DataItem,&quot;Text&quot;) %&gt;&#8217; NavigateUrl = &#8216;&lt;%# DataBinder.Eval(Container.DataItem,&quot;Url&quot;) %&gt;&#8217; id=&quot;hlLinks&quot; runat=&quot;server&quot;&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/ItemTemplate&gt;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&lt;/asp:DataList&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/form&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/body&gt;</div>
</li>
<li class="li1">
<div class="de1">&lt;/html&gt;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/binding-xml-and-datalist-control-to-create-menus.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Counting Number of Users on the Web Site - ASP.net,VB.net,C#.net</title>
		<link>http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html</link>
		<comments>http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 06:17:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[.net Application variables]]></category>

		<category><![CDATA[.net global.asax]]></category>

		<category><![CDATA[.net session_start]]></category>

		<category><![CDATA[.net user count]]></category>

		<category><![CDATA[Application variables]]></category>

		<category><![CDATA[asax]]></category>

		<category><![CDATA[asax file]]></category>

		<category><![CDATA[asp session_start]]></category>

		<category><![CDATA[asp user count]]></category>

		<category><![CDATA[asp Using the text files]]></category>

		<category><![CDATA[asp.net Application variables]]></category>

		<category><![CDATA[asp.net session_start]]></category>

		<category><![CDATA[asp.net text files]]></category>

		<category><![CDATA[asp.net user count]]></category>

		<category><![CDATA[asp.net Using the text files]]></category>

		<category><![CDATA[Bitmap]]></category>

		<category><![CDATA[bitmap Dispose]]></category>

		<category><![CDATA[c# Application variables]]></category>

		<category><![CDATA[c# session_start]]></category>

		<category><![CDATA[c# user count]]></category>

		<category><![CDATA[c# Using the text files]]></category>

		<category><![CDATA[c#.net Application variables]]></category>

		<category><![CDATA[c#.net session_start]]></category>

		<category><![CDATA[c#.net user count]]></category>

		<category><![CDATA[c#.net Using the text files]]></category>

		<category><![CDATA[Cint]]></category>

		<category><![CDATA[close]]></category>

		<category><![CDATA[CreateText]]></category>

		<category><![CDATA[display number of user on the web sites]]></category>

		<category><![CDATA[display number of users]]></category>

		<category><![CDATA[Dispose]]></category>

		<category><![CDATA[DrawRectangle]]></category>

		<category><![CDATA[DrawString]]></category>

		<category><![CDATA[File]]></category>

		<category><![CDATA[File.CreateText]]></category>

		<category><![CDATA[File.Exists]]></category>

		<category><![CDATA[File.OpenText]]></category>

		<category><![CDATA[Font]]></category>

		<category><![CDATA[FromImage]]></category>

		<category><![CDATA[Gif]]></category>

		<category><![CDATA[global.asax]]></category>

		<category><![CDATA[ImageFormat]]></category>

		<category><![CDATA[jpg]]></category>

		<category><![CDATA[new pen]]></category>

		<category><![CDATA[OutputStream]]></category>

		<category><![CDATA[pen]]></category>

		<category><![CDATA[PointF]]></category>

		<category><![CDATA[ReadToEnd]]></category>

		<category><![CDATA[Rectangle]]></category>

		<category><![CDATA[session_start]]></category>

		<category><![CDATA[session_start function]]></category>

		<category><![CDATA[SolidBrush]]></category>

		<category><![CDATA[System.Drawing]]></category>

		<category><![CDATA[System.Drawing.Imaging]]></category>

		<category><![CDATA[System.Drawing.Imaging.PixelFormat]]></category>

		<category><![CDATA[System.Drawing.Imaging.PixelFormat.Format32bppArgb]]></category>

		<category><![CDATA[to track the number of users]]></category>

		<category><![CDATA[user count]]></category>

		<category><![CDATA[user count on web site]]></category>

		<category><![CDATA[Using the text files]]></category>

		<category><![CDATA[vb session_start]]></category>

		<category><![CDATA[vb user count]]></category>

		<category><![CDATA[vb.net Application variables]]></category>

		<category><![CDATA[vb.net session_start]]></category>

		<category><![CDATA[vb.net user count]]></category>

		<category><![CDATA[vb.net Using the text files]]></category>

		<category><![CDATA[website staticis]]></category>

		<category><![CDATA[website user count]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2629</guid>
		<description><![CDATA[

Introduction
Using the Application variables (Global.asax )
Using the text files (Not lose User Count Between Application Restarts)
Displaying the user count as a colorful image on the web sites



Introduction
Most of the web sites today using the hit count either to track the number of users (or) to display on the web sites to increase the popularity. There [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html#toc-using-the-application-variables-globalasax"><strong>Using the Application variables (Global.asax )</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html#toc-using-the-text-files-not-lose-user-count-between-application-restarts"><strong>Using the text files (Not lose User Count Between Application Restarts)</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html#toc-displaying-the-user-count-as-a-colorful-image-on-the-web-sites"><strong>Displaying the user count as a colorful image on the web sites</strong></a></li>
</ol>
</div>
<p><span id="more-2629"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>Most of the web sites today using the hit count either to track the number of users (or) to display on the web sites to increase the popularity. There are many ways and each have pros and cons. In this article, I will try to show the different ways of counting and showing the number of users as follows:</p>
<ul>
<li>Using the Application variables(Global.asax) </li>
<li>Using the text files</li>
<li>Displaying the user count as a colorful image on the web sites</li>
</ul>
<h2 id="toc-using-the-application-variables-globalasax"><strong>Using the Application variables (Global.asax )</strong></h2>
<p>Most of programmers, who think about counting number of users on the web site, first thing, that strikes in mind, are using the application variables to hold the user count. Because, it is simple to implement and can be accessed application wide. Let first see how to do this:</p>
<p>First open the <strong>global.asax</strong> file and go to <strong>session_start()</strong> function. Now add the following code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">protected void Session_Start<span class="br0">&#40;</span>Object sender, EventArgs e<span class="br0">&#41;</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Application</span><span class="br0">&#91;</span>TOTALNUMBEROFUSERS<span class="br0">&#93;</span> = Convert.<span class="me1">ToInt32</span><span class="br0">&#40;</span><span class="kw1">Application</span><span class="br0">&#91;</span>TOTALNUMBEROFUSERS<span class="br0">&#93;</span><span class="br0">&#41;</span> + <span class="nu0">1</span>; </div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Use the application state variable <strong>TOTALNUMBEROFUSERS </strong>to access the count and display it to the users in your asp pages. This seems to be simple but, this approach has a major drawback, like, if the application restarts, the count will be lost because, the application variables are initialized again when the application starts.</p>
<h2 id="toc-using-the-text-files-not-lose-user-count-between-application-restarts"><strong>Using the text files (Not lose User Count Between Application Restarts)</strong></h2>
<p>As discussed, storing user count on application variables is not a good option. We need some way of storing the user count, so even if the application restarts, we can access user count. The simple solution is using the text files to store the user count as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Try</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">If</span> <span class="kw1">not</span> File.<span class="me1">Exists</span><span class="br0">&#40;</span>sFile<span class="br0">&#41;</span> <span class="kw1">then</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; objWriter = File.<span class="me1">CreateText</span><span class="br0">&#40;</span>“<span class="kw1">count</span>.<span class="me1">txt</span>”<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; objWriter.<span class="kw1">Write</span><span class="br0">&#40;</span><span class="st0">&quot;0&quot;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; objWriter.<span class="kw1">Close</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">End</span> <span class="kw1">if</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">objReader = File.<span class="me1">OpenText</span><span class="br0">&#40;</span>sFile<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">sCount = objReader.<span class="me1">ReadToEnd</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">objReader.<span class="kw1">Close</span></div>
</li>
<li class="li1">
<div class="de1">iCount = <span class="kw1">Cint</span><span class="br0">&#40;</span>sCount<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">iCount = iCount + <span class="nu0">1</span></div>
</li>
<li class="li1">
<div class="de1">sCount = iCount.<span class="me1">ToString</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">objWriter = File.<span class="me1">CreateText</span><span class="br0">&#40;</span>sFile<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">objWriter.<span class="kw1">Write</span><span class="br0">&#40;</span>sCount<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">objWriter.<span class="kw1">Close</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">Catch Ex <span class="kw1">as</span> Exception</div>
</li>
<li class="li1">
<div class="de1">Label1.<span class="kw1">width</span> = <span class="kw1">New</span> Unit<span class="br0">&#40;</span><span class="nu0">640</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">sCount = <span class="st0">&quot;[Count could not be obtained due to the following error]: &quot;</span> &amp; &nbsp;Ex.<span class="me1">Message</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Finally</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">HitCountLabel.<span class="me1">Text</span> = “The total number of hits: ” &amp; sCount</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">End</span> Try</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Put the above code in the <strong>page_load</strong> function, so when the page is loaded the function executes each time and updates the user count.</p>
<h2 id="toc-displaying-the-user-count-as-a-colorful-image-on-the-web-sites"><strong>Displaying the user count as a colorful image on the web sites</strong></h2>
<p>The above section describes how to save and retrieve the hit counter from the text file. By using the same hit counter, I will try to show to how to create a image with the number of users as follows:<br />
<strong>[Continuation of the above example]</strong></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> bitmap <span class="kw1">as</span> <span class="kw1">new</span> Bitmap<span class="br0">&#40;</span><span class="nu0">200</span>,<span class="nu0">150</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.<span class="me1">Drawing</span>.<span class="me1">Imaging</span>.<span class="me1">PixelFormat</span>.<span class="me1">Format32bppArgb</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">Dim</span> &nbsp;g <span class="kw1">as</span> Graphics= Graphics.<span class="me1">FromImage</span><span class="br0">&#40;</span>bitmap<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> pen <span class="kw1">as</span> Pen <span class="kw1">new</span> Pen<span class="br0">&#40;</span>Color.<span class="me1">Yellow</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> rect <span class="kw1">as</span> Rectangle <span class="kw1">new</span> Rectangle<span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">0</span>,<span class="nu0">200</span>,<span class="nu0">150</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> b <span class="kw1">as</span> SolidBrush <span class="kw1">new</span> SolidBrush<span class="br0">&#40;</span>Color.<span class="me1">DarkKhaki</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> blue <span class="kw1">as</span> SolidBrush <span class="kw1">new</span> SolidBrush<span class="br0">&#40;</span>Color.<span class="me1">Blue</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">Dim</span> counter <span class="kw1">as</span> <span class="kw1">integer</span> </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> i <span class="kw1">as</span> <span class="kw1">integer</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">counter =<span class="nu0">0</span></div>
</li>
<li class="li1">
<div class="de1">i=<span class="nu0">0</span></div>
</li>
<li class="li2">
<div class="de2">g.<span class="me1">DrawRectangle</span><span class="br0">&#40;</span>pen, rect<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">g.<span class="me1">FillRectangle</span><span class="br0">&#40;</span>b, rect<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> &nbsp;i = <span class="nu0">0</span> <span class="kw1">to</span> sCount.<span class="me1">Length</span> - <span class="nu0">1</span> </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; g.<span class="me1">DrawString</span><span class="br0">&#40;</span>sCount<span class="br0">&#91;</span>i<span class="br0">&#93;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">new</span> Font<span class="br0">&#40;</span><span class="st0">&quot;Verdena&quot;</span>, <span class="nu0">10</span> + rand.<span class="kw1">Next</span><span class="br0">&#40;</span><span class="nu0">14</span>, <span class="nu0">18</span><span class="br0">&#41;</span><span class="br0">&#41;</span>, </div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; blue, <span class="kw1">new</span> PointF<span class="br0">&#40;</span><span class="nu0">10</span> + counter, <span class="nu0">10</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">next</span> i</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Response.<span class="me1">ContentType</span> = <span class="st0">&quot;image/gif&quot;</span></div>
</li>
<li class="li1">
<div class="de1">bitmap.<span class="me1">Save</span><span class="br0">&#40;</span>Response.<span class="me1">OutputStream</span>,ImageFormat.<span class="me1">Gif</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">g.<span class="me1">Dispose</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">bitmap.<span class="me1">Dispose</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/counting-number-of-users-on-the-web-site-asp.netvb.netc.net.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Different Ways of Generating Random Passwords in Asp.net (We Can Use Same for Creating Captchas)</title>
		<link>http://www.highonmicrosoft.com/different-ways-of-generating-random-passwords-in-asp.net-we-can-use-same-for-creating-captchas.html</link>
		<comments>http://www.highonmicrosoft.com/different-ways-of-generating-random-passwords-in-asp.net-we-can-use-same-for-creating-captchas.html#comments</comments>
		<pubDate>Sun, 05 Jul 2009 05:35:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Asp.net]]></category>

		<category><![CDATA[tutorials]]></category>

		<category><![CDATA[.net tutorials]]></category>

		<category><![CDATA[32 bit]]></category>

		<category><![CDATA[alphabet password]]></category>

		<category><![CDATA[Append function]]></category>

		<category><![CDATA[Append method]]></category>

		<category><![CDATA[ArgumentException]]></category>

		<category><![CDATA[asp generate password]]></category>

		<category><![CDATA[asp password]]></category>

		<category><![CDATA[asp StringBuilder]]></category>

		<category><![CDATA[asp.net Append]]></category>

		<category><![CDATA[asp.net generate password]]></category>

		<category><![CDATA[asp.net password]]></category>

		<category><![CDATA[asp.net string Replace]]></category>

		<category><![CDATA[asp.net StringBuilder]]></category>

		<category><![CDATA[asp.net Substring]]></category>

		<category><![CDATA[asp.net tutorials]]></category>

		<category><![CDATA[c# Append]]></category>

		<category><![CDATA[c# generate password]]></category>

		<category><![CDATA[c# password]]></category>

		<category><![CDATA[c# string Replace]]></category>

		<category><![CDATA[c# StringBuilder]]></category>

		<category><![CDATA[c# Substring]]></category>

		<category><![CDATA[c#.net]]></category>

		<category><![CDATA[c#.net Append]]></category>

		<category><![CDATA[c#.net articles]]></category>

		<category><![CDATA[c#.net password]]></category>

		<category><![CDATA[c#.net StringBuilder]]></category>

		<category><![CDATA[c#.net tutorials]]></category>

		<category><![CDATA[CLSID]]></category>

		<category><![CDATA[generate password]]></category>

		<category><![CDATA[GenerateRandomPasswordUsingGUID]]></category>

		<category><![CDATA[GUID]]></category>

		<category><![CDATA[hexadecimal]]></category>

		<category><![CDATA[hexadecimal string]]></category>

		<category><![CDATA[NewGuid function]]></category>

		<category><![CDATA[NewGuid method]]></category>

		<category><![CDATA[password]]></category>

		<category><![CDATA[random passwords]]></category>

		<category><![CDATA[replace]]></category>

		<category><![CDATA[Replace function]]></category>

		<category><![CDATA[Replace method]]></category>

		<category><![CDATA[Response.Write]]></category>

		<category><![CDATA[StringBuilder]]></category>

		<category><![CDATA[substring]]></category>

		<category><![CDATA[Substring function]]></category>

		<category><![CDATA[Substring method]]></category>

		<category><![CDATA[System.Guid]]></category>

		<category><![CDATA[System.Guid structure]]></category>

		<category><![CDATA[System.Guid.NewGuid]]></category>

		<category><![CDATA[vb.net StringBuilder]]></category>

		<guid isPermaLink="false">http://www.highonmicrosoft.com/?p=2621</guid>
		<description><![CDATA[

Introduction
Using the NewGuid function
Generate the random password in our own way



Introduction
How to generate random passwords, I came across this question many times in forums. Recently again I came across this question, so I decided to write this article. This may help the required ones. Actually it is a simple task; usually there are two best [...]]]></description>
			<content:encoded><![CDATA[<div class="toc">
<ol>
<li><a href="http://www.highonmicrosoft.com/different-ways-of-generating-random-passwords-in-asp.net-we-can-use-same-for-creating-captchas.html#toc-introduction"><strong>Introduction</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/different-ways-of-generating-random-passwords-in-asp.net-we-can-use-same-for-creating-captchas.html#toc-using-the-newguid-function"><strong>Using the NewGuid function</strong></a></li>
<li><a href="http://www.highonmicrosoft.com/different-ways-of-generating-random-passwords-in-asp.net-we-can-use-same-for-creating-captchas.html#toc-generate-the-random-password-in-our-own-way"><strong>Generate the random password in our own way</strong></a></li>
</ol>
</div>
<p><span id="more-2621"></span></p>
<h2 id="toc-introduction"><strong>Introduction</strong></h2>
<p>How to generate random passwords, I came across this question many times in forums. Recently again I came across this question, so I decided to write this article. This may help the required ones. Actually it is a simple task; usually there are two best or common methods to generate the random passwords as follows:</p>
<ul>
<li>Using the NewGuid function</li>
<li>Generate the random password in our own way</li>
</ul>
<h2 id="toc-using-the-newguid-function"><strong>Using the NewGuid function</strong></h2>
<p>NewGuid method is a member of the System.Guid structure, this generates a 32 bit hexadecimal string and it will globally unique. If you are familiar with the components then, you can refer this to the CLSIDs.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">&#8216; VB.NET</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Dim</span> Result <span class="kw3">as</span> <span class="kw1">String</span> = System.<span class="me1">Guid</span>.<span class="me1">NewGuid</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="co2">// C#</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">string</span> Result = System.<span class="me1">Guid</span>.<span class="me1">NewGuid</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The result will be as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">63dac3e4- 63b9<span class="nu0">-4057</span>-862b-7d28e2868389</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Now, you can say like, how to use this crazy thing as the password. Let me explain it, every time when the new GUID is generated it will be unique. So, our passwords will be unique for all the users, so I think this is some what good option, right!!  But, the problem is, the string is lengthy. </p>
<p>We will solve this problem by taking the required amount of characters from the generated GUID as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">public</span> <span class="kw1">string</span> GenerateRandomPasswordUsingGUID<span class="br0">&#40;</span><span class="kw1">int</span> length<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co2">// Get the GUID</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="kw1">string</span> Result = System.<span class="me1">Guid</span>.<span class="me1">NewGuid</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co2">// Remove the hyphens</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; Result = Result.<span class="kw3">Replace</span><span class="br0">&#40;</span><span class="st0">&quot;-&quot;</span>, <span class="kw1">string</span>.<span class="me1">Empty</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li2">
<div class="de2">&nbsp; <span class="co2">// Make sure length is valid</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>length &lt;= <span class="nu0">0</span> || length &gt; Result.<span class="me1">Length</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; throw <span class="kw2">new</span> ArgumentException<span class="br0">&#40;</span><span class="st0">&quot;Length must be between 1 and &quot;</span> + guidResult.<span class="me1">Length</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; </div>
</li>
<li class="li1">
<div class="de1">&nbsp; <span class="co2">// Return the first length bytes</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; return guidResult.<span class="me1">Substring</span><span class="br0">&#40;</span><span class="nu0">0</span>, length<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Now, if the length is 8 , the return password from this function will be as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">63dac3e4</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>It is a good and healthy password, cheers.</p>
<h2 id="toc-generate-the-random-password-in-our-own-way"><strong>Generate the random password in our own way</strong></h2>
<p>Generating passwords with GUID is simple and effective but, some times their will be other things to consider like, if our password needs only alphabets, then GUID will not be the answer. Like this kind of custom rules, we need our own function to generate passwords. There are many ways to write this function, I will show an example how to write this:</p>
<p>Condition like, includes like no numbers, but only, combination of capital and small letters as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">string</span> alphabets = <span class="st0">&quot;abcdefghijklmnopqrstuvwxyz&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">StringBuilder password = <span class="kw2">new</span> StringBuilder<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">Random r = <span class="kw2">new</span> Random<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw1">int</span> j=<span class="nu0">0</span>; j&lt;=<span class="nu0">20</span>;j++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw1">for</span><span class="br0">&#40;</span><span class="kw1">int</span> i=<span class="nu0">0</span>;i&lt;=<span class="nu0">5</span>;i++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">password.<span class="me1">Append</span><span class="br0">&#40;</span>alphabets<span class="br0">&#91;</span>r.<span class="kw1">Next</span><span class="br0">&#40;</span>alphabets.<span class="me1">Length</span><span class="br0">&#41;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">string</span> temp = alphabets<span class="br0">&#91;</span>r.<span class="kw1">Next</span><span class="br0">&#40;</span>alphabets.<span class="me1">Length</span><span class="br0">&#41;</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1">password.<span class="me1">Append</span><span class="br0">&#40;</span>temp.<span class="me1">ToUpper</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">Response</span>.<span class="kw3">Write</span><span class="br0">&#40;</span>password.<span class="me1">ToString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">password.<span class="me1">Remove</span><span class="br0">&#40;</span><span class="nu0">0</span>,password.<span class="me1">Length</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>We are using the string builder to reduce the memory usage and append the password string. In the for loop, we are taking a random alphabet from the string and appending to the password and then, we are taking the random alphabet from the string and converting it to upper case and appending to string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.highonmicrosoft.com/different-ways-of-generating-random-passwords-in-asp.net-we-can-use-same-for-creating-captchas.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
