Thursday, September 18, 2008

ASP.Net - Tip

Do you have a user control or a custom server control that you use in all your pages? Did you know that instead of registering the control on each and every page using:

<%@ Register TagPrefix="ucYouth" TagName="YouthInfo" Src="~/controls/YouthInfoUserControl.ascx" %>

You can register the controls in your web.config.

Under <Configuration><System.Web> add the following sections to register your controls site wide.

<pages>
    <controls>
        <!--Register custom user controls-->
        <add tagPrefix="tgp1" tagName="tagName" src="~/controls/myFirstUserControl.ascx"/>
        <!--Register custom server controls --assembly name is without extension (.dll)-->
        <add tagPrefix="tgp2" assembly="assemblyName" namespace="assemblyNameSpace"/>
    </controls>
</pages>

You can now use any of the controls in any page in your site - without having to register them on the page.

ASP.Net - Custom Controls - embedded images not displaying

Problem: Briefly: You have written a custom control which uses an embedded resource image, which does not show up on the ASP page.

Indepth: You have created a custom ASP.Net control which uses images. You have included images in your project and you have set them to be embedded resources.

image 

And you load the images using "GetWebResourceURL". Like so - "Page.ClientScript.GetWebResourceUrl(this.GetType(), "ProjectName.FileName.ext");

You also verified that the method returns what looks like a valid url. (WebResource.axd? followed by a long string of characters). But you end up with the invalid image icon.

image

You check the dll in ILDASM and the resource is there.

image 

And yet - it does not show up on your web page.

You look through the project properties and you scratch your head and you scratch your head some more - and you still can't make it work.

Solution: Here is what you need to do to make it work: It is called the "WebResourceAttribute" and the documentation says that it "Defines the metadata attribute that enables an embedded resource in an assembly."

So add following line in the AssemblyInfo.cs file. (One for each image that should be served from your dll).

[assembly: WebResource("ProjectName.ImageName.gif", "image/gif")]

The second attribute is the "content-type". (Valid values for this can be found at - http://www.iangraham.org/books/html4ed/appb/mimetype.html)

That is it! You should now get to see the image in your control. image

AJAX enabling an existing ASP.Net WebSite

When I wanted to use AJAX in an existing ASP.Net website - I had no idea about how I would go about doing this. Turns out it is pretty simple.

The first thing you need to do is install Microsoft ASP.NET AJAX 1.0. Also if you wish you can install the MS AJAX Control Toolkit. (version for VS 2005 - release 11119)

The main part that you need to fix is your existing web.config file so that it includes a reference to the System.Web.Extensions dll - which is essential for ASP AJAX. The other changes are needed to configure AJAX as well as tell the ASP engine how to handle incoming requests (HTTPHandlers and HTTPModules sections).

Here we go.....

Add the following code into the Web.Config file inside the <configSections> section. (If you dont have one - you need to create it under the section of the <configuration> web.config.[1] (The number represents where in the entire body of the web.config the section should appear - see the last code section below).

<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
    <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
            <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
            <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
    </sectionGroup>
</sectionGroup>

Next under <assemblies> section add the following code: (the assemblies section appears under <configuration><compilation> section).[2]

<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

Next add the following HTTPHandlers[3]  and HTTPModules[4] section under the section <system.web>.

<httpHandlers>
      <add verb="GET" path="CrystalImageHandler.aspx" type="CrystalDecisions.Web.CrystalImageHandler, CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"/>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

Finally add the following <system.webServer> section under the <configuration> section.[5]

<system.webServer>
   <validation validateIntegratedModeConfiguration="false"/>
   <modules>
     <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
   </modules>
   <handlers>
     <remove name="WebServiceHandlerFactory-Integrated" />
     <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
          type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
     <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
          type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
     <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
   </handlers>
 </system.webServer>

That is it - you will now be able to use MS AJAX in your existing ASP website project.

Here is the basic skeleton of the Web.Config file into which the above pieces need to be plugged in. (I figured that you might not have all the sections that I have talked about above and this will show you where they go under the configuration item).

<?xml version="1.0"?> <configuration> <configSections> <!--Add Section Group Here-->[1] </configSections> <system.web> <compilation debug="false"> <assemblies> <!--Add assembly reference to System.Web.Extensions here-->[2] </assemblies> </compilation>

 

<httpHandlers> <!--Add HTTP Handlers here-->[3] </httpHandlers> <httpModules> <!--Add script module here-->[4] </httpModules> </system.web> <system.webServer> <!-- add system.webserver items here-->[5] </system.webServer> </configuration>

Wednesday, September 17, 2008

Tuesday, September 16, 2008

Anti-theft device to protect your lunch

moldy_bag_1

Do you want my moldy sandwich?

iPhone - Tips and Tricks

If you are running the latest version of the iPhone firmware (v2.1), then you can use the newest shortcut added to the iPhone headphone's mic/button.

A triple click will go to the previous song.

Here is a list of the other shortcuts available through the headphone's mic/button (link)

iphone-headset

A cheap .NET Micro development board

Shown in the video is the USBizi (pronounced USB Easy), development board controlling a robotic arm and also playing a MP3 file. At $99 a board - it is being touted as the cheapest board that runs the .NET micro system.

GHI Electronics, LLC. | USBizi Development System

The site also has a bunch of projects available for download. (Check out - http://www.ghielectronics.com/projects.php)

Monday, September 15, 2008

Music in the new iPod Ad - iPod Chromatic

As always - the Apple folk picked an awesome song for their ad. It is called Bruises and its by ChairLift. Hat-tip to the ad-maker.

Here is the full song from YouTube

And here is the ad:

Sunday, September 14, 2008

What a C# coder needs to know before diving into VB.Net

I am a hard-core C#/C++ developer. I had dabbled in VB a long-long time ago. Recently I had to work on a VB.Net and I found it pretty easy to work with the code. (The hardest part is to skip the semi-colons in VB).

Here is a great resource for any C# developer who needs to work with VB.Net.

What a C# Coder Should Know Before They Write VB

    • Always turn Strict, Explicit and probably Infer On.
    • Retrain the baby finger and ask on Connect for the IDE to remove EOL semi-colons

Onion Map - SimCity like maps of big cities from across the globe

Onion Map provides a 3D map of many of the large cities across the globe. Unlike Virtual Earth and Google Earth, Onion Map's display is not as realistic and has a very cartoonish look to it (almost looks like you are looking at a SimCity map). In my opinion - this is a good thing - as it differentiates this map service from the other 2 behemoths.

image

Not all the big cities are available on this website (Denver is sadly not). Such a map is great as an aid for tourists who wish to plan visits to a city as it removes all the clutter that comes with displaying Geo-Specific city models.

Onion map is less of a map service for mapping purposes as it is for research and learning more about a city, as well as using it to get information on businesses. It is almost an encyclopedia on a map.

image

Looks like it is a Korean company.