Contents tagged with ASP.NET

  • Web Profile Builder

    It's been over five years since I've made any updates to this project.  I had basically

    left it for dead because I personally no longer have a need for it.  I know a lot of

    people do still rely on it though.  I had some free time recently so I decided to give

    the project a little bump to make it easier to use and more accessible to those who do

    still use it.



    What's changed?

    • Most importantly, this is no longer required to be installed in the GAC.
      • There is no installer at all anymore!
      • Now it can be included in the project source control and referenced
    • Added support to install using NuGet.
      • PM> Install-Package WebProfileBuilder
    • Simplified the configuration.
      • Support for configuration via Web.config has been removed. This was
        more of a "nice to have" feature and added unneeded complexity to
        the code base.
      • All configurable options are still supported, but now it has to be
        configured in the web project file.  See below for a complete
        example of the configuration.
    • Moved project home to CodePlex.
    • Added build automation to the source code using NAnt.




    • The core code base has not been changed.  I didn't want to introduce any bugs, so
      I only changed the code necessary to achieve my goal.  All code changes were
      related to configuration.
    • If you are new to WebProfileBuilder, know the following:
      • The generated profile class does not get automatically included into the
        project.  You must use the Solution Explorer to show all files, then
        manually include the generated class into your project.  You only
        need to do this once.
      • You also must create the "Profile" property in your Page class. See
        below for an example.



    Example web project file:

    <?xml version="1.0" encoding="utf-8"?>

    <Project ToolsVersion="4.0"




        <!-- ... other project content ... -->


        <!--WebProfileBuilder setup.-->

        <UsingTask TaskName="BuildWebProfile"



        <Target Name="BeforeBuild">

            <!--WebSiteRoot, RootNamespace, and Language are required.-->

            <!--ClassName, Directory, and FileName are optional for additional customization.-->

            <BuildWebProfile WebSiteRoot="$(ProjectDir)"





                             FileName="MyWebProfile" />



        <!-- ... other project content ... -->





    Example page class:

    using System;

    using System.Collections.Generic;

    using System.Web.UI;


    namespace CsExample {

        public partial class _Default : Page {

            //... other class content ...


            public static MyWebProfile Profile {

                get { return MyWebProfile.Current; }



            //... other class content ...




  • Web Profile Builder Released

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

    If you are unfamiliar with Web Profile Builder, you can read my initial blog post about it here.

    Changes made for release

    • Added the ability to detect changes made to the profile section of the web.config file
      and only rebuild the Profile class if changes have been made.


    • If you used the previous release, remember to uninstall it first.
    • Also, if you used the previous release and added the customize section in the
      web.config file, remember to update the assembly reference to
      “WebProfileBuilder.WebProfileConfigurationSection, WebProfileBuilder, Version=,
      Culture=neutral, PublicKeyToken=01d50f1f82943b0c”.

    Thank you to everybody who provided me with valuable feedback.  This release should address
    all of your concerns.



  • 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.

    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".

    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" />

    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.
    //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.

        $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.


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


    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.



  • 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.


    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.

    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.
    <sectionGroup name="robo.webProfile">
    <section name="webProfileSettings"
    type="WebProfileBuilder.WebProfileConfigurationSection, WebProfileBuilder, Version=, Culture=neutral, PublicKeyToken=01d50f1f82943b0c"

    This section can be anywhere under the configuration section.
    <webProfileSettings className="MyWebProfile"
    fileName="MyWebProfile" />

    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#.



  • A more elegant solution to display GridView header and footer when the data source is empty.

    I think the need to always show the header and footer of a GridView is pretty common.
    When I first ran into this problem, I went to Google and found lots of content about this.
    Some suggest tampering with the data source to add an extra row if it’s empty. 
    Others show overriding the CreateChildControls method.

    I was not satisfied with either of these solutions. I didn’t like that dirty feeling I had by tampering
    with the data source.  And I didn’t like overriding the CreateChildControls method because it
    simply didn’t work of me anyway.  This solution only gave the appearance of the header and
    footer existing.  I ran into issues because I was programmatically adding controls to the header. 
    Upon postback, if the data source was empty, the control hierarchy would not be the same as
    before thus causing an error.

    So here is my solution.  I'll cover the main points of interest and if you want to see more, I have
    uploaded all the source and example to the new MSDN Code Gallery.

    Start out by extending the GridView control and add the following structure. 
    The most important part here is to override the PerformDataBinding method.

    As you can see here.  I am intercepting the data and making sure there is at lease one row. 
    If there is, the control behaves normally.
    On the line with the red arrow, I am checking if the binding source is a DataView. 
    If it is, I can just add a row here and be done with it.
    If the binding source is not a DataView (or DataSet), then I fire an event that will need to be handled.

    The event "MustAddARow", as seen here, will provide access to the binding data and allow you to add a row.

    Here is a snippet from a page where I handle the MustAddARow event.
    In this case, I am binding a List of Products to the GridView.
    As you can see, I'm just adding a new Product to the list.  It doesn't matter what data you add here
    because it will get hidden in back in the GridView.

    And finally back in the GridView, I override the OnDataBound method so I can hide that extra row.

    So there you have it.  This is my first blog post ever. 
    Hopefully someone will get some use out of this.