<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-5967051795922795442</atom:id><lastBuildDate>Sat, 14 Sep 2024 07:38:56 +0000</lastBuildDate><category>asp data bind</category><category>ajax</category><category>asp net user control design pattern</category><category>bind Dictionary</category><category>bind List</category><category>render user control</category><category>service not found</category><category>silveright</category><category>wcf logging</category><category>web service</category><category>wwDataBind</category><title>.net howto</title><description>Find ideas here.</description><link>http://aspnetfix.blogspot.com/</link><managingEditor>noreply@blogger.com (lt)</managingEditor><generator>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5967051795922795442.post-8690787235607880386</guid><pubDate>Sun, 18 Apr 2010 06:53:00 +0000</pubDate><atom:updated>2010-04-18T10:18:56.317+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">service not found</category><category domain="http://www.blogger.com/atom/ns#">silveright</category><category domain="http://www.blogger.com/atom/ns#">wcf logging</category><title>svc logging</title><description>&lt;p&gt;Service not found exeception is quite annoying in silverlight when calling wcf services. Usually this happens when unhandled exceptions are thrown. One easy way to find out what&#39;s wrong is to enable logging of svc web services in IIS. Just paste code below in web.config. A new file with .svclog will be placed in root of virtual folder running these services.&lt;br /&gt;&lt;p&gt;Also you can check &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms732023.aspx&quot;&gt;here&lt;/a&gt; for more details. You can find it in C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SvcTraceViewer.exe or C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\SvcTraceViewer.exe.&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;diagnostics&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;messageLogging&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;logEntireMessage&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;true&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;logMalformedMessages&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;true&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;logMessagesAtServiceLevel&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;true&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;maxMessagesToLog&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;3000&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;maxSizeOfMessageToLog&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;2000&quot;&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;diagnostics&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;system.serviceModel&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;system.diagnostics&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;sources&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;source&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;System.ServiceModel.MessageLogging&quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;listeners&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;          &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;add&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;messages&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;System.Diagnostics.XmlWriterTraceListener&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;initializeData&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;messages.svclog&quot;&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;listeners&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;sources&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;  &lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;system.diagnostics&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms732023.aspx&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/ms732023.aspx&quot;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br/&gt;&lt;br /&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2faspnetfix.blogspot.com%2f2010%2f04%2fsvc-logging.html&quot;&gt;&lt;img src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2faspnetfix.blogspot.com%2f2010%2f04%2fsvc-logging.html&quot; border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; /&gt;&lt;/a&gt;</description><link>http://aspnetfix.blogspot.com/2010/04/svc-logging.html</link><author>noreply@blogger.com (lt)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5967051795922795442.post-70497847259929630</guid><pubDate>Thu, 03 Sep 2009 03:18:00 +0000</pubDate><atom:updated>2009-09-03T06:47:46.502+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ajax</category><category domain="http://www.blogger.com/atom/ns#">render user control</category><category domain="http://www.blogger.com/atom/ns#">web service</category><title>Render web user control in a web service</title><description>&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;[WebMethod(EnableSession = &lt;span class=&quot;kwrd&quot;&gt;true&lt;/span&gt;)]&lt;br /&gt;[ScriptMethod]&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; RenderControl()&lt;br /&gt;{&lt;br /&gt;            StringWriter tw = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; StringWriter();&lt;br /&gt;            HtmlTextWriter hw = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; HtmlTextWriter(tw);&lt;br /&gt;            &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; searchResultHtml = &lt;span class=&quot;str&quot;&gt;&quot;&quot;&lt;/span&gt;;&lt;br /&gt;            Page page = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; Page();&lt;br /&gt;            ScriptManager sm = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ScriptManager();&lt;br /&gt;            HtmlForm form = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; HtmlForm();&lt;br /&gt;            HtmlGenericControl div = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; HtmlGenericControl(&lt;span class=&quot;str&quot;&gt;&quot;UserControlDiv_DisplayResults&quot;&lt;/span&gt;);&lt;br /&gt;            ucDemoControl userControl = (ucDemoControl)page.LoadControl(&lt;span class=&quot;str&quot;&gt;&quot;~/Controls/ucDemoControl.ascx&quot;&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;            &lt;span class=&quot;rem&quot;&gt;//init controlm properties like datasource or other&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            userControl.DataSource = &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;; &lt;span class=&quot;rem&quot;&gt;//add something here&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;            page.Controls.Add(form);&lt;br /&gt;            form.Controls.Add(sm);&lt;br /&gt;            form.Controls.Add(div);&lt;br /&gt;            div.Controls.Add(userControl);&lt;br /&gt;            HttpContext.Current.Server.Execute(page, tw, &lt;span class=&quot;kwrd&quot;&gt;false&lt;/span&gt;);&lt;br /&gt;       &lt;br /&gt;            searchResultHtml = tw.ToString().Substring(tw.ToString().IndexOf(&lt;span class=&quot;str&quot;&gt;&quot;&amp;lt;UserControlDiv_DisplayResults&amp;gt;&quot;&lt;/span&gt;) + 30);&lt;br /&gt;            searchResultHtml = searchResultHtml.Substring(1, searchResultHtml.IndexOf(&lt;span class=&quot;str&quot;&gt;&quot;&amp;lt;/UserControlDiv_DisplayResults&amp;gt;&quot;&lt;/span&gt;) - 1);&lt;br /&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; searchResultHtml;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2faspnetfix.blogspot.com%2f2009%2f09%2frender-web-user-control-in-web-service.html&quot;&gt;&lt;img src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2faspnetfix.blogspot.com%2f2009%2f09%2frender-web-user-control-in-web-service.html&quot; border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; /&gt;&lt;/a&gt;</description><link>http://aspnetfix.blogspot.com/2009/09/render-web-user-control-in-web-service.html</link><author>noreply@blogger.com (lt)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5967051795922795442.post-19616607598704699</guid><pubDate>Mon, 31 Mar 2008 14:18:00 +0000</pubDate><atom:updated>2008-03-31T18:01:20.889+03:00</atom:updated><title>Boosting performance on aspx pages</title><description>Most of internet tutorials tells you to use Page Load event to bind your data to controls. What&#39;s happening when you have to rebind controls again because your data source is changed due to some event where you have to re bind controls.&lt;br /&gt; Well, the answer is: use OnPrender event for binding your control.&lt;br /&gt; Let&#39;s presume we have the following situation: we have a page where we have a list with a  lot of items(say for example products) and a button where we add products&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;asp:TextBox&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;ID&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;txtProduct&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;runat&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;server&quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;asp:TextBox&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;asp:Button&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;ID&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;btnAdd&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;runat&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;server&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;Text&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;Add product&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;Width&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;57px&quot;&lt;/span&gt;&lt;br /&gt;   &lt;span class=&quot;attr&quot;&gt;onclick&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;btnAdd_Click&quot;&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;br&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;asp:ListBox&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;ID&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;lstProducts&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;runat&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;server&quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;asp:ListBox&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;The usual way(without optimization) of doing yhis would be like this:&lt;br /&gt;1. We use session to keep products list read from the database as optimization(not to read products on page load each time)&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;List&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt; Products&lt;br /&gt;{&lt;br /&gt;  get&lt;br /&gt;  {&lt;br /&gt;      &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (Session[&lt;span class=&quot;str&quot;&gt;&quot;Products&quot;&lt;/span&gt;] != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;      {&lt;br /&gt;          &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; (List&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;)Session[&lt;span class=&quot;str&quot;&gt;&quot;Products&quot;&lt;/span&gt;];&lt;br /&gt;      }&lt;br /&gt;      &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;  set&lt;br /&gt;  {&lt;br /&gt;      Session[&lt;span class=&quot;str&quot;&gt;&quot;Products&quot;&lt;/span&gt;] = &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt;;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;Here comes the page load event (our first binding)&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Page_Load(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;   &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (!Page.IsPostBack)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class=&quot;rem&quot;&gt;//here we read products form database or whereever we need&lt;/span&gt;&lt;br /&gt;       &lt;span class=&quot;rem&quot;&gt;//I&#39;ll just pun some dummy products in here&lt;/span&gt;&lt;br /&gt;       Products = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;       &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 20; i++)&lt;br /&gt;       {&lt;br /&gt;           Products.Add(&lt;span class=&quot;str&quot;&gt;&quot;Product &quot;&lt;/span&gt; + i.ToString());&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;   &lt;span class=&quot;rem&quot;&gt;//Binding No 1&lt;/span&gt;&lt;br /&gt;   &lt;span class=&quot;rem&quot;&gt;//here we bind products to the list&lt;/span&gt;&lt;br /&gt;   lstProducts.DataSource = Products;&lt;br /&gt;   lstProducts.DataBind();&lt;/pre&gt;Now suppose we click Add product, let&#39;s see what happens:&lt;br /&gt;1. Page do a page load ad do the binding once.&lt;br /&gt;2. Page execute click event, add the new product to the list then do a rebind of the list of products&lt;br /&gt;In conclusion we have 2 bindings.&lt;br /&gt;&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; btnAdd_Click(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;//here we add the new product form textbox on Add event&lt;/span&gt;&lt;br /&gt;    Products.Add(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtProduct.Text);&lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;//our product list is changed so we have to rebind list control&lt;/span&gt;&lt;br /&gt;    &lt;span class=&quot;rem&quot;&gt;//Binding No 2&lt;/span&gt;&lt;br /&gt;    lstProducts.DataSource = Products;&lt;br /&gt;    lstProducts.DataBind();&lt;br /&gt;}&lt;/pre&gt;Well, imagine you have a page with a lot of controls, and some of you controls events must force you to rebind a lot. Imagine how this can affect page loading performance.&lt;br /&gt;&lt;br /&gt;Now, let&#39;s see the optimized way of doing some data binding.&lt;br /&gt;1.First we see our new page load method without data bind. Here we only read products first time a page is loaded.&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Page_Load(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt; {&lt;br /&gt;     &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (!Page.IsPostBack)&lt;br /&gt;     {&lt;br /&gt;         &lt;span class=&quot;rem&quot;&gt;//here we read products form database or whereever we need&lt;/span&gt;&lt;br /&gt;         &lt;span class=&quot;rem&quot;&gt;//I&#39;ll just pun some dummy products in here&lt;/span&gt;&lt;br /&gt;         Products = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; List&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt;&amp;gt;();&lt;br /&gt;         &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0; i &amp;lt; 20; i++)&lt;br /&gt;         {&lt;br /&gt;             Products.Add(&lt;span class=&quot;str&quot;&gt;&quot;Product &quot;&lt;/span&gt; + i.ToString());&lt;br /&gt;         }&lt;br /&gt;     }    &lt;br /&gt; }&lt;/pre&gt;You can see no databinding here.&lt;br /&gt;&lt;br /&gt;2.Second we change the button click&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; btnAdd_Click(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;  {&lt;br /&gt;      &lt;span class=&quot;rem&quot;&gt;//Here we only add product to the list&lt;/span&gt;&lt;br /&gt;      &lt;span class=&quot;rem&quot;&gt;//here we add the new product form textbox on Add event&lt;/span&gt;&lt;br /&gt;      Products.Add(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtProduct.Text);      &lt;br /&gt;  }&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;3. Third and last thing is to implement binding in OnPrerender event:&lt;br /&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;  &lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnPreRender(EventArgs e)&lt;br /&gt;  {&lt;br /&gt;      &lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;.OnPreRender(e);&lt;br /&gt;      &lt;span class=&quot;rem&quot;&gt;//Here is the only time when we do the binding&lt;/span&gt;&lt;br /&gt;      lstProducts.DataSource = Products;&lt;br /&gt;      lstProducts.DataBind();&lt;br /&gt;  }&lt;/pre&gt;Well, you can see binding is done only once in page. This is only a demonstrative case, but imagine your page have a lot of controls to bind, what happens then?&lt;br /&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2faspnetfix.blogspot.com%2f2008%2f03%2fboosting-performance-on-aspx-pages.html&quot;&gt;&lt;img src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2faspnetfix.blogspot.com%2f2008%2f03%2fboosting-performance-on-aspx-pages.html&quot; border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; /&gt;&lt;/a&gt;</description><link>http://aspnetfix.blogspot.com/2008/03/boosting-performance-on-aspx-pages.html</link><author>noreply@blogger.com (lt)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5967051795922795442.post-95765143975649211</guid><pubDate>Sat, 29 Mar 2008 06:49:00 +0000</pubDate><atom:updated>2009-08-19T14:57:47.877+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp data bind</category><category domain="http://www.blogger.com/atom/ns#">asp net user control design pattern</category><title>Web user controls design pattern and data binding part 1</title><description>A lot of people are using &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;user controls&lt;/span&gt; in asp because they are very easy to use, have design time support and so on.&lt;br /&gt;Anyway, because this controls are like a web page that you can add it in every page you want, you tend to do some mistakes and forget about &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;user control design line&lt;/span&gt;. Most common mistakes are:&lt;br /&gt;&lt;ol&gt;&lt;li&gt;put logic functionality into control&lt;/li&gt;&lt;li&gt;forget about implementing events, and use basic control events like button click, dropdown SelectedIndexChanged, etc.&lt;/li&gt;&lt;li&gt;not treat it as a control, implementing a data source, and a data bind mechanism&lt;/li&gt;&lt;/ol&gt;You will get into a lot of trouble , especially when page will be very complex and of course you will loose the flow of this page, spend many hours of debug to patch problems of logic and so on.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;Whats the plan?&lt;/span&gt;&lt;br /&gt;The purpose of this article is to &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;design a user control&lt;/span&gt; for editing a user details object, save data, cancel editing or deleting the user object.&lt;br /&gt;You can download web site &lt;a href=&quot;http://www.laur77.3x.ro/download/download.html&quot;&gt;here&lt;/a&gt; (use Save target as) or you can read on.&lt;br /&gt;Notice: this is a website with VS 2008 and framework 3.5. It&#39;ll not work on VS 2005&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Data Model&lt;/li&gt;&lt;/ul&gt;Let&#39;s say we have a user object defined as below&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;enum&lt;/span&gt; Gender&lt;br /&gt;{&lt;br /&gt;Female = 0,&lt;br /&gt;Male = 1&lt;br /&gt;}&lt;br /&gt;[Serializable()]&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; User&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; UserId { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; FirstName { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; LastName { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; MiddleName { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; Gender UserGender { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Phone { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; Address { get; set; }&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; Age { get; set; }&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; User()&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;span style=&quot;FONT-WEIGHT: bold;font-size:78%;&quot; &gt;Notice that we use an enum for user gender.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Well we will first design the control (let&#39;s say it&#39;s called UserDetails.ascs). I don&#39;t think it&#39;s a good idea to put code below, because it&#39;s just some long text with asp tags, so we got to code behind to implement some &lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;guidelines &lt;/span&gt;or this &lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;user control&lt;/span&gt;.&lt;br /&gt;Well in my opinion a control should have:&lt;br /&gt;&lt;ul&gt;&lt;ul&gt;&lt;li&gt;a datasource containing data to be displayed&lt;/li&gt;&lt;li&gt;a &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;binding method&lt;/span&gt; that fill control with fed data&lt;/li&gt;&lt;li&gt;an unbinding method so we can grab user changes back to datasource&lt;/li&gt;&lt;li&gt;some custom events for Save, Cancel, Delete to handle this actions in &lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;main page&lt;/span&gt; (not in user control as I said above)&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;Well, let&#39;s go to work.&lt;br /&gt;&lt;ol&gt;&lt;li&gt;Implement a datasource:&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; User DataSource&lt;br /&gt;{&lt;br /&gt;get&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (ViewState[&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.ClientID + &lt;span class=&quot;str&quot;&gt;&quot;USER&quot;&lt;/span&gt;] != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; (User)ViewState[&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.ClientID + &lt;span class=&quot;str&quot;&gt;&quot;USER&quot;&lt;/span&gt;];&lt;br /&gt;}&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;set&lt;br /&gt;{&lt;br /&gt;ViewState[&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.ClientID + &lt;span class=&quot;str&quot;&gt;&quot;USER&quot;&lt;/span&gt;] = &lt;span class=&quot;kwrd&quot;&gt;value&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;2. Implement a binding method&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;/// Populate controls with user values&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; DataBind()&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//call base method&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;.DataBind();&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; Fill controls&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//populate dropdown with options for gender&lt;/span&gt;&lt;br /&gt;ddlGender.Items.Clear();&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;foreach&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;string&lt;/span&gt; enumName &lt;span class=&quot;kwrd&quot;&gt;in&lt;/span&gt; Enum.GetNames(&lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(Gender)))&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; enumValue = Convert.ToInt32( Enum.Parse(&lt;span class=&quot;kwrd&quot;&gt;typeof&lt;/span&gt;(Gender), enumName));&lt;br /&gt;ListItem li = &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; ListItem(enumName, enumValue.ToString());&lt;br /&gt;ddlGender.Items.Add(li);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;preproc&quot;&gt;#region&lt;/span&gt; Bind Controls&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtFirstName.Text = DataSource.FirstName;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtMiddleName.Text = DataSource.MiddleName;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtLastName.Text = DataSource.LastName;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtAge.Text = DataSource.Age.ToString();&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.ddlGender.SelectedValue = ((&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;)DataSource.UserGender).ToString();&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtAddress.Text = DataSource.Address;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtPhone.Text = DataSource.Phone;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;preproc&quot;&gt;#endregion&lt;/span&gt;&lt;br /&gt;}&lt;/pre&gt;As you can see we set control values with our DataSource(User object) values in here so we decide when we want some binding in the main page.&lt;br /&gt;&lt;br /&gt;3. Implement an &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;Unbind method&lt;/span&gt;&lt;br /&gt;You can see here we set the control values changed by the user to our DataSource and we decide in the main page if we want to do the saving(save the control changed data source) to some other object, database, etc.&lt;br /&gt;&lt;span style=&quot;font-size:78%;&quot;&gt;&lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;Notice: you can make this method public an call it from the main page also.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;/// Binds changed values from controls to DataSource(user object)&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; UnBind()&lt;br /&gt;{&lt;br /&gt;DataSource.FirstName = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtFirstName.Text;&lt;br /&gt;DataSource.MiddleName = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtMiddleName.Text;&lt;br /&gt;DataSource.LastName = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtLastName.Text;&lt;br /&gt;DataSource.Age = Int32.Parse( &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtAge.Text);&lt;br /&gt;DataSource.UserGender = (Gender)Int32.Parse(&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.ddlGender.SelectedValue);&lt;br /&gt;DataSource.Address = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtAddress.Text;&lt;br /&gt;DataSource.Phone = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.txtPhone.Text;&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//add user Id as command argument if needed in parent page&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.btnCancel.CommandArgument = DataSource.UserId.ToString();&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.btnDeleteUser.CommandArgument = DataSource.UserId.ToString();&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.btnSaveUser.CommandArgument = DataSource.UserId.ToString();&lt;br /&gt;}&lt;/pre&gt;4. Implement some custom events.&lt;br /&gt;As I said before this custom events will help you do the job in the parent page, where control is placed so let&#39;s implement some basic events this controls needs:&lt;br /&gt;&lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;Declaring events&lt;/span&gt; is very simple:&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;event&lt;/span&gt; EventHandler SaveUser;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;event&lt;/span&gt; EventHandler DeleteUser;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;event&lt;/span&gt; EventHandler CancelEdit;&lt;/pre&gt;and implementing them is also not hard to do:&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnSaveUser(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.SaveUser != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;SaveUser(sender, e);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnDeleteUser(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.DeleteUser != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;DeleteUser(sender, e);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnCancelEdit(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.CancelEdit != &lt;span class=&quot;kwrd&quot;&gt;null&lt;/span&gt;)&lt;br /&gt;{&lt;br /&gt;CancelEdit(sender, e);&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;Now we just have to call this defined events on button click events (for btnSave, btnDelete, btnCancel):&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; btnSaveUser_Click(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//bind back values&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UnBind();&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//we treat saving event in the parent page not in control&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.OnSaveUser(sender, e);&lt;br /&gt;}&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; btnDeleteUser_Click(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//we treat saving event in the parent page not in control&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.OnDeleteUser(sender, e);&lt;br /&gt;}&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; btnCancel_Click(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//we treat saving event in the parent page not in control&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.OnCancelEdit(sender, e);&lt;br /&gt;}&lt;/pre&gt;Please notice in Save click method I call Unbind to set DataSource with values user canged in textboxes, dropdowns, etc.&lt;br /&gt;&lt;br /&gt;As you can see this is not hard to do, sometimes missing small details makes your pages hard to follow by others, or event by yourself after a while.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;Using the control&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Using the control in the age will be very easy job for us now. In my code I also added some status labels so you can see what&#39;s happening and some status values for User properties on the page.&lt;br /&gt;I&#39;ll skip asp markup and go to the code behind.&lt;br /&gt;&lt;br /&gt;Steps to do here:&lt;br /&gt;&lt;br /&gt;1. Declare user control events in OnInit method:&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnInit(EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;.OnInit(e);&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.SaveUser += &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; EventHandler(UserDetails1_SaveUser);&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.DeleteUser += &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; EventHandler(UserDetails1_DeleteUser);&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.CancelEdit += &lt;span class=&quot;kwrd&quot;&gt;new&lt;/span&gt; EventHandler(UserDetails1_CancelEdit);&lt;br /&gt;}&lt;/pre&gt;2. DataSource and DataBind&lt;br /&gt;We first have to initialize the control with some data, the do the &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;binding&lt;/span&gt;.&lt;br /&gt;Suppose we have and object in the main page called &lt;span style=&quot;FONT-WEIGHT: bold&quot;&gt;SelectedUser&lt;/span&gt;, we obtained form database or whatever. All we have to to is:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;override&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; OnPreRender(EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;base&lt;/span&gt;.OnPreRender(e);&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.DataSource = SelectedUser;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.DataBind();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;You notice here I put this code OnPreRender method, this is because this is executed after all page user defined events like Save, Cancel, Delete. I did this because I don&#39;t want to call this code twice : page load and then user handled event. This way we only bind control once after all custom logic is executed.&lt;br /&gt;&lt;br /&gt;3. &lt;span style=&quot;COLOR: rgb(255,102,0); FONT-WEIGHT: bold&quot;&gt;Handle events&lt;/span&gt; in page.&lt;br /&gt;We have only one step, decide what to do when user save, delete or cancel changes to this User control.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; UserDetails1_CancelEdit(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//we cancel changes by databind control again with initial values&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.DataSource = SelectedUser;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.DataBind();&lt;br /&gt;&lt;br /&gt;lblAction.Text = &lt;span class=&quot;str&quot;&gt;&quot;Cancel Edit&quot;&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; UserDetails1_DeleteUser(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//do you delete logic here&lt;/span&gt;&lt;br /&gt;lblAction.Text = &lt;span class=&quot;str&quot;&gt;&quot;Delete User&quot;&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; UserDetails1_SaveUser(&lt;span class=&quot;kwrd&quot;&gt;object&lt;/span&gt; sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;&lt;span class=&quot;rem&quot;&gt;//set changed user value in user control to page User object&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.SelectedUser = &lt;span class=&quot;kwrd&quot;&gt;this&lt;/span&gt;.UserDetails1.DataSource;&lt;br /&gt;lblAction.Text = &lt;span class=&quot;str&quot;&gt;&quot;Save User&quot;&lt;/span&gt;;&lt;br /&gt;}&lt;/pre&gt;Well, I hope you enjoyed this reading.&lt;br /&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2faspnetfix.blogspot.com%2f2008%2f03%2fweb-user-controls-design-pattern-and.html&quot;&gt;&lt;img border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2faspnetfix.blogspot.com%2f2008%2f03%2fweb-user-controls-design-pattern-and.html&quot; /&gt;&lt;/a&gt;</description><link>http://aspnetfix.blogspot.com/2008/03/web-user-controls-design-pattern-and.html</link><author>noreply@blogger.com (lt)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-5967051795922795442.post-2530793210018873359</guid><pubDate>Fri, 28 Mar 2008 08:10:00 +0000</pubDate><atom:updated>2008-10-20T10:43:00.898+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">asp data bind</category><category domain="http://www.blogger.com/atom/ns#">bind Dictionary</category><category domain="http://www.blogger.com/atom/ns#">bind List</category><category domain="http://www.blogger.com/atom/ns#">wwDataBind</category><title>ASP wwDataBinder with dictionaries and lists</title><description>&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_0&quot;&gt;Hello&lt;/span&gt; &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_1&quot;&gt;people&lt;/span&gt;,&lt;br /&gt;&lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_2&quot;&gt;This&lt;/span&gt; &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_3&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_4&quot;&gt;my&lt;/span&gt; &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_5&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_6&quot;&gt;article&lt;/span&gt; of &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_7&quot;&gt;my&lt;/span&gt; blog &lt;span class=&quot;blsp-spelling-error&quot; id=&quot;SPELLING_ERROR_8&quot;&gt;so I hope you find this helpful.&lt;br /&gt;&lt;/span&gt;How many of you used &lt;span style=&quot;FONT-WEIGHT: bold; COLOR: rgb(255,102,0)&quot;&gt;wwDataBinder&lt;/span&gt; extender from &lt;a href=&quot;http://msdn2.microsoft.com/en-us/magazine/cc163505.aspx&quot;&gt;MSDN&lt;/a&gt; in asp pages . If you did, maybe you wanted to &lt;span style=&quot;FONT-WEIGHT: bold; COLOR: rgb(255,102,0)&quot;&gt;bind&lt;/span&gt; some &lt;span style=&quot;FONT-WEIGHT: bold; COLOR: rgb(255,102,0)&quot;&gt;lists&lt;/span&gt; or&lt;span style=&quot;FONT-WEIGHT: bold; COLOR: rgb(255,102,0)&quot;&gt; dictionaries&lt;/span&gt; and had a surprise, it does not work.&lt;br /&gt;I changed extender a little bit so you can use lists and dictionaries.&lt;br /&gt;This can be a lot of help if you&#39;re using lists or dictionaries in your data model.&lt;br /&gt;Well, you can download it from here as &lt;a href=&quot;http://rapidshare.com/files/155737874/Debug.zip.html&quot;&gt;compiled version&lt;/a&gt; or &lt;a href=&quot;http://rapidshare.com/files/155737873/Databinding.zip.html&quot;&gt;source code&lt;/a&gt; (use Save target as).&lt;br /&gt;&lt;br /&gt;Using is very simple:&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;ww:wwDataBindingItem&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;runat&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;server&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;BindingMode&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;TwoWay&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;DisplayFormat&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;{0:00.00}&quot;&lt;/span&gt;&lt;br /&gt;   &lt;span class=&quot;attr&quot;&gt;BindingSource&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;this&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;ControlId&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;TextBox33&quot;&lt;/span&gt; &lt;span class=&quot;attr&quot;&gt;BindingSourceMember&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;=&quot;SomeDictionary[1]&quot;&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;html&quot;&gt;ww:wwDataBindingItem&lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;&amp;gt;&lt;/span&gt;    &lt;/pre&gt;&lt;br /&gt;I will come with another article regarding using this extender for those who does not know about this.&lt;br /&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2faspnetfix.blogspot.com%2f2008%2f03%2fwwdatabinder-with-dictionary-and-lists.html&quot;&gt;&lt;img alt=&quot;kick it on DotNetKicks.com&quot; src=&quot;http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2faspnetfix.blogspot.com%2f2008%2f03%2fwwdatabinder-with-dictionary-and-lists.html&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;</description><link>http://aspnetfix.blogspot.com/2008/03/wwdatabinder-with-dictionary-and-lists.html</link><author>noreply@blogger.com (lt)</author><thr:total>5</thr:total></item></channel></rss>