Sunday, July 30, 2006

Adding A .Net Command Prompt to Explorer context menu

In order to get access to ildasm or the csc (C# compiler) you need to run vsvars32.bat. It would be easier to point to a folder in explorer and right click and see a ".Net Cmd 2003" item and just select it. You can then access all the .Net tools for this version of the framework.

I have a reg file I use to set this up. You can create a reg file with this content and double click on it or set the registry key by hand.

There is a 2003 and 2005 version (both can work at the same time)


---- 2003 / Version Begin Of Reg File
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\.NET Cmd 2003\command]
@="cmd.exe /K  \"C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\Tools\\vsvars32.bat\""

-- End Of RegFile


---- 2005 / Version Begin Of Reg File
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\.NET Cmd 2005 Beta 2\command]
@="cmd.exe /K  \"C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\Tools\\vsvars32.bat\""

-- End Of RegFile


See the all-new, redesigned Yahoo.com. Check it out.

Thursday, July 13, 2006

On the light side / Superman

I just saw the new Superman movie. Yes, I was a younger man when I saw the first one. I hesitated going to see this new version. Remakes are usually disappointing.

I was very pleased with this version. The themes are wellknown and the music is still the same good old one. Powerful special effects that do not obscure the story. A good moment.

BTW, Superman's cape is made of whool from France. I know you don't care but I had to brag a little


Sneak preview the all-new Yahoo.com. It's not radically different. Just radically better.

A validated check box in ASP.Net

As you probably know, there is no validator for a check box in ASP.Net. People go through hoops to make this work.

We just want to force the user to check the box before submitting. This is for scenarios like "click to accept agreement etc..."

The solutions I have found were not to my complete liking. So here is my solution to this annoying problem. It is very compact and involves very little javascript. It is a cool little trick. Have fun.


<%@ Control Language="C#" %>
<script runat="server">
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        _BoxMirror.Style.Add("display", "none"); // Hide the box. It is just there for the
        // validation logic to work.
        // Now hook up the check box to the text box. The text value mirrors the
        // value of the check box in sync.
        _Box.Attributes["onclick"] = "getElementById('" + _BoxMirror.ClientID + "').value=this.checked";
        _BoxMirror.Text = _Box.Checked.ToString().ToLower(); // Sync text box and check box
        // The validator Initial value is set to "false" so the Text Box will validate when
        // its value is different from "false".
    }
   
</script>

<div runat="server" id="_Wrapper" style="background-color:Yellow">
    <asp:TextBox runat="server" ID="_BoxMirror" />
    <asp:CheckBox runat="server" ID="_Box" />
    <asp:RequiredFieldValidator InitialValue="false"
    runat="server" Text="*" ID="_NeedToBeChecked" ControlToValidate="_BoxMirror" />
</div>


Sneak preview the all-new Yahoo.com. It's not radically different. Just radically better.

Saturday, July 01, 2006

AT&T's Big Brother Policy

Sony puts rootkits on their CD, now AT&T violates basic privacy

http://www.sfgate.com/cgi-bin/article.cgi?f=/c/a/2006/06/21/BUG9VJHB9C1.DTL&hw=at&sn=002&sc=870

Should we just throw our TVs, Phones, Computers and live on a deserted island to keep our
basic rights?

I have had bad experiences with AT&T so this is not surprising




An alternative to Page.RegisterClientScriptBlock

When you make a custom control that requires custom javascript you usually have to make sure that
you output the javascript code once and once only. If you don't, the same javascript is downloaded to the browser and waste bandwith. There are also issues with global variables declared multiple times etc..

ASP.Net provides a Page.RegisterClientScriptBlock that allows you to register a piece of code once and the framework will output it once on the page. This works fine and I have been using it a lot.
The issue with this is that you must generate the javascript in code like this.

protected override void OnInit(EventArgs e)
    {
      string strCode = @"
         "<script language='javascript'>"
        +
        "function ChangeImage(ImageRef)"
        +
        "{"
        +
            "ImageRef.src = 'http://www.google.com/images/firefox/fox1.gif'"
        +
        "}"
        +
        "</script>";

      Page.RegisterClientScriptBlock("SomeUniqueKeyOfYourChoosing",strCode);
    }

This works fine but it would be cool if I could still put this code in the ascx and still make sure it is output
once. The advantage is that it is easier to edit ascx inline than editing the C# code to craft the javascript.

I came up with this simple solution. The idea is this. We need to keep track of the fact that we have or have not output the javascript section already. ViewState is out of the question since it is scoped to each control. SessionState is also out of the question. SessionState should be use sparingly and we should stay away from it if we can. We just need to keep track of the javascript for this page only so we can use the Items collection that lives only for the duration of the request. After we output the page, we do not need to keep count so this is a good solution.

We are half way there. We now need to not output the javascript. Here is the trick. We will wrap the script with a <div> tag with runat="server". Inside the <div> is the javascript code! We just need to control the visibility of the div!

Here is the complete control code. It is just an image that changes when you click on it. We call
a javascript ChangeImage() function in javascript when the image is clicked.

Cool <div> isn't it?

<%@ Control Language="C#" ClassName="SimpleControl" %>
<asp:Image runat="server" ID="_Image" />

<script runat="server">
    void Page_Load(object s, EventArgs e)
    {
        _Image.ImageUrl = "http://www.google.com/images/logo_sm.gif";
    }
    string JavascriptKey
    {
        get
        {
            return this.GetType().FullName + "JS";
        }
    }

    void HandleJavascriptCodeStreaming()
    {
        HttpContext ctx = HttpContext.Current;

        bool? JavascriptAlreadyOutput = (bool?) ctx.Items[JavascriptKey];

        JavascriptCode.Visible = !JavascriptAlreadyOutput.HasValue;
        ctx.Items[JavascriptKey] = true;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        _Image.Attributes["onclick"] = "ChangeImage(this)";

        HandleJavascriptCodeStreaming();
    }
    
</script>

<div runat="server" id="JavascriptCode">
    <!-- We use the Div to Control The Visibility of the javascript code -->

    <script language="javascript">

        function ChangeImage(ImageRef)
        {
            ImageRef.src = "http://www.google.com/images/firefox/fox1.gif"
        }
    </script>

</div>