in

ASP.NET Weblogs

Joe Wrobel

February 2008 - Posts

  • ClientID Problem In External JavaScript Files Solved

    Well, at least for me it is.  I say that because this solution might
    not appeal to the masses, but it works great for me.

    The binary and source files can be downloaded from the MSDN Code Gallery.
    Here is the direct link.
    http://code.msdn.microsoft.com/RegClientControls

    Up until lately, I have been writing my JavaScript in the .aspx file.
    That way I could use the ClientID trick.  <%=TextBox1.ClientID%>
    I just began working with a team who prefers to write all JavaScript in
    external .js files.  What they had been doing is hard coding the
    ClientID prefixes caused by the container controls.  I guess this works
    fine.  It has been working for them so far.  I personally can't do this.
    It just feels wrong.

    I took some time to figure out a better way to deal with the ClientID in
    an external JavaScript file.  I found an interesting article
    here about
    Creating JavaScript objects from ASP.NET objects.  I liked David's technique,
    but it still required manually writing JavaScript on the .aspx page. I wrote
    a control called "RegClient" that encapsulates and automates this technique.
    If you place this control on your page anywhere below the Script Manager,
    then all you have to do to access the controls from JavaScript is something
    like "var controlObj = PageControls.TextBox1;".

    The RegClient control is dependant on Microsoft ASP.NET AJAX 1.0.  The
    source could easily be targeted to .NET 3.5 if you wanted.  I'm sure
    that with a little work it could even be library independent, but I
    didn't have that requirement.

    The RegClient control has two modes, "Marked" and "All".

    Marked:
    With the RegClient control set to Marked, only the controls marked with
    RegClient="true" or ClientControlID="{yourClientName}" will be registered.

    <Robo:RegClient ID="RegClient1" runat="server" ClientControls="Marked" />

    All:
    With the RegClient control set to All, every control in master
    and content pages will be registered unless RegClient="false".

    <Robo:RegClient ID="RegClient1" runat="server" ClientControls="All" />

    To access the controls in the external JavaScript file, handle the
    "ready" event of
    the "PageControls" object like shown here.
    PageControls.add_ready(function(){
       
    //Write your code here.
    });

    All of the controls that have been registered will be available from
    the "PageControls" object in JavaScript.  Here is an example of accessing
    the controls.

    PageControls.add_ready(function(){ 
        $addHandler($get('Button2'), 'click'
            function() { 
               
    PageControls.Label1.innerHTML = PageControls.tbText.value; 
           

       
    ); 
    });

    One caution I would make for using this control is to be careful when you
    have the ClientControls setting to "All".  This control will find and
    register every control that derives from WebControl.  If you're using
    a GridView or something similar on the page, then there could be many
    extra controls that you don't want to get registered.

    Here is a screen shot of the RegClient control.  There really is only one
    setting for this control, but I added a big blob of text as a reminder of
    how to use it with other controls.

    image

    When the page renders, this is what the generated output looks like at
    at the bottom of the page.

    image

    So, that's it.  I hope you find this useful and if you know of any ways
    this control could be improved, please let me know.

    Thanks
    -Joe

  • Web Profile Builder for Web Application Projects

    Files can be downloaded from the Web Profile Builder project page.

    If you use Web Application Projects, you have probably run into the issue of not being
    able to access the Profile at design time.

    Thankfully, some nice people created an Add-In for Visual Studio 2005 that will generate
    a wrapper class as a workaround.  That project can be found here.  I wanted to contribute
    to the Web Profile Generator project, but my emails went unanswered.  I decided to start
    a new project.

    This project picks up where that one left off and is based on their source code.  I started
    out rebuilding that project as it stood to add support for Visual Studio 2008.  I got it to
    work, but I wanted to do more. 

    I decided to go ahead and address the issues listed on the original project site.

    Here is a summary of the changes made.

    • First of all, this is no longer an Add-In.  Instead, it is a Build Task.
    • Works for Visual Studio 2005 and 2008.
    • Added ability to set the file name.
    • Added ability to set the directory the file gets created in.
    • Added ability to set the class name.
    • Added ability to set the name space.
    • Added an extra method as requested here.

    Usage:

    1. Run the installer.
    2. Add this Import statement to your project file.  (see special notes below)
      <Import Project="$(MSBuildExtensionsPath)\WebProfileBuilder\WebProfileBuilder.targets" />
    3. Done.

    The profile will get generated every time you build the project.

    Special Notes:

    After you modify the project file by adding the import statement, you will get this security
    warning when the project loads.  Choose the "Load project normally" option and press OK.
    image 

    Extended Usage:

    If you want to customize the web profile, you can add the following sections to your web.config.

    This section needs to be at the top of the file just under the opening configuration tag.
    <configSections>
       
    <sectionGroup name="robo.webProfile">
           
    <section name="webProfileSettings"
             
    type="WebProfileBuilder.WebProfileConfigurationSection, WebProfileBuilder, Version=1.0.0.0, Culture=neutral, PublicKeyToken=01d50f1f82943b0c"
             
    allowLocation="true"
             
    allowDefinition="Everywhere"/>
       
    </sectionGroup>
    </configSections>

    This section can be anywhere under the configuration section.
    <robo.webProfile>
       
    <webProfileSettings className="MyWebProfile"
                           
    nameSpace="CustomNameSpace" 
                           
    directory="CodeFiles"
                           
    fileName="MyWebProfile" />
    </robo.webProfile>

    The directory name is not a virtual directory, but is in reference to the root of the Web Application.
    The fileName is just the name of the file and should not include an extension.
    The className and nameSpace are as you would expect.
    None of these settings are required.  You don't even need to define this section at all.

    If you would like to use Web Profile Builder, it can be downloaded here.
    Also available is the complete source code and examples in VB and C#.

    Thanks
    -Joe

More Posts