tag:blogger.com,1999:blog-40466516980164671022024-03-05T17:11:39.847+00:00Andrew Gunn's BlogTales of a half pint...Andrew Gunnhttp://www.blogger.com/profile/07687301186540182546noreply@blogger.comBlogger103125tag:blogger.com,1999:blog-4046651698016467102.post-55207046112607612502016-02-18T17:14:00.000+00:002016-02-18T17:14:07.123+00:00<a href="http://clkuk.tradedoubler.com/click?p=50662&a=1471893&g=16686336" target="_BLANK"><img border="0" src="http://impgb.tradedoubler.com/imp?type(img)g(16686336)a(1471893)" /></a>Andrew Gunnhttp://www.blogger.com/profile/07687301186540182546noreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-26373786294843751322009-08-05T13:24:00.000+01:002009-08-10T09:15:42.668+01:00Catch-all redirect in Apache<p>Assume you have a website running on an Apache server and the URL is http://mydomain.com/. What if you want to dominate the UK market too!? You'd probably buy mydomain.co.uk and point it to the same server. This is bad! It causes something called <strong><a href="http://www.mattcutts.com/blog/seo-advice-url-canonicalization/" title="Canonicalization">Canonicalization</a></strong> - web content that has more than one possible URL. This can lead to massive SEO penalties so my advice is avoid it at all costs.</p><p>Instead, have a single URL that serves the content and defer anything else. In this scenario, mycompany.com would be the master and the rest (e.g. mycompany.co.uk) would return a <a href="http://andrewgunn.blogspot.com/2008/04/http-status-codes.html" title="HTTP Status Codes">301 redirect</a> (not a 302). It's also worth mentioning that http://www.mycompany.com/ and http://mycompany.com/ are different (with and without the "www." prefix). If they both serve the same content, <strong><a href="http://www.mattcutts.com/blog/seo-advice-url-canonicalization/" title="Canonicalization">Canonicalization</a></strong> will occur. Choose one and stick with it.</p><p>The following can be put inside the httpd.conf file to allow a single URL access to a website and redirect the others:</p><pre class="brush: xml"><VirtualHost *:80><br /> ServerName mydomain.com<br /> DocumentRoot /var/www<br /></VirtualHost><br /> <br /><VirtualHost *:80><br /> ServerName default<br /> ServerAlias *<br /> Redirect 301 / http://mydomain.com/<br /></VirtualHost></pre><p>I'll also do a follow-up post to show how to do the same in IIS.</p>Andrew Gunnhttp://www.blogger.com/profile/07687301186540182546noreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-38146521585449567232009-07-22T12:05:00.001+01:002009-07-22T12:06:37.270+01:00Agile Development<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihVa7PQBuahbaw-A1kxEelYp4N7-SbNhbgsUcNmdCIrqK4AYKJBhB-4dcdvnt2D4KpMMYCDXhLqfiV3r75SrmsNYkq_z-ookw1nrQRASvV6IOMZIfi8HNdgUVX5FBcf4npGN0ElXc3EyE/s1600-h/Agile.gif"><img style="cursor:pointer; cursor:hand;width: 400px; height: 124px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihVa7PQBuahbaw-A1kxEelYp4N7-SbNhbgsUcNmdCIrqK4AYKJBhB-4dcdvnt2D4KpMMYCDXhLqfiV3r75SrmsNYkq_z-ookw1nrQRASvV6IOMZIfi8HNdgUVX5FBcf4npGN0ElXc3EyE/s400/Agile.gif" border="0" alt=""id="BLOGGER_PHOTO_ID_5361239272687757170" /></a>Andrew Gunnhttp://www.blogger.com/profile/07687301186540182546noreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-58373476498656009712009-07-15T11:28:00.001+01:002009-07-22T12:46:20.459+01:00SQL - Dynamic Order By clause<p>Have you ever wanted a stored procedure to order it's results dynamically? The ORDER BY clause is the place to start but normally relates to a series of columns that's hard-coded. If you want this clause to change depending on some input, you can adapt the following SQL. It contains a switch statement that compares the value of a parameter against pre-defined string column names. When a match is found, the respective clause is inserted. You can also specify the direction (ascending/descending) which doubles the amount of case options.</p><pre class="brush: sql">declare @orderBy nvarchar(max)<br />set @orderBy = 'columnName'<br /> <br />-- 0 Descending<br />-- 1 Acsending<br />declare @orderByDirection int<br />set @orderByDirection = 0<br /> <br />select *<br />from tableName<br />order by<br /> case when @orderBy = 'columnName' and @orderByDirection = 0<br /> then columnName end desc,<br /> case when @orderBy = 'columnName' and @orderByDirection = 1<br /> then columnName end</pre>Andrew Gunnhttp://www.blogger.com/profile/07687301186540182546noreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-17618368508580917022009-07-13T17:19:00.000+01:002009-07-22T12:54:12.655+01:00ASP.NET MVC - UserControls and IPrincipal<p>This is my first post on ASP.NET MVC so I'm gonna keep it short and sweet (I'm sure they'll be plenty more to come :-D).<br /></p><p>On a MVC View Page, you can access the user that has been previously authenticated with ease:</p><pre class="brush: xml"><%= User.Identity.Name %></pre><p>Unfortunately, you can't do the same in an MVC View User Control because they don't have a property called <span class="code">User</span>. Instead, you have to use the ViewContext property which is available on any MVC View:</p><pre class="brush: xml"><%= ViewContext.HttpContext.User.Identity.Name %></pre>Andrew Gunnhttp://www.blogger.com/profile/07687301186540182546noreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-43069899254718562712009-02-20T09:06:00.000+00:002009-07-22T12:49:32.424+01:00To serialize, or not to serialize - that is the question<p>When <a href="http://andrewgunn.blogspot.com/2008/06/xml-serialization-in-cnet.html">serializing</a> objects, you sometimes want to exclude properties that don't meet a certain criteria. This validation could be a simple check for default/null values or a more complicated scenario.</p><p>This example shows how to prevent a <strong>DateTime</strong> property being <a href="http://andrewgunn.blogspot.com/2008/06/xml-serialization-in-cnet.html">serialized</a> if it hasn't been set, meaning it's value will be <strong>DateTime.MinValue</strong>. You'll notice there is the property for date of birth and a method called ShouldSerializeDateOfBirth that has no parameters and returns a boolean value. If this returns true, serialize, and vice versa. How does it get called I hear you ask. I don't actually know exactly but the key to invoking is in the name - make sure it starts with "ShouldSerialize" followed by the name of the property (in this case, "DateOfBirth").</p><pre class="brush: csharp">public bool ShouldSerializeDateOfBirth() { return DateOfBirth != DateTime.MinValue; }<br /> <br />[XmlElement("dateofbirth")]<br />public DateTime DateOfBirth { get; set; }</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-22314371689152332572009-02-09T11:35:00.000+00:002009-07-22T14:23:00.649+01:00How do I create the ASPState database?<p>After installing the Microsoft .NET framework 2.0, launch a command window and run:</p><pre class="brush: plain">aspnet_regsql.exe -ssadd -sstype p -S {Server} -U {Username} -P {Password}</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-15059679002480307602009-01-30T15:08:00.000+00:002009-07-22T14:28:00.734+01:00CSS "or" separator<pre class="brush: xml"><div style="background-color: white; margin-left: -10px; margin-top: 40px; padding: 5px; position: absolute;">or</div><br /><div style="border-left: solid 1px #999; height: 8em; margin:auto; width: 1px;"></div></pre><p>Example:</p><div style="margin-left: 10px; width: 1px;"><div style="background-color: black; color: white; margin-left: -10px; margin-top: 40px; padding: 5px; position: absolute;">or</div><div style="border-left: solid 1px #999; height: 8em; margin:auto; width: 1px;"></div></div><p style="margin-top: 10px">* Copied from <a href="http://stackoverflow.com">Stack Overflow</a></p>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-52857712161419164382009-01-26T19:39:00.000+00:002009-01-26T19:42:56.207+00:000x80048820I recently kept getting an error after trying to sign into Windows Live Messenger or sync Windows Live Mail. My laptop had been unplugged at the mains for over a week and the date/time settings were scrambled. I noticed the time was wrong but I failed to spot the year was 2002. I simply changed it to 2009 and hey presto, problem solved.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-48635714250046468752009-01-16T11:25:00.000+00:002009-01-16T11:37:58.616+00:00How do I clean up my log files?Create a scheduled task that will execute the following command:<br /><br /><pre>C:\WINDOWS\system32\forfiles.exe /p C:\logs /s /m *.log /d -7 /c "CMD /C del @FILE"</pre><br />This example recursively deletes "*.log" files older than 7 days. If you simply wanted to move the files, change the comannd parameter - this provides a nice way to archive.<br /><br />For further information, see the <a href="http://technet.microsoft.com/en-us/library/cc753551.aspx">Forfiles</a> documentation.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-5113121361986804492009-01-09T15:09:00.001+00:002009-01-16T08:59:32.012+00:00Custom controls in ASP.NET MVCFinally, my article on custom controls in ASP.NET MVC has been published on CodeProject so check it out:<br /><br /><a href="http://www.codeproject.com/KB/custom-controls/MVCCustomControls.aspx">http://www.codeproject.com/KB/custom-controls/MVCCustomControls.aspx</a>Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-4046651698016467102.post-92107488004247610262009-01-08T09:34:00.000+00:002009-06-22T10:04:07.792+01:00jQuery watermark pluginHere is a jQuery plugin for adding a watermark effect to elements (e.g. text box):<br /><br /><pre>(function($) {<br /> // The element that has focus (or null when nothing has focus).<br /> var inputFocus = null;<br /> <br /> // This keeps track of the element that has focus.<br /> $(":input").focus(function() {<br /> inputFocus = this;<br /> }).blur(function() {<br /> inputFocus = null;<br /> });<br /> <br /> $.fn.hasFocus = function() {<br /> return this.attr("id") == $(inputFocus).attr("id");<br /> }<br />})(jQuery);<br /> <br />(function($) {<br /> var watermarkDefaults = {<br /> watermarkedClass: "input-watermarked",<br /> watermarkedText: ""<br /> };<br /> <br /> $.fn.watermark = function(settings) {<br /> settings = $.extend({}, watermarkDefaults, settings);<br /> <br /> var self = this;<br /> <br /> self.addWatermark(settings);<br /> <br /> if (self.isWatermarked(settings)) {<br /> settings.watermarkedText = self.val();<br /> }<br /> <br /> self.focus(function() {<br /> self.removeWatermark(settings);<br /> });<br /> <br /> // This might be an expensive exeucution!?<br /> // Added to prevent strange behaviour when the user<br /> // sets the focus before everything has fully loaded.<br /> self.keydown(function() {<br /> self.removeWatermark(settings);<br /> });<br /> <br /> self.blur(function() {<br /> self.addWatermark(settings);<br /> });<br /> <br /> // Clear the watermark text when the form is submitted.<br /> self.parents("form:first").submit(function() {<br /> self.removeWatermark(settings);<br /> });<br /> }<br /> <br /> $.fn.addWatermark = function(settings) {<br /> settings = $.extend({}, watermarkDefaults, settings);<br /> <br /> if (!this.hasFocus() && (this.val().length === 0 this.val() === settings.watermarkedText)) {<br /> this.addClass(settings.watermarkedClass);<br /> <br /> this.val(settings.watermarkedText);<br /> }<br /> }<br /> <br /> $.fn.isWatermarked = function(settings) {<br /> settings = $.extend({}, watermarkDefaults, settings);<br /> <br /> return this.hasClass(settings.watermarkedClass)/* && this.val() === settings.watermarkedText*/;<br /> }<br /> <br /> $.fn.removeWatermark = function(settings) {<br /> settings = $.extend({}, watermarkDefaults, settings);<br /> <br /> if (this.isWatermarked(settings)) {<br /> this.removeClass(settings.watermarkedClass);<br /> <br /> this.val("");<br /> }<br /> }<br />})(jQuery);</pre><br />Examples:<br /><br /><pre>$(function(){<br /> $("#id1").watermark({<br /> watermarkedClass: "custom-input-watermarked",<br /> watermarkedText: "Click here..."<br /> });<br />});</pre><br /><pre>$(function(){<br /> $("#id2").watermark({<br /> watermarkedText: "Click here..."<br /> });<br />});</pre>Unknownnoreply@blogger.com3tag:blogger.com,1999:blog-4046651698016467102.post-72160247857826945072009-01-06T15:40:00.001+00:002009-01-06T15:40:46.386+00:00EventType clr20r3I was recently on call when I got alerted to a scheduled task that failed to run - the following error was added to the Event Viewer: <br /><br />EventType clr20r3, P1 MyApp.exe, P2 1.0.0.21357, P3 4935215a, P4 MyApp, P5 1.0.0.21357, P6 4935215a, P7 5, P8 1, P9 system.typeinitialization, P10 NIL<br /><br />The exe is executed on the first day of every month and we didn't have any problems the previous time. After doing some digging, I got the impression there wasn't a single cause as everyone had a different story. One possible explanation was unhandled exceptions but I knew the application hadn't been updated since it's last execution. What I failed to notice was a config file that had been updated - some required settings simply vanished. Running the exe through a command line interface returned the exception which highlighted the missing values.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-33494732126239800502009-01-05T13:31:00.000+00:002009-01-06T15:39:55.943+00:00jQuery $(document).ready()We all know that anything inside the $(document).ready() function will execute in between loading the DOM and content. Here is a syntax alternative for those who want to save a few characters:<br /><br /><pre>$(function(){<br /> // Do something.<br />});</pre><br />Vs<br /><br /><pre>$(document).ready(function() {<br /> // Do something.<br />});</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-30741903995209299642008-12-09T16:08:00.000+00:002008-12-09T16:44:33.364+00:00Xbox Live AvatarsThe Xbox 360 dashboard was given a facelift back in November 2008 and one of the new features is Avatars - like a "Mii", only better! I've customised my avatar which you can see right here on my blog. Microsoft have a series of pulic URLs which allow you to download pictures of your avatar (simply replace {gamer-tag} with you know what):<br /><br /><strong>Full body:</strong><br />http://avatar.xboxlive.com/avatar/{gamer-tag}/avatar-body.png<br /><br /><strong>Head shot:</strong><br />http://avatar.xboxlive.com/avatar/{gamer-tag}/avatarpic-l.png<br /><br /><strong>Head shot (small</strong><br />http://avatar.xboxlive.com/avatar/{gamer-tag}/avatarpic-s.png)<br /><br /><br />Here are my pictures:<br /><br /><a href="http://avatar.xboxlive.com/avatar/deadlode/avatar-body.png" target="_new">Full body</a><br /><a href="http://avatar.xboxlive.com/avatar/deadlode/avatarpic-l.png" target="_new">Head shot</a><br /><a href="http://avatar.xboxlive.com/avatar/deadlode/avatarpic-s.png" target="_new">Head shot (small)</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-14446086208603218252008-11-03T10:07:00.000+00:002008-12-08T09:15:16.561+00:00Equals, Equals Equals or Equals Equals Equals?In JavaScript, you often see conditional statements with 3 equals signs (===). I always thought it was a typo but it turns out to have a significant advantage over the traditional 2 equals signs (==). This post shows how to use 1, 2 or 3 equals signs effectively.<br /><br /><br /><strong>Assign and evaluate</strong><br /><pre> if ( x = y + z ) { alert( "true" ); }</pre><br />Assigns x to the sum of y and z and evaluates the result.<br /><br /><br /><strong>Compare (irrespective of data type)</strong><br /><pre> if ( x == y + z ) { alert( "true" ); }</pre><br />The result of the sum of y and z is compared to x where both sides must have the same value. <br /><br />Example:<br />x = "5"<br />y = 10<br />z = -5<br /><br />y + z = 5<br /><br />Using the above values, the condition will be considered to be true because the sum of y and z is converted to a string prior to the comparison. <br /><br /><br /><strong>Compare and match data types</strong><br /><pre> if ( x === y + z ) { alert( "true" ); }</pre><br />The result of the sum of y and z is compared to x where both sides must have the same value and be of the same data type.<br /><br /><br />To summarise, always use === when doing conditional statements unless you want to compare values of different data types.<br /><br /><br />* zero and an empty string are considered to be false and all other numbers and strings are considered to be true.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-79774413635907800062008-11-01T14:37:00.000+00:002008-12-09T16:20:31.890+00:00SQL Server compatability levelWhen you upgrade to SQL Server 2005, you may find that some SQL statements are no longer valid and produce errors. A typical example is when you have 2 tables under different databases with the same name (the schema is irrelevant).<br /><br />Assume that you have 2 databases, MyDatabase1 and MyDatabase2, each containing a table called MyTable. Both tables have a primary key called ID which are also foreign keys between Database1..MyTable and Database2..MyTable. The following SQL statement is valid under SQL Server 2000 but invalid under SQL Server 2005:<br /><br /><pre>use [MyDatabase1]<br /> <br />select * from [MyTable]<br />inner join [Database2]..[MyTable] on [MyTable].[ID] = [Database2]..[MyTable].[ID]</pre><br />SQL Server 2005 is more strict and won't allow you to reference MyTable of MyDatabase1 in this way, even though you've specified the default database (use [MyDatabase1]). Instead you'd have to change the statement:<br /><br /><pre>select * from [Database1]..[MyTable]<br />inner join [Database2]..[MyTable] on [Database1]..[MyTable].[ID] = [Database2]..[MyTable].[ID]</pre><br />Alternatively, you can change the compatability level on the nececssary database(s). This is a quick fix and should only be done if you can't easily modify the SQL code.<br /><br /><ol><li>Open Microsoft SQL Server Management Studio</li><li>Expand the 'Databases' node</li><li>Right click the database and choose 'Properties'</li><li>Go to 'Options' and change the 'Compatability level' accordingly</li></ol><br />The 3 compatability levels are:<br /><ul><li>SQL Server 7.0 (70)</li><li>SQL Server 2000 (80)</li><li>SQL Server 2005 (90)</li></ul>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-32817551447186632402008-09-25T11:22:00.000+01:002008-09-25T13:36:43.121+01:00iPhone ringtonesThe following guide is for creating custom iPhone ringtones using iTunes. Some instructions might be specfic to Windows but the same principles apply to other operating systems.<br /><ul><li>Create an AAC format copy of the original song (we'll call this x) - right click on the song and select "Create ACC version" (you might need to change your recording settings first)</li><br /><li>Change the start/finish times of x but make sure it's length doesn't exceed 40 seconds - right click on the song and select "Get Info", then go to the "Options" tab</li><br /><li>Create an AAC format copy of x - this version will be a lot smaller in size. (we'll call this y)</li><br /><li>Navigate to y's physical location (e.g. "C:\Music\Song.m4a") - right click on the song and select "Show in Windows Explorer"</li><br /><li>Rename the file and change it's extension to m4r (e.g. "Song.m4r") - right click on the file and select "Rename"</li><br /><li>Drag the file (or import) into iTunes and it should go into the Ringtones section - if you can't see a Ringtones section, go to Edit > Preferences > General and make sure the check box is selected for "Ringtones"</li><br /><li>Finally, sync your iPhone but make sure it has been setup to sync ringtones - click on the your iPhone (under "Devices") and go to the "Ringtones" tab</li></ul>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-4046651698016467102.post-65753311275086033402008-09-07T11:45:00.001+01:002008-09-25T13:50:39.540+01:00The hyperlink trailing slashWhen creating hyperlinks to a folder resource, always trail with a forward a slash.<br /><br /><pre>http://andrewgunn.blogspot.com/test (slower)<br /> <br />Vs<br /> <br />http://andrewgunn.blogspot.com/test/ (faster)</pre><br />Any reqeusts for the URL without a trailing slash will hit the server and a 301 redirect will be returned to the URL with a trailing slash. This is because the server thinks that "test" is a file and tries to locate it. After searching, it realises that the request was actually for a folder called "test"; therefore it's response informs the client of the correct URL (with a trailing slash).<br /><br />It makes sense to include the trailing slash to speed up your website and reduce the load on the server. It doesn't make that much difference for root domains but it's good practice anyway:<br /><br /><pre>http://andrewgunn.blogspot.com<br /> <br />Vs<br /> <br />http://andrewgunn.blogspot.com/</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-50238958096871506462008-09-07T11:33:00.000+01:002008-09-07T11:41:00.447+01:00The C# ?? (null coalescing) operatorThe ?? operator checks whether the value on the left side of the expression is null, and if so it returns an alternate value on the right side of the expression. If the value on the left side of the expression isn't null, it returns the original value.<br /><br />Example 1:<br /><pre>string input = null;<br />string output = input ?? "Input is null";<br /> <br />// Outcome: ouput == "Input is null".</pre><br /> <br />Example 2:<br /><pre>string input = "Hello World";<br />string output = input ?? "Input is null";<br /> <br />// Outcome: ouput == "Hello World".</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-53246159801375003562008-09-03T14:03:00.000+01:002008-09-25T13:53:57.499+01:00JavaScript - Anchor popupsIf JavaScript is disabled, the user will be redirected to whatever is in the href attribute in a new frame. If JavaScript is enabled, a new window will open with the same content but the parent window will stay at it's current location.<br /><br />JavaScript:<br /><pre>function Popup( url, height, width )<br />{<br /> var windowProperties = "toolbar = 0, scrollbars = 1, location = 0, statusbar = 0, menubar = 0, resizable = 1, width = " + width + ", height = " + height + ", left = 50, top = 50";<br /> <br /> return window.open( url, "", windowProperties );<br />}</pre><br /><br />HTML:<br /><pre><a href="http://www.google.com/" onclick="javascript:Popup( 'http://www.google.com/', 600, 600 ); return false;" target="_blank">Test</a></pre><br />The morale of the story is always have a backup for users with JavaScript disabled.Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-4046651698016467102.post-37192564488665193312008-08-30T16:43:00.000+01:002008-08-30T17:07:36.254+01:00ASP.NET LinkButton without JavaScriptIf you use ASP.NET LinkButton controls, the client browser must have JavaScript enabled. If JavsScript is disabled, clicking on the link won't cause a postback. To avoid this scenario, use standard Button controls and style them to look like hyperlinks.<br /><br />Example:<br /><pre><asp:Button runat="server" CssClass="HyperlinkButton" Text="Submit" /></pre><br />CSS:<br /><pre>.HyperlinkButton<br />{<br /> background: none;<br /> border: none;<br /> cursor: pointer;<br /> text-align: left;<br />}<br /> <br />.HyperlinkButton:hover<br />{<br /> text-decoration: underline;<br />}</pre>Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-4046651698016467102.post-52560193023369576662008-08-22T13:51:00.001+01:002008-08-22T13:51:36.719+01:00How do I add a HTTP module in ASP.NET?<pre><?xml version="1.0"?><br /><configuration><br /> <system.web><br /> <httpModules><br /> <add name="ModuleName" type="ClassName, AssembleyName" /><br /> </httpModules><br /> </system.web><br /></configuration></pre><br />Example:<br /><br /><pre><add name="URLRewriter" type="HTTPModules.URLRewriter, HTTPModules" /></pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-41658316606247364832008-08-22T13:48:00.000+01:002008-08-22T13:49:21.981+01:00How do I create controls at runtime in ASP.NETIf you want to create controls at runtime and render them, the LoadControl method should be used instead of doing a normal object declaration. This is the only way that child controls (within the control you want to create) will be instantiated. The LoadControl method accepts the virtual path to the control as a string (e.g. *.ascx file) or the type of control and it's arguements as an object array (pass in null if no arguements need to be supplied). The second overload can be used if the control is part of a class library.<br /><br />Although you're creating an object of the control type, I wouldn't recommend doing the following because null reference exceptions will be thrown when any child control is referenced:<br /><pre>MyControl myControl = new MyControl();</pre><br />These examples show how dynamically create controls correctly:<br /><br />Example 1:<br /><br /><pre>MyControl myControl = LoadControl( "MyControl.ascx" ) as MyControl;<br /> <br />if ( myControl != null )<br />{<br /> // Do something.<br />}</pre><br />Example 2:<br /><br /><pre>MyControl myControl = LoadControl( typeof( MyControl ), null ) as MyControl;<br /> <br />if ( myControl != null )<br />{<br /> // Do something.<br />}</pre><br />I normally have a PlaceHolder indicating the position where I want this dynamic content:<br /><br /><pre>phDynamicControls.Controls.Add( myControl );</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-4046651698016467102.post-69613259978931578462008-08-22T12:44:00.000+01:002008-09-07T11:46:44.780+01:00HTML labelsHTML labels can be placed next to form controls as an indication of what they represent so the user has an idea of their expecated value(s). They can also be linked to an individual control using the <b>for</b> attribute. This means that when the user clicks on the label, the focus of the page will be set to the respective control.<br /><pre><label for="name"><br /> Name<br /></label><br /><input type="textbox" name="name" id="name" /></pre><br />Clicking on the label will set the focus to the input box.<br /><br />Example:<br /><br /><label for="name">Name</label> <input type="textbox" name="name" id="name" />Unknownnoreply@blogger.com0