Tips and Trickshttp://www.geekzilla.co.uk/Innovation Team's dumping grounden-usTue, 10 Jun 2003 04:00:00 GMTTue, 10 Jun 2003 09:41:01 GMTGeekZilla.co.ukeditor@GeekZilla.co.ukwebmaster@GeekZilla.co.uk Nested Master Page Events not being called http://www.geekzilla.co.uk/viewE07AA87A-F88E-4570-A504-4FB5FED36F56.htm Nested Master Page Events not being called This is the second time I've been caught out by this little feature, so I thought I'd geekzilla it for both reference and to help others. Scenario I'm a big fan of MasterPages and Themes as a design principle for web development, and often you need to nest master pages to create complex repeatable layouts. The Feature In my child MasterPage I decided to put some code in the Page_Load to populate a related menu, and it wasn't being called....... very strange.. The Top level MasterPages' events were being called, but the child's weren't! Solution I'm not sure why it defaults to false but.. you need to change the AutoEventWireup property to '''true''' in the Master Directive in the MasterPage. If this has caught you out, here's the solution. 9/10/2008 http://www.geekzilla.co.uk/viewE07AA87A-F88E-4570-A504-4FB5FED36F56.htm Returning the first few items from an Array list or IEnumerable collection http://www.geekzilla.co.uk/view3B286690-73F9-41B2-A882-DDAD91647B7F.htm Returning the first few items from an Array list or IEnumerable collection, like T-Sql top() I needed to bind the first 5 items in a List<T> to a repeater. Having recently discovered the .Distinct() extension, I had a look through the other methods attached to my List<T> and found .Take(int) Basic example of using .Take() The output Only the first 5. Example of using .Take() when binding an IEnumerable object to a repeater You may also like to take a look at the .Skip(int) method. It can be used in conjunction with .Take() to help you with paging object data. 20/2/2008 http://www.geekzilla.co.uk/view3B286690-73F9-41B2-A882-DDAD91647B7F.htm Returning a Distinct list of values from an array or IEnumerable cllection http://www.geekzilla.co.uk/viewAC42E918-3A03-45B1-B8EE-2068ACC1DA1C.htm Returning a Distinct list of values from an array or IEnumerable collection I recently had a list of Guids in a string which was returned from a group of checkboxes with identical names. I wanted to itterate through Guids and process each one only once. Basicaly, ignoring duplicates. I didn't have to look very far before I discovered the .Distinct() method. See example below: The output? This triggered me to look at some of the other IEnumerable methods I'd ignored. My favourites so far are: {.Take(int)}http://www.geekzilla.co.uk/View3B286690-73F9-41B2-A882-DDAD91647B7F.htm .Skip(int) .Reverse() 20/2/2008 http://www.geekzilla.co.uk/viewAC42E918-3A03-45B1-B8EE-2068ACC1DA1C.htm Adding a Strong Name to an existing DLL that you don't have the source to http://www.geekzilla.co.uk/viewCE64BEF3-51A6-4F1C-90C9-6A76B015C9FB.htm Adding a Strong Name to an existing DLL that you don't have the source to There are times when you need a DLL to have a strong name; putting it in the GAC for example. With 3rd party DLL's this could be a pain. This is how you do it: From a VS.NET command prompt, enter the following: '''1. Generate a KeyFile''' '''2. Get the MSIL for the assembly''' '''3. Rename the original assembly, just in case''' '''4. Build a new assembly from the MSIL output and your KeyFile''' Where do these tools live If you've not got your framework and sdk paths properly mapped... In framework 2 this is where the command line utilities live: *C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\'''ilasm.exe''' *C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\'''ildasm.exe''' *C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\'''sn.exe''' 7/8/2007 http://www.geekzilla.co.uk/viewCE64BEF3-51A6-4F1C-90C9-6A76B015C9FB.htm Getting the ProviderUserKey for the Current User http://www.geekzilla.co.uk/view26C30C80-3868-45A5-A4B8-983630AAE20B.htm Getting the ProviderUserKey for the Current User The following gets the ProviderUserKey for the current user. #c#(Guid)Membership.GetUser().ProviderUserKey 24/7/2007 http://www.geekzilla.co.uk/view26C30C80-3868-45A5-A4B8-983630AAE20B.htm ASP.NET Framework 1.1 Validation not working in IE7? http://www.geekzilla.co.uk/viewFF2FB9D4-28F7-4289-BEB3-6EA081A003EB.htm ASP.NET Framework 1.1 Validation not working in IE7? I recently had a problem with an old '''Framework 1.1''' website where the form would not post back if the page contained validators (CustomValidators to be exact). It caused me to do a lot of googling and in the end some javascript debugging. It was a strange problem, the site worked fine in IE6 and Firefox; just IE7 failed. Searching the web I was suppried that nobody else was complaining about this.. surely it was a 1.1 IE7 bug?? First clue In the onSubmit attribute of the form tag there is a call to '''ValidatorOnSubmit()'''. When Alerting this in JavaScript it returned '''"Undefined"''' I checked that the scriptmaps were ok and even reinstalled them using '''ASPNET_REGIIS -s <path>'''. Still no dice. The solution It turned out that .net '''Framework 1.1 SP1''' had been installed on the server but this site had local copies of the pre SP1 '''WebUIValidation.js''' file. I replaced the files with the correct ones and things started to work... sort of.. Problems with '''pre SP1''' code and the CustomValidator control The validation now triggered. Slight problem, it always failed, without even doing the postback it needed to perform server-side validation. The solution was to add a dummy custom validator function to allow client validation to pass client-side checks before doing server-side validation. #h#<ASP:CUSTOMVALIDATOR ClientValidationFunction="customvalidatorDummy" ... /> 4/7/2007 http://www.geekzilla.co.uk/viewFF2FB9D4-28F7-4289-BEB3-6EA081A003EB.htm Need a high resolution timer? http://www.geekzilla.co.uk/view4D608741-7A43-4626-A959-8EC2D14C7193.htm Need a high resolution timer I like to profile my code regularly to see if any changes I make during development affect performance. In .Net 1.1 there is a distinct lack of a high resolution timer object. Here's one you can use: Call like this: 26/7/2006 http://www.geekzilla.co.uk/view4D608741-7A43-4626-A959-8EC2D14C7193.htm TraceListener creates huge log files http://www.geekzilla.co.uk/view2C5161FE-783B-4AB7-90EF-C249CB291746.htm TraceListener creates huge log files If you've used the .Net trace listeners, you'll know that they just keep appending to the file specified in the App.Config file, this can lead to some massive log files! Here's a simple TraceListener that logs to a new file for every day. You can configure this by amending the App.Config file: So on the day of this submission this would create a file in e:\logfiles called mylogfile_20060726.log. 26/7/2006 http://www.geekzilla.co.uk/view2C5161FE-783B-4AB7-90EF-C249CB291746.htm Missing App.Path http://www.geekzilla.co.uk/view539AC7FD-E30A-40B2-A427-7402A055BA4E.htm Missing App.Path If you have the need to load resources (XSL stylesheets, images etc etc) at runtime, you may need to know the path of the currently executing code. In VB6 you'd just use good old App.Path, here is the .Net equivalent. #c# string Location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); 26/7/2006 http://www.geekzilla.co.uk/view539AC7FD-E30A-40B2-A427-7402A055BA4E.htm Referencing ConfigurationManager http://www.geekzilla.co.uk/view36932421-B4B7-4948-BBDA-39567F616BBC.htm Referencing ConfigurationManager When I started working in VS2005, I assumed that accessing your AppSettings would be the same as it was in VS2003. On compiling, the IDE served up the following warning. 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings' Old way of accessing AppSettings New way of accessing AppSettings So I changed my code accordingly, but the project wouldn't compile. It turns out that you need to add a reference to System.Configuration to the project, rather than simply adding it with a using statement. This can be done as follows. Add a reference to System.Configuration by clicking Project -> Add Reference... and searching through the .NET components for System.Configuration 14/6/2006 http://www.geekzilla.co.uk/view36932421-B4B7-4948-BBDA-39567F616BBC.htm SQL Server 2005 mdf file Query String http://www.geekzilla.co.uk/viewCAD25576-8B87-4AC8-9F4E-54A1ED749D8E.htm SQL Server 2005 mdf file Query String To add a SQL Server 2005 local database to one of your projects, create the connection as follows. 14/6/2006 http://www.geekzilla.co.uk/viewCAD25576-8B87-4AC8-9F4E-54A1ED749D8E.htm Programmatically resolving ~ URL's to the Virtual Root using ResolveURL() http://www.geekzilla.co.uk/view35445C1B-E1DA-45C7-AFE5-934988BCBB72.htm Programmatically resolving ~ URL's to the Virtual Root using ResolveURL() It's common knowledge that a control, when '''Runat="server"''' will resolve it's src or href attribute to a virtual root when the URL starts with '''~/''' For example: will become: How do we do this programmatically? The base WebControl class exposes a method named ResolveURL(). This method accepts a url such as '''"~/Customers/Profile.aspx"''' and returns the real url, starting from your site's virtual root. To get to this functionality you can use the following pattern: ..or.. ..or.. 14/6/2006 http://www.geekzilla.co.uk/view35445C1B-E1DA-45C7-AFE5-934988BCBB72.htm