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

<channel>
	<title>Sam Beauvois &#187; ASP.NET</title>
	<atom:link href="http://www.sambeauvois.be/blog/category/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sambeauvois.be/blog</link>
	<description>general dev, .net and other stuff</description>
	<lastBuildDate>Tue, 31 Jan 2012 13:38:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Dynamic is magic : A solution to use the ASP.NET profile in web applications.</title>
		<link>http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/</link>
		<comments>http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 13:38:32 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=926</guid>
		<description><![CDATA[Profiles are not auto generated with Web Applications, so you can’t just add them in the web.config and use them directly. (unless …) The WebSite project do a magic thing : when you add properties to the profile in the web.config file, a class is autogenerated and allow the developer to access these properties. The [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>Profiles are not auto generated with Web Applications, so you can’t just add them in the web.config and use them directly. (unless …)</p></blockquote>
<p>The WebSite project do a magic thing : when you add properties to the profile in the web.config file, a class is autogenerated and allow the developer to access these properties. The web application project don&#8217;t do that.</p>
<p>You can find some workarounds over the web, here is some</p>
<ul>
<li><a href="http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx" target="_blank">http://weblogs.asp.net/joewrobel/archive/2008/02/03/web-profile-builder-for-web-application-projects.aspx</a></li>
<li><a href="http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/" target="_blank">http://leedumond.com/blog/asp-net-profiles-in-web-application-projects/</a></li>
<li><a href="http://willmtz.blogspot.com/2011/09/using-aspnet-profile-feature-in-web.html" target="_blank">http://willmtz.blogspot.com/2011/09/using-aspnet-profile-feature-in-web.html</a></li>
<li><a href="http://stackoverflow.com/questions/426609/how-to-assign-profile-values" target="_blank">http://stackoverflow.com/questions/426609/how-to-assign-profile-values</a></li>
</ul>
<p>The &#8220;<a href="http://msdn.microsoft.com/en-us/library/dd264741.aspx" target="_blank">dynamic</a>&#8221; keyword help us here.</p>
<p>So, no more chit chat, here is the code snippets for a profile with 3 parameters : LastName, FirstName and GSM</p>
<p>web.config:</p>
<pre class="brush: xml; title: ;">

&lt;profile enabled=&quot;true&quot;&gt;
 &lt;providers&gt;
 &lt;clear/&gt;
 &lt;add name=&quot;AspNetSqlProfileProvider&quot; type=&quot;System.Web.Profile.SqlProfileProvider&quot; connectionStringName=&quot;ApplicationServices&quot; applicationName=&quot;MyApp&quot;/&gt;
 &lt;/providers&gt;
 &lt;properties&gt;
 &lt;add name=&quot;FirstName&quot; type=&quot;string&quot;/&gt;
 &lt;add name=&quot;LastName&quot; type=&quot;string&quot;/&gt;
 &lt;add name=&quot;GSM&quot; type=&quot;string&quot;/&gt;
 &lt;/properties&gt;
 &lt;/profile&gt;
</pre>
<p>A &#8220;business&#8221; class, I called it &#8220;ProfilePresenter&#8221;, it returns the profile of the logged user and allow modifications:</p>
<pre class="brush: csharp; title: ;">

public class ProfilePresenter
{
 public dynamic GetProfile()
 {
 return HttpContext.Current.Profile;
 }
 public dynamic UpdateProfile(string FirstName, string LastName, string GSM)
 {
 HttpContext.Current.Profile.SetPropertyValue(&quot;FirstName&quot;, FirstName);
 HttpContext.Current.Profile.SetPropertyValue(&quot;LastName&quot;, LastName);
 HttpContext.Current.Profile.SetPropertyValue(&quot;GSM&quot;, GSM);
 HttpContext.Current.Profile.Save();
 return HttpContext.Current.Profile;
 }
}
</pre>
<p>A page that uses the ProfilePresenter class : displaying the profile properties, and edit them</p>
<p>ObjectDataSource:</p>
<pre class="brush: csharp; title: ;">

&lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;ProfileODS&quot; TypeName=&quot;Board.Presenters.ProfilePresenter&quot;
 SelectMethod=&quot;GetProfile&quot; UpdateMethod=&quot;UpdateProfile&quot;/&gt;
&lt;pre&gt;</pre>
<p>FormView: Display part</p>
<pre class="brush: csharp; title: ;">
 &lt;asp:FormView runat=&quot;server&quot; ID=&quot;ProfileFV&quot; DataSourceID=&quot;ProfileODS&quot; RenderOuterTable=&quot;false&quot;&gt;
 &lt;EmptyDataTemplate&gt;
 &lt;p&gt;
 No profil datas
 &lt;/p&gt;
 &lt;/EmptyDataTemplate&gt;
 &lt;ItemTemplate&gt;
 &lt;h2&gt;
 Profil informations&lt;/h2&gt;
 &lt;div&gt;
 &lt;asp:Button runat=&quot;server&quot; ID=&quot;Edit&quot; CommandName=&quot;Edit&quot; Text=&quot;Edit&quot; /&gt;&lt;/div&gt;
 &lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th colspan=&quot;2&quot;&gt;
 Infos
 &lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;th&gt;
 LastName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;%# Eval(&quot;LastName&quot;)%&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 FirstName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;%# Eval(&quot;FirstName&quot;)%&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 GSM
 &lt;/th&gt;
 &lt;td&gt;
 &lt;%# Eval(&quot;GSM&quot;)%&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
 &lt;/table&gt;
 &lt;/ItemTemplate&gt;
</pre>
<p>FormView : the Edit part</p>
<pre class="brush: csharp; title: ;">
 &lt;EditItemTemplate&gt;
 &lt;h2&gt;
 Profil Update&lt;/h2&gt;
 &lt;div&gt;
 &lt;asp:Button runat=&quot;server&quot; ID=&quot;Edit&quot; CommandName=&quot;Cancel&quot; Text=&quot;Annuler&quot; /&gt;
 &lt;asp:Button runat=&quot;server&quot; ID=&quot;Button1&quot; CommandName=&quot;Update&quot; Text=&quot;Sauver&quot; /&gt;&lt;/div&gt;
 &lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th colspan=&quot;2&quot;&gt;
Infos
 &lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;th&gt;
 LastName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;LastName&quot; Text='&lt;%# Bind(&quot;LastName&quot;)%&gt;' /&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 FirstName
 &lt;/th&gt;
 &lt;td&gt;
 &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;FirstName&quot; Text='&lt;%# Bind(&quot;FirstName&quot;)%&gt;' /&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;th&gt;
 GSM
 &lt;/th&gt;
 &lt;td&gt;
 &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;GSM&quot; Text='&lt;%# Bind(&quot;GSM&quot;)%&gt;' /&gt;
 &lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
 &lt;/table&gt;
 &lt;/EditItemTemplate&gt;
 &lt;/asp:FormView&gt;
</pre>
<p>A simple use :</p>
<pre class="brush: csharp; title: ;">

dynamic profil = HttpContext.Current.Profile;
if (profil != null)
{
 Console.Writeline(profil.GSM);
}
</pre>
<p>Another example : retrieve the gsm numbers from  members of a security role. It shows how to retrieve the profile of an other user</p>
<pre class="brush: csharp; title: ;">

string[] users = Roles.GetUsersInRole(role);

List&lt;string&gt; gsm = new List&lt;string&gt;();
foreach (string user in users)
{
 dynamic profil = HttpContext.Current.Profile;
 dynamic profi = profil.GetProfile(user);
 if (profi != null)
 {
 if (!string.IsNullOrEmpty(profi.GSM))
 {
 gsm.Add(profi.GSM);
 }
 }
}
</pre>
<p>Let me know what you think !</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2012/01/dynamic-is-magic-a-solution-to-use-the-asp-net-profile-in-web-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Different Ways to Transfer Data between pages by Peter Bromberg</title>
		<link>http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/</link>
		<comments>http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 08:46:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=922</guid>
		<description><![CDATA[It&#8217;s a common question when working with asp.net, and Peter Bromberg has done a nice job by grouping eight methods to do it! Read his article here : http://www.eggheadcafe.com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx]]></description>
			<content:encoded><![CDATA[<blockquote><p>It&#8217;s a common question when working with asp.net, and Peter Bromberg has done a nice job by grouping eight methods to do it!</p></blockquote>
<p>Read his article here :<a href="http://www.eggheadcafe.com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx" target="_blank"> http://www.eggheadcafe.com/tutorials/asp-net/e653f028-01fb-4d0e-843b-058deae562a2/eight-different-ways-to-transfer-data-from-one-page-to-another-page.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/12/different-ways-to-transfer-data-between-pages-by-peter-bromberg/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET videos : Choosing the Right Programming Model</title>
		<link>http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/</link>
		<comments>http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 20:34:09 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=897</guid>
		<description><![CDATA[As mentioned by Kris van der Mast on the asp.net forums, this video was on the get started page before, but don&#8217;t appears to be there anymore. This video is great to know in 5 minutes which model is good for you. So, because I don&#8217;t want to search for it, I post this video [...]]]></description>
			<content:encoded><![CDATA[<p>As mentioned by <a href="http://blog.krisvandermast.com/" target="_blank">Kris van der Mast</a> on the asp.net forums, this video was on the get started page before, but don&#8217;t appears to be there anymore.</p>
<p>This video is great to know in 5 minutes which model is good for you.</p>
<p>So, because I don&#8217;t want to search for it, I post this video here :</p>
<p><object style="width:400px;height:338px;" autoupdate="true" data="data:application/x-silverlight-2," type="application/x-silverlight-2"><param value="2.0.31005.0" name="MinRuntimeVersion"/><param name="source" value="http://www.asp.net/clientbin/mediaplayer/MSCommunities.MediaPlayer.xap" /><param value="videoid=24686" name="InitParams"/><a href="http://go2.microsoft.com/fwlink/?LinkID=114576&amp;v=2.0"><img style="border-width: 0px;" alt="Install Silverlight" src="http://i2.asp.net/common/static-asp/asp.net/videos/silverlight.mediaplayer/slplayer_disabled.png?cdn_id=04302010"/></a></object></p>
<p>(<a href="http://www.asp.net/general/videos/choosing-the-right-programming-model" target="_blank">direct link</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/06/asp-net-videos-choosing-the-right-programming-model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET WebForms Datacontrols : Dropdownlist and detailview connected.</title>
		<link>http://www.sambeauvois.be/blog/2011/01/asp-net-webforms-datacontrols-dropdownlist-and-detailview-connected-2/</link>
		<comments>http://www.sambeauvois.be/blog/2011/01/asp-net-webforms-datacontrols-dropdownlist-and-detailview-connected-2/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 12:05:00 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=782</guid>
		<description><![CDATA[Links for this article : The complete source MSDN : objectdatasource, detailview How to link a dropdownlist and a detailview in both ways ? Ie: I want my dropdownlist to be populated with a list of vendors, and if I select one of them I want to see the details of the vendor in the [...]]]></description>
			<content:encoded><![CDATA[<p>Links for this article :</p>
<ul>
<li><a href="http://www.sambeauvois.be/Codes/ASP.NET/DropDownDetailView.zip" target="_blank">The complete source</a></li>
<li>MSDN : <a href="http://msdn.microsoft.com/en-us/library/9a4kyhcx.aspx" target="_blank">objectdatasource</a>, <a href="http://msdn.microsoft.com/en-us/library/s3w1w7t4.aspx" target="_blank">detailview</a></li>
</ul>
<blockquote><p>How to link a dropdownlist and a detailview in both ways ?</p></blockquote>
<p>Ie: I want my dropdownlist to be populated with a list of vendors, and if I select one of them I want to see the details of the vendor in the detailview.<br />
Plus, if I add/delete/update a vendor in the detailview, I want the dropdownlist to be updated.</p>
<p>This kind of questions is often asked on different forums, here is my solution:</p>
<h2>Here are the steps to follow.</h2>
<h3>Step 1 : getting the datas</h3>
<p>First we need a datasource, so for the demo we will create a simple vendor class in our DAL.</p>
<p>The vendor class has three properties : ID, Name and WebSite and one static method : &#8220;All&#8221; who allows us to retrieve a list of vendors.</p>
<h3>Step 2 : managing the datas</h3>
<p>We also need a manager class for these vendors, which will be in our BLL.</p>
<p>VendorsManager will contain the simple crud methods (Get, Insert, Update, Delete) and a &#8220;GetAll&#8221; method which simply call the DAL.Vendors’s &#8220;All&#8221; Method</p>
<h3>Step 3 : displaying the data</h3>
<p>Now that we have our datasource methods ready, we can create a webform named “DropDownDetailView.aspx”</p>
<p>The solution has to be like that:</p>
<p><img class="alignnone size-full wp-image-786" title="Pic1" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic1.png" alt="" width="255" height="250" /></p>
<p>In the aspx markup we’ll add an objectdatasource which will be used by the dropdownlist.<br />
This objectdatasource will use the GetAll method from our BLL.</p>
<pre class="brush: xml; title: ;">

&lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;VendorsDataSource&quot;
TypeName=&quot;DataControls.BLL.VendorsManager&quot;
SelectMethod=&quot;GetAll&quot;/&gt;
</pre>
<p>Then we add the dropdownlist and configure it to use the objectdatasource.</p>
<pre class="brush: xml; title: ;">

&lt;asp:DropDownList runat=&quot;server&quot; ID=&quot;VendorsList&quot;
DataSourceID=&quot;VendorsDataSource&quot;
DataValueField=&quot;ID&quot; DataTextField=&quot;Name&quot; /&gt;
</pre>
<p>If you run the application you will have a dropdownlist populated with our vendors<br />
<img class="alignnone size-full wp-image-787" title="Pic2" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic2.png" alt="" width="114" height="182" /></p>
<p>Now we add a second objectdatasource with the same TypeName, and we set the SelectMethod, UpdateMethod, DeleteMethod and InsertMethod to use the methods of the VendorsManager’s methods.</p>
<pre class="brush: xml; title: ;">

&lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;VendorDetailDataSource&quot;
TypeName=&quot;DataControls.BLL.VendorsManager&quot;
SelectMethod=&quot;Get&quot;
UpdateMethod=&quot;Update&quot;
DeleteMethod=&quot;Delete&quot;
InsertMethod=&quot;Insert&quot;/&gt;
</pre>
<p>And because the GetMethod takes a parameter, we will say to the objectdatasource to take this parameter from the Selected value of the dropdownlist.</p>
<pre class="brush: xml; title: ;">

&lt;SelectParameters&gt;
&lt;asp:ControlParameter Name=&quot;id&quot; ControlID=&quot;VendorsList&quot; PropertyName=&quot;SelectedValue&quot; Type=&quot;Int32&quot; /&gt;
&lt;/SelectParameters&gt;
</pre>
<p>So the complete objectdatasource will be</p>
<pre class="brush: xml; title: ;">

&lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;VendorDetailDataSource&quot;
TypeName=&quot;DataControls.BLL.VendorsManager&quot;
SelectMethod=&quot;Get&quot;
UpdateMethod=&quot;Update&quot;
DeleteMethod=&quot;Delete&quot;
InsertMethod=&quot;Insert&quot;&gt;
&lt;SelectParameters&gt;
&lt;asp:ControlParameter Name=&quot;id&quot; ControlID=&quot;VendorsList&quot; PropertyName=&quot;SelectedValue&quot; Type=&quot;Int32&quot; /&gt;
&lt;/SelectParameters&gt;
&lt;/asp:ObjectDataSource&gt;
</pre>
<p>Now that we have our datasource, we can link it to a detailview.<br />
So we create the detailview and tell him to use the datasource</p>
<pre class="brush: xml; title: ;">

&lt;asp:DetailsView runat=&quot;server&quot; ID=&quot;VendorDetail&quot;
DataSourceID=&quot;VendorDetailDataSource&quot;  /&gt;
</pre>
<p>If we run the application, we see that the detailview is filled with the details of the first vendor<br />
<img class="alignnone size-full wp-image-788" title="Pic3" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic3.png" alt="" width="236" height="131" /></p>
<p>Try to select another vendor in the list -&gt; nothing happens!<br />
It’s because we don’t cause a postback to the server to let him change the data.</p>
<p>To achieve that, we simply set the AutoPostBack property of the dropdownlist to true, so the dropdownlist is</p>
<pre class="brush: xml; title: ;">

&lt;asp:DropDownList runat=&quot;server&quot; ID=&quot;VendorsList&quot;
DataSourceID=&quot;VendorsDataSource&quot;
DataValueField=&quot;ID&quot; DataTextField=&quot;Name&quot;
AutoPostBack=&quot;true&quot;/&gt;
</pre>
<p>Re-run the application and change the selected value of the dropdownlist.</p>
<p><img class="alignnone size-full wp-image-789" title="Pic4" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic4.png" alt="" width="243" height="106" /><br />
Now the data in the detailview matches the selected vendor.</p>
<p><strong>The first part of our need is achieved! </strong></p>
<h3>Step 4 : playing with the data</h3>
<p>Now we want to be able to add, edit or delete vendors, so we set the detailview&#8217;s propertie AutoGenerateDeleteButton,  AutoGenerateEditButton and  AutoGenerateInsertButton to true.</p>
<p>And we don’t forget to set the DataKeyNames property to “ID”, because ID is the key for our vendors.</p>
<p>So now, the detailview looks like this:</p>
<pre class="brush: xml; title: ;">

&lt;asp:DetailsView runat=&quot;server&quot; ID=&quot;VendorDetail&quot;
DataSourceID=&quot;VendorDetailDataSource&quot;
DataKeyNames=&quot;ID&quot;
AutoGenerateDeleteButton=&quot;true&quot;
AutoGenerateEditButton=&quot;true&quot;
AutoGenerateInsertButton=&quot;true&quot;/&gt;
</pre>
<p>Run the application and try to add a new item<br />
<img class="alignnone size-full wp-image-790" title="Pic5" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic5.png" alt="" width="235" height="211" /></p>
<p><img class="alignnone size-full wp-image-791" title="Pic6" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic6.png" alt="" width="254" height="223" /></p>
<p>Now that the new vendor is inserted, look at the values in the dropdownlist<br />
<img class="alignnone size-full wp-image-792" title="Pic7" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic7.png" alt="" width="270" height="201" /></p>
<p>Arghh, the new vendor is not in the list !</p>
<p>It’s normal! We don’t refresh the dropdownlist yet!<br />
To do that, we’ll add handlers for deleted, inserted and updated events.</p>
<p>The aspx markup of the detailview becomes</p>
<pre class="brush: xml; title: ;">

&lt;asp:DetailsView runat=&quot;server&quot; ID=&quot;VendorDetail&quot;
DataSourceID=&quot;VendorDetailDataSource&quot;
DataKeyNames=&quot;ID&quot;
AutoGenerateDeleteButton=&quot;true&quot;
AutoGenerateEditButton=&quot;true&quot;
AutoGenerateInsertButton=&quot;true&quot;
onitemdeleted=&quot;VendorDetail_ItemDeleted&quot;
oniteminserted=&quot;VendorDetail_ItemInserted&quot;
onitemupdated=&quot;VendorDetail_ItemUpdated&quot;/&gt;
</pre>
<p>and in the code-behind of the page :</p>
<pre class="brush: csharp; title: ;">

protected void VendorDetail_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
Refresh();
}

protected void VendorDetail_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
Refresh();
}

protected void VendorDetail_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
Refresh();
}
</pre>
<p>In each handler, I call  the Refresh() method , and in this method we will rebind the dropdownlist:</p>
<pre class="brush: csharp; title: ;">

private void Refresh()
{
this.VendorsList.DataBind();
}
</pre>
<p>Now, run the application, and redo the same test:</p>
<p><img class="alignnone size-full wp-image-793" title="Pic8" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic8.png" alt="" width="238" height="197" /></p>
<p><img class="alignnone size-full wp-image-798" title="Pic9" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic91.png" alt="" width="264" height="228" /><br />
Now look at the dropdownlist:<br />
<img class="alignnone size-full wp-image-795" title="Pic10" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic10.png" alt="" width="231" height="245" /><br />
And that’s it! The new vendor is in the list<br />
<img class="alignnone size-full wp-image-796" title="Pic11" src="http://www.sambeauvois.be/blog/wp-content/uploads/2011/01/Pic11.png" alt="" width="173" height="144" /></p>
<p>Now, all the steps are processed!</p>
<p>Here is the complete final aspx markup</p>
<pre class="brush: xml; title: ;">

&lt;%@ Page Language=&quot;C#&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;DropDownDetailView.aspx.cs&quot; Inherits=&quot;DataControls.DropDownDetailView&quot; %&gt;

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
 &lt;title&gt;Dropdownlist and detailview demo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
 &lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
 &lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;VendorsDataSource&quot;
 TypeName=&quot;DataControls.BLL.VendorsManager&quot;
 SelectMethod=&quot;GetAll&quot;/&gt;
 &lt;asp:DropDownList runat=&quot;server&quot; ID=&quot;VendorsList&quot;
 DataSourceID=&quot;VendorsDataSource&quot;
 DataValueField=&quot;ID&quot; DataTextField=&quot;Name&quot;
 AutoPostBack=&quot;true&quot;/&gt;

 &lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;VendorDetailDataSource&quot;
 TypeName=&quot;DataControls.BLL.VendorsManager&quot;
 SelectMethod=&quot;Get&quot;
 UpdateMethod=&quot;Update&quot;
 DeleteMethod=&quot;Delete&quot;
 InsertMethod=&quot;Insert&quot;&gt;
 &lt;SelectParameters&gt;
 &lt;asp:ControlParameter Name=&quot;id&quot; ControlID=&quot;VendorsList&quot; PropertyName=&quot;SelectedValue&quot; Type=&quot;Int32&quot; /&gt;
 &lt;/SelectParameters&gt;
 &lt;/asp:ObjectDataSource&gt;
 &lt;asp:DetailsView runat=&quot;server&quot; ID=&quot;VendorDetail&quot;
 DataSourceID=&quot;VendorDetailDataSource&quot;
 DataKeyNames=&quot;ID&quot;
 AutoGenerateDeleteButton=&quot;true&quot;
 AutoGenerateEditButton=&quot;true&quot;
 AutoGenerateInsertButton=&quot;true&quot;
 onitemdeleted=&quot;VendorDetail_ItemDeleted&quot;
 oniteminserted=&quot;VendorDetail_ItemInserted&quot;
 onitemupdated=&quot;VendorDetail_ItemUpdated&quot;/&gt;
 &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>And the matching code behind</p>
<pre class="brush: csharp; title: ;">

using System.Web.UI.WebControls;

namespace DataControls
{
 public partial class DropDownDetailView : System.Web.UI.Page
 {
 protected void VendorDetail_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
 {
 Refresh();
 }

 protected void VendorDetail_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
 {
 Refresh();
 }

 protected void VendorDetail_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
 {
 Refresh();
 }

 private void Refresh()
 {
 this.VendorsList.DataBind();
 }
 }
}
</pre>
<p>Links for this article :</p>
<ul>
<li><a href="http://www.sambeauvois.be/Codes/ASP.NET/DropDownDetailView.zip" target="_blank">The complete source</a></li>
<li>MSDN : <a href="http://msdn.microsoft.com/en-us/library/9a4kyhcx.aspx" target="_blank">objectdatasource</a>, <a href="http://msdn.microsoft.com/en-us/library/s3w1w7t4.aspx" target="_blank">detailview</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2011/01/asp-net-webforms-datacontrols-dropdownlist-and-detailview-connected-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Free beta ebook and samples : ASP.NET Web Pages with Razor Syntax</title>
		<link>http://www.sambeauvois.be/blog/2010/12/free-beta-ebook-and-samples-asp-net-web-pages-with-razor-syntax/</link>
		<comments>http://www.sambeauvois.be/blog/2010/12/free-beta-ebook-and-samples-asp-net-web-pages-with-razor-syntax/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 13:06:12 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=771</guid>
		<description><![CDATA[ASP.NET Web Pages with Razor Syntax is a web framework and part of WebMatrix which gives you everything you need to build Web sites using Windows. A free  ebook (it&#8217;s a draft) and the samples are availables on the Microsoft Download Center. Here is the table of contents : Chapter 1 – Getting Started with [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>ASP.NET Web Pages with Razor Syntax is a web framework and part of WebMatrix which gives you everything you need to build Web sites using Windows.</p></blockquote>
<p>A free  ebook (it&#8217;s a draft) and the samples are availables on the<a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e750fc0b-8b8f-46f9-b30f-0ead6f6e538c" target="_blank"> Microsoft Download Center</a>.</p>
<p>Here is the table of contents :</p>
<h4>Chapter 1 – Getting Started with WebMatrix Beta and ASPNET Web Pages 1</h4>
<p>What is WebMatrix Beta?  1<br />
Installing WebMatrix Beta  1<br />
Getting Started with WebMatrix Beta  2<br />
Creating a Web Page  4<br />
Installing Helpers with Package Manager  7<br />
Using ASPNET Web Pages Code  9<br />
Programming ASPNET Razor Pages in Visual Studio  12<br />
Creating and Testing ASPNET Pages Using Your Own Text Editor  13</p>
<h4>Chapter 2 – Introduction to ASPNET Web Programming Using the Razor Syntax  15</h4>
<p>The Top 8 Programming Tips  15<br />
HTML Encoding  16<br />
HTTP GET and POST Methods and the IsPost Property  21<br />
A Simple Code Example  22<br />
Basic Programming Concepts  24<br />
Classes and Instances 25<br />
Language and Syntax  26<br />
Additional Resources  47</p>
<h4>Chapter 3 – Creating a Consistent Look  48</h4>
<p>Creating Reusable Blocks of Content  48<br />
Creating a Consistent Look Using Layout Pages  51<br />
Designing Layout Pages That Have Multiple Content Sections  55<br />
Making Content Sections Optional  58<br />
Passing Data to Layout Pages  59<br />
Creating and Using a Basic Helper  65</p>
<h4>Chapter 4 – Working with Forms  67</h4>
<p>Creating a Simple HTML Form  67<br />
Reading User Input From the Form  68<br />
HTML Encoding for Appearance and Security  70<br />
Validating User Input  71<br />
Restoring Form Values After Postbacks  73<br />
Additional Resources  74</p>
<h4>Chapter 5 – Working with Data  75</h4>
<p>Introduction to Databases  75<br />
Relational Databases  76<br />
Creating a Database  76<br />
Adding Data to the Database  78<br />
Displaying Data from a Database  79<br />
Structured Query Language (SQL) 81<br />
Inserting Data in a Database  82<br />
Updating Data in a Database  86<br />
Deleting Data in a Database  91<br />
Connecting to a Database  95<br />
Additional Resources  96</p>
<h4>Chapter 6: Displaying Data in a Grid  97</h4>
<p>The WebGrid Helper  97<br />
Displaying Data Using the WebGrid Helper 97<br />
Specifying and Formatting Columns to Display  99<br />
Styling the Grid as a Whole  102<br />
Paging Through Data  103</p>
<h4>Chapter 7 – Displaying Data in a Chart  106</h4>
<p>The Chart Helper  106<br />
Chart Elements  107<br />
Creating a Chart from Data  108<br />
&#8220;Using&#8221; Statements and Fully Qualified Names  114<br />
Displaying Charts Inside a Web Page  115<br />
Styling a Chart  116<br />
Saving a Chart  117<br />
Additional Resources  123</p>
<h4>Chapter 8 – Working with Files  124</h4>
<p>Creating a Text File and Writing Data to It  124<br />
Appending Data to an Existing File  127<br />
Reading and Displaying Data from a File  128<br />
Displaying Data from a Microsoft Excel Comma-Delimited File  131<br />
Deleting Files  131<br />
Letting Users Upload a File  133<br />
Letting Users Upload Multiple Files 136<br />
Additional Resources  138</p>
<h4>Chapter 9 – Working with Images  139</h4>
<p>Adding an Image to a Web Page Dynamically  139<br />
Uploading an Image  142<br />
About GUIDs  144<br />
Resizing an Image  144<br />
Rotating and Flipping an Image  147<br />
Adding a Watermark to an Image  148<br />
Using an Image As a Watermark  149</p>
<h4>Chapter 10 – Working with Video  152</h4>
<p>Choosing a Video Player  152<br />
MIME Types  153<br />
Playing Flash (swf) Videos  153<br />
Playing MediaPlayer (wmv) Videos  156<br />
Playing Silverlight Videos  158<br />
Additional Resources  159</p>
<h4>Chapter 11 – Adding Email to Your Website  160</h4>
<p>Sending Email Messages from Your Website  160<br />
Sending a File Using Email  164<br />
Additional Resources  166</p>
<h4>Chapter 12 – Adding Search to Your Website  167</h4>
<p>Searching from Your Website  167<br />
Additional Resources  170</p>
<h4>Chapter 13 – Adding Social Networking to Your Web Site  171</h4>
<p>Linking Your Website on Social Networking Sites  171<br />
Adding a Twitter Feed  172<br />
Rendering a Gravatar Image 174<br />
Displaying an Xbox Gamer Card  175<br />
Displaying a Facebook &#8220;Like&#8221; Button  176</p>
<h4>Chapter 14 – Analyzing Traffic  179</h4>
<p>Tracking Visitor Information (Analytics)  179</p>
<h4>Chapter 15 – Caching to Improve the Performance of Your Website  183</h4>
<p>Caching to Improve Website Responsiveness  183</p>
<h4>Chapter 16 – Adding Security and Membership  186</h4>
<p>Introduction to Website Membership  186<br />
Creating a Website That Has Registration and Login Pages  187<br />
Creating a Members-Only Page  191<br />
Creating Security for Groups of Users (Roles)  192<br />
Creating a Password-Change Page  194<br />
Letting Users Generate a New Password  196<br />
Preventing Automated Programs from Joining Your Website  200</p>
<h4>Chapter 17 – Introduction to Debugging  203</h4>
<p>Using the ServerInfo Helper to Display Server Information  203<br />
Embedding Output Expressions to Display Page Values  205<br />
Using the ObjectInfo Helper to Display Object Values  209<br />
Using Debugging Tools  210<br />
Additional Resources  213</p>
<h4>Chapter 18 – Customizing Site-Wide Behavior  214</h4>
<p>Adding Website Startup Code  214<br />
Running Code Before and After Files in a Folder 218<br />
Creating More Readable and Searchable URLs  224</p>
<h4>Appendix – ASPNET Quick API Reference  227</h4>
<p>Classes  227<br />
Data  233<br />
Helpers  234</p>
<h4>Appendix – ASPNET Web Pages Visual Basic  241</h4>
<p>The Top 8 Programming Tips  241<br />
HTML Encoding  242<br />
A Simple Code Example  248<br />
Visual Basic Language and Syntax  250<br />
Additional Resources  271</p>
<h4>Appendix – Programming ASPNET Web Pages in Visual Studio  272</h4>
<p>Why Use Visual Studio?  272<br />
Installing the ASPNET Razor Tools  272<br />
Using the ASPNET Razor Tools for Visual Studio  273</p>
<h4>Disclaimer  278</h4>
<p>Previous versions of the draft are also <a href="http://www.microsoft.com/downloads/en/results.aspx?freetext=ASP.NET+Web+Pages+with+Razor+Syntax&amp;displaylang=en&amp;stype=s_basic" target="_blank">available</a>.</p>
<p>For the latest informations and more about webmatrix and the Razor syntax, visit <a href="http://www.asp.net/webmatrix/" target="_blank">http://www.asp.net/webmatrix/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/12/free-beta-ebook-and-samples-asp-net-web-pages-with-razor-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Analyse your web traffic without redeployment.</title>
		<link>http://www.sambeauvois.be/blog/2010/11/analyse-your-web-traffic-without-redeployment/</link>
		<comments>http://www.sambeauvois.be/blog/2010/11/analyse-your-web-traffic-without-redeployment/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 11:41:48 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=666</guid>
		<description><![CDATA[The spying of users activity is good for you business﻿ ! Web analytics are an important part for a website; it allows the site owner to understand what users are looking for and to enhance his site in order to grab more users. Web Analytics systems. One of the most popular system is google analytics, [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>The spying of users activity is good for you business﻿ !</p></blockquote>
<p>Web analytics are an important part for a website; it allows the site owner to understand what users are looking for and to enhance his site in order to grab more users.</p>
<h3>Web Analytics systems.</h3>
<p>One of the most popular system is google analytics, if you have a google account, you can use this system for your sites.<br />
You just have to create an entry for your website on <a href="http://www.google.com/analytics" target="_blank">www.google.com/analytics</a>.<br />
It generates for you a piece of code with a specific ID that you have to paste just before the closing head tag of each of the pages you want to monitor.</p>
<p>An alternative is Piwik, which is open source.<br />
This web application has to be installed on a web server (with php/mysql).<br />
This app also generates for you a piece of javascript code that you have to paste right before the closing body tag.</p>
<h3>With ASP.NET Webforms.</h3>
<p>In ASP.NET Webform you can copy this tracking code on your main Master page and it’s included in all pages using this master page.<br />
But it requires modifying the page and redeploying the web site.</p>
<p>You can also use the Global.asax to add code to each page which is generated. It also requires redeployment.</p>
<p>Another alternative are the <a href="http://msdn.microsoft.com/en-us/library/zec9k340%28v=VS.85%29.aspx" target="_blank">httpmodules</a>.<br />
It allows you to add functionalities to a website with just copying a dll file in the bin directory of your application and reference it in your main web.config file.</p>
<p>It’s the solution I choose in order to add analytics functionality to a website already running.</p>
<h3>SAHM for Simple Analytics HttpModule.</h3>
<p>My first implementation added only google tracking code, but I added piwik just in case and because I was testing this system.</p>
<p>I made it public, so you can download it from codeplex at the address : <a href="http://sahm.codeplex.com/" target="_blank">http://sahm.codeplex.com/</a></p>
<h4>To use it:</h4>
<p>1.) Copy the SAHM.dll file in the bin folder of your application</p>
<p>2.) Edit your Web.Config application file.</p>
<p>2.1) Locate &lt;configSections&gt; and add</p>
<pre class="brush: xml; title: ;">

&lt;sectionGroup name=&quot;SAHM&quot;&gt;
 &lt;section name=&quot;google&quot; type=&quot;SAHM.GoogleConfig, SAHM&quot; allowLocation=&quot;true&quot; allowDefinition=&quot;Everywhere&quot;/&gt;
 &lt;section name=&quot;piwik&quot; type=&quot;SAHM.PiwikConfig, SAHM&quot; allowLocation=&quot;true&quot; allowDefinition=&quot;Everywhere&quot;/&gt;
&lt;/sectionGroup&gt;
</pre>
<p>2.1) Add a SAHM section</p>
<pre class="brush: xml; title: ;">

&lt;SAHM&gt;
 &lt;google Code=&quot;UA-XXXXXX-X&quot;/&gt;
 &lt;piwik Server=&quot;www.yourpiwikserver/&quot; IDSite=&quot;1&quot;/&gt;
&lt;/SAHM&gt;
</pre>
<p>Set your identifiers.</p>
<p>For the piwik : be sure to set the Server value without the &#8220;http://&#8221;</p>
<p>2.3) Add the module in the &lt;module&gt;</p>
<pre class="brush: xml; title: ;">

&lt;httpModules&gt;
 &lt;add name=&quot;SAHM&quot; type=&quot;SAHM.AnalyticsModule, SAHM&quot;/&gt;
 &lt;/httpModules&gt;
</pre>
<p>or if you use II7</p>
<pre class="brush: xml; title: ;">

&lt;system.webServer&gt;
 &lt;modules runAllManagedModulesForAllRequests=&quot;true&quot;/&gt;
 &lt;/system.webServer&gt;
</pre>
<p>2.4) Save your web.config file and test if your tracking code is in your page code.</p>
<p>2.5) That&#8217;s it</p>
<p>If you enjoy this module, please let me know!<br />
Happy web traffic analyses.</p>
<p>Download link : <a href="http://sahm.codeplex.com/" target="_blank">http://sahm.codeplex.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/11/analyse-your-web-traffic-without-redeployment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ASP.NET do and don’ts serie : episode 4 – If you retrieve server controls in your code-behind do it the safe way !</title>
		<link>http://www.sambeauvois.be/blog/2010/09/the-asp-net-do-and-donts-if-you-retrieve-server-controls-in-your-code-behind-do-it-the-right-safe-way/</link>
		<comments>http://www.sambeauvois.be/blog/2010/09/the-asp-net-do-and-donts-if-you-retrieve-server-controls-in-your-code-behind-do-it-the-right-safe-way/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 14:48:40 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Do and don'ts]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=633</guid>
		<description><![CDATA[When we are developping with databound controls, a common action is to retreive controls (like dropdownlists) in an objectdatasource&#8217;s &#8220;Inserting&#8221; event. Here is a specific example : In the InsertItemTemplate of a ListView: &#60;asp:ObjectDataSource runat=&#34;server&#34; ID=&#34;MyDataSource&#34; // ... /&#62; &#60;asp:DropDownList runat=&#34;server&#34; ID=&#34;MyDDL&#34; DataSourceID=&#34;MyDataSource&#34; DataTextField=&#34;TheText&#34; DataValueField=&#34;TheValue&#34;/&#62; Do: protected void TheDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e) { if (MyListView.InsertItem [...]]]></description>
			<content:encoded><![CDATA[<p>When we are developping with databound controls, a common action is to retreive controls (like dropdownlists) in an objectdatasource&#8217;s &#8220;Inserting&#8221; event.</p>
<p>Here is a specific example :</p>
<p>In the InsertItemTemplate of a ListView:</p>
<pre class="brush: xml; title: ;">

&lt;asp:ObjectDataSource runat=&quot;server&quot; ID=&quot;MyDataSource&quot;
 // ... /&gt;

&lt;asp:DropDownList runat=&quot;server&quot;
       ID=&quot;MyDDL&quot;
       DataSourceID=&quot;MyDataSource&quot;
       DataTextField=&quot;TheText&quot;
       DataValueField=&quot;TheValue&quot;/&gt;
</pre>
<h3><span style="text-decoration: underline;"><span style="color: #00ff00;">Do:</span></span></h3>
<pre class="brush: csharp; title: ;">

protected void TheDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
    if (MyListView.InsertItem == null)
       return;

     DropDownList myDDL= MyListView.InsertItem.FindControl(&quot;MyDDL&quot;) as DropDownList;
     if (myDDL== null)
         return;
     if (string.IsNullOrEmpty(myDDL.SelectedValue))
         return;

    e.InputParameters[&quot;TheWantedValue&quot;] = myDDL.SelectedValue;
 }
</pre>
<h3><span style="text-decoration: underline;"><span style="color: #ff0000;">Don&#8217;t:</span></span></h3>
<pre class="brush: csharp; title: ;">

protected void TheDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
    e.InputParameters.Add(&quot;TheWantedValue&quot;, ((DropDownList)MyListView.InsertItem.FindControl(&quot;MyDDL&quot;)).SelectedValue);
}
</pre>
<p>This for an obvious reason : you can’t always be sure that a problem won&#8217;t pop up.</p>
<p>A random problem can occur anywhere, whether the dropdownlist population or something else</p>
<p>So, be safe and protect yourself !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/09/the-asp-net-do-and-donts-if-you-retrieve-server-controls-in-your-code-behind-do-it-the-right-safe-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ASP.NET do and don’ts serie : episode 3 – use the validator designed for your needs !</title>
		<link>http://www.sambeauvois.be/blog/2010/09/asp-net-do-and-dont-use-the-validator-designed-for-your-needs/</link>
		<comments>http://www.sambeauvois.be/blog/2010/09/asp-net-do-and-dont-use-the-validator-designed-for-your-needs/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 13:48:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Do and don'ts]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=616</guid>
		<description><![CDATA[This post is relative to the first episode of the &#8220;ASP.NET do and don’ts serie&#8221; ASP.NET contains several validation controls. Be sure to use the good ones for your needs. Imagine you have the following dropdownlist &#60;asp:DropDownList runat=&#34;server&#34; ID=&#34;MyDDL&#34; // datasource params AppendDataBoundItems=&#34;true&#34;&#62; &#60;asp:ListItem Text=&#34;select a value&#34; Value=&#34;-1&#34; Selected=&#34;True&#34; /&#62; &#60;/asp:DropDownList&#62; Do: &#60;asp:RequiredFieldValidator runat=&#34;server&#34; ID=&#34;MyDDLValidator&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>This post is relative to the <a href="http://www.sambeauvois.be/blog/2010/08/the-asp-net-do-and-donts-serie-episode-1-the-custom-validator-control/" target="_blank">first episode</a> of the &#8220;ASP.NET do and don’ts serie&#8221;</p>
<p>ASP.NET contains several validation controls. Be sure to use the good ones for your needs.</p>
<p>Imagine you have the following dropdownlist</p>
<pre class="brush: xml; title: ;">
&lt;asp:DropDownList runat=&quot;server&quot; ID=&quot;MyDDL&quot;
      // datasource params
     AppendDataBoundItems=&quot;true&quot;&gt;
     &lt;asp:ListItem Text=&quot;select a value&quot; Value=&quot;-1&quot; Selected=&quot;True&quot; /&gt;
&lt;/asp:DropDownList&gt;
</pre>
<h3><span style="color: #00ff00; text-decoration: underline;">Do:</span></h3>
<pre class="brush: xml; title: ;">

&lt;asp:RequiredFieldValidator runat=&quot;server&quot; ID=&quot;MyDDLValidator&quot; ControlToValidate=&quot;MyDDL&quot; ErrorMessage=&quot;Selection required&quot; InitialValue=&quot;-1&quot; /&gt;
</pre>
<h3><span style="color: #ff0000; text-decoration: underline;">Don&#8217;t:</span></h3>
<pre class="brush: xml; title: ;">

&lt;asp:CompareValidator runat=&quot;server&quot; ID=&quot;MyDDLValidator&quot;
 ValueToCompare=&quot;-1&quot; ControlToValidate=&quot;MyDDL&quot;
 Operator=&quot;NotEqual&quot; ErrorMessage=&quot;Selection required&quot; /&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/09/asp-net-do-and-dont-use-the-validator-designed-for-your-needs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ASP.NET do and don’ts serie : episode 2 – the navigation controls</title>
		<link>http://www.sambeauvois.be/blog/2010/09/the-asp-net-do-and-don%e2%80%99ts-serie-episode-2-%e2%80%93-the-navigation-controls/</link>
		<comments>http://www.sambeauvois.be/blog/2010/09/the-asp-net-do-and-don%e2%80%99ts-serie-episode-2-%e2%80%93-the-navigation-controls/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 12:25:12 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Do and don'ts]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=590</guid>
		<description><![CDATA[There are many controls you can use for the navigation in ASP.NET. Be sure that you use the good ones for the good uses. Let say we want a link on a page to redirect on an other page Do: in the aspx markup &#60;asp:HyperLink runat=&#34;server&#34; ID=&#34;NavigateToOtherPage&#34; NavigateUrl=&#34;~/otherpage.aspx&#34; Text=&#34;Go to the other page&#34; /&#62; or [...]]]></description>
			<content:encoded><![CDATA[<p>There are many controls you can use for the navigation in ASP.NET.</p>
<p>Be sure that you use the good ones for the good uses.</p>
<p>Let say we want a link on a page to redirect on an other page</p>
<h3><span style="color: #00ff00;">Do:</span></h3>
<p>in the aspx markup</p>
<pre class="brush: xml; title: ;">

&lt;asp:HyperLink runat=&quot;server&quot; ID=&quot;NavigateToOtherPage&quot; NavigateUrl=&quot;~/otherpage.aspx&quot; Text=&quot;Go to the other page&quot; /&gt;
</pre>
<p>or even better if you just want to redirect on a page</p>
<pre class="brush: xml; title: ;">

&lt;a href=&quot;otherpage.aspx&quot;&gt;Go to the other page&lt;/a&gt;
</pre>
<h3><span style="color: #ff0000;">Don&#8221;t:</span></h3>
<p>in the aspx markup</p>
<pre class="brush: xml; title: ;">

&lt;asp:LinkButton runat=&quot;server&quot; ID=&quot;NavigateToOtherPage&quot; Text=&quot;Go to the other page&quot; onclick=&quot;RedirectToOtherPage&quot; /&gt;
</pre>
<p>and in the code behind</p>
<pre class="brush: csharp; title: ;">

protected void RedirectToOtherPage(object sender, EventArgs e)
{
     Response.Redirect(&quot;~/otherpage.aspx&quot;);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/09/the-asp-net-do-and-don%e2%80%99ts-serie-episode-2-%e2%80%93-the-navigation-controls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The ASP.NET do and don&#8217;ts serie : episode 1 &#8211; the custom validator control</title>
		<link>http://www.sambeauvois.be/blog/2010/08/the-asp-net-do-and-donts-serie-episode-1-the-custom-validator-control/</link>
		<comments>http://www.sambeauvois.be/blog/2010/08/the-asp-net-do-and-donts-serie-episode-1-the-custom-validator-control/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 13:34:12 +0000</pubDate>
		<dc:creator>Sam Beauvois</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Do and don'ts]]></category>

		<guid isPermaLink="false">http://www.sambeauvois.be/blog/?p=501</guid>
		<description><![CDATA[This is the first post of a serie on the ASP.NET practices. For this serie, I get inspiration from what I see in my daily work or on the web Don&#8217;t use custom validator when you can use an other control ! Do: in the aspx markup &#60;asp:TextBox runat=&#34;server&#34; ID=&#34;FeedURLTbx&#34;  /&#62; &#60;asp:RegularExpressionValidator runat=&#34;server&#34; ID=&#34;URLFormatValidator&#34; ControlToValidate=&#34;FeedURLTbx&#34; [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>This is the first post of a serie on the ASP.NET practices.</p>
<p>For this serie, I get inspiration from what I see in my daily work or on the web</p></blockquote>
<h2>Don&#8217;t use custom validator when you can use an other control !</h2>
<h3><span style="text-decoration: underline;"><span style="color: #00ff00;">Do:</span></span></h3>
<p>in the aspx markup</p>
<pre class="brush: xml; title: ;">

&lt;asp:TextBox runat=&quot;server&quot; ID=&quot;FeedURLTbx&quot;  /&gt;

&lt;asp:RegularExpressionValidator runat=&quot;server&quot;
     ID=&quot;URLFormatValidator&quot;
     ControlToValidate=&quot;FeedURLTbx&quot;
     ValidationExpression=&quot;(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;amp;:/~\+#]*[\w\-\@?^=%&amp;amp;/~\+#])?&quot;
     Text=&quot;!&quot;
     ErrorMessage=&quot;The URL format is not valid.&quot; /&gt;
</pre>
<h3><span style="text-decoration: underline;"><span style="color: #ff0000;">Don&#8217;t:</span></span></h3>
<p>in the aspx markup</p>
<pre class="brush: xml; title: ;">

&lt;asp:TextBox runat=&quot;server&quot; ID=&quot;FeedURLTbx&quot;  /&gt;

 &lt;asp:CustomValidator runat=&quot;server&quot; ID=&quot;URLFormatValidator&quot;
 ControlToValidate=&quot;FeedURLTbx&quot;
 ErrorMessage=&quot;The URL format is not valid.&quot;
 OnServerValidate=&quot;ValidateURL&quot; /&gt;
</pre>
<p>and in the aspx code behind</p>
<pre class="brush: csharp; title: ;">
protected void ValidateURL(object source, ServerValidateEventArgs args)
{

System.Text.RegularExpressions.Regex myRegExp = new  System.Text.RegularExpressions.Regex(@&quot;(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;amp;:/~\+#]*[\w\-\@?^=%&amp;amp;/~\+#])?&quot;);

args.IsValid = myRegExp.IsMatch(args.Value);
}
</pre>
<h3><span style="text-decoration: underline;">Why??</span></h3>
<p>First because the <a href="http://msdn.microsoft.com/en-us/library/eahwtc9e.aspx" target="_blank">RegularExpressionValidator </a>is designed for string validations and it&#8217;s a non sense to do the same with another control !</p>
<p>Second because the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx" target="_blank">CustomValidator </a>used like here will cause a postback while the RegularExpressionValidator will generate the validation javascript and don&#8217;t cause postback.</p>
<p>With the RegularExpressionValidator :<br />
<a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/good_start.png" target="_blank"><img class="size-medium wp-image-506 alignnone" title="good_start" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/good_start-480x188.png" alt="good_start" width="480" height="188" /></a><br />
<a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/good_end.png" target="_blank"><img class="size-medium wp-image-507 alignnone" title="good_end" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/good_end-480x198.png" alt="good_end" width="480" height="198" /></a></p>
<p>With the CustomValidator :</p>
<p><a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/bad_start.png"><img class="alignnone size-medium wp-image-508" title="bad_start" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/bad_start-480x205.png" alt="bad_start" width="480" height="205" /></a><br />
<a href="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/bad_end.png"><img class="alignnone size-medium wp-image-509" title="bad_end" src="http://www.sambeauvois.be/blog/wp-content/uploads/2010/08/bad_end-480x195.png" alt="bad_end" width="480" height="195" /></a></p>
<p>I think the first reason is enough to not discuss the point !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sambeauvois.be/blog/2010/08/the-asp-net-do-and-donts-serie-episode-1-the-custom-validator-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

