CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

March 2006 - Posts

  • ASP .NET 2.0 Validator Controls

    Here's an interesting thing I came across.

    Create a form, put some text boxes with required field validators on it.  Have a list bound by ObjectDataSource.

    Now drop a couple buttons on it, one button to add an item to the list.  The other button to go to the next form and should have causesvalidation = false.

    Because we want to make sure they add at least one thing to the list before they go to the next form, set the visible=false property on the next button.  Set it to true in the ObjectDataSource Selected event if the items returned has count > 0.

    Go to the next form.  Then use a hyperlink or button to return to the previous form, restoring the state with session or querystring.  The next button will be visible since the list has items.

    Click the next button, the page will post back, but the validators will fire even though causesvalidation is false.  If you then click the button a second time it will ignore validation like it should and move to the next form.

    If you take the visible=false off the properties in the .aspx page it works fine.  It also works fine if you just set visible in the codebehind.  It only bugs out when Visible=False is set in the .aspx page.

     

    Wierd.

  • A challenge to the development world

    I come across situations like this all the time.  Literally 70% of the project work I've done has been spent coming in after failed efforts and fixing things.  Now, while I like this superhero type feeling it gives me, I would like to pass on a challenge to all you developers out there.

    Start with clarity, then refactor to patterns.  Keep it simple, stupid.

    The most common causes I see for the WTFs I find in the wild are:

    1. Not understanding the toolset you are given.
    2. Trying hard for something "pure" or "elegant" in the OO sense without having the experience to do it properly.

    If you focus on clarity first, even if you don't create the greatest pure OO example ever at the very least the company you are doing work for will be able to maintain, refactor, and extend/rewrite functionality in the future in a more timely and efficient manner.  In the end, to a business, that is far more valuable than some coding icon to be held up on some OO or architecture altar.

    Learning to write good OO and tiered applications is a matter of trial and error.  I would be remiss if I didn't point out that I have written some god awful code in my day, and I'm sure when the paradigm majorly shifts out of .NET today I will write some god awful code again.  The key should be to write this code on your own time and keep your work related projects simple and clear until you learn enough to refactor to good patterns and practices.

  • ObjectDataSource Programatic Parameters and the DataList

    So I've been toying with the new object datasource in our new pet insurance quoting engine we are building and I must say that even though I started with a very wary feeling thanks to Sahil, this control is really growing on me.

    The basic scenario in this exercise is that users are filling out a form with pet information that I want to display in a 3 column datalist with a button in the data to remove a pet they have already added.  I wanted to leverage the ObjectDataSource calling a method that provided a generic list of QuotePet objects. 

    The datalist itself needed to select the list of added pets by the QuoteId which I could have stored in session etc, but for exploration's sake I decided to figure out how to provide a parameter to the ObjectDataSource that wasn't in the wizard list (ie asking a method for it).

    So first we need my datalist and its object datasource which has the select and delete methods defined:

    <asp:DataList ID="dlPetlist" runat="server" Width="265px" CellPadding="3" CellSpacing="6" DataSourceID="odsPetList" DataKeyField="Id" RepeatColumns="3" OnDeleteCommand="dlPetlist_DeleteCommand">
                <ItemTemplate>
                    <label class="gridLabel">Name: </label>
                    <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    <br />
                    <label class="gridLabel">Breed: </label>
                    <asp:Label ID="BreedLabel" runat="server" Text='<%# Eval("Breed") %>' />
                    <br />
                    <asp:Label ID="AgeYearsLabel" runat="server" Text='<%# Eval("AgeYears") %>' />
                    <label>years</label>
                    
                    <asp:Label ID="AgeMonthsLabel" runat="server" Text='<%# Eval("AgeMonths") %>' />
                    <label>months</label>
                    <br />
                    <asp:Button ID="Delete" Runat="server" Text="Delete" CommandName="delete" />
                    <br />
                    <br />
                </ItemTemplate>
            </asp:DataList>

            <asp:ObjectDataSource ID="odsPetList" runat="server" SelectMethod="GetQuotePets"
                DeleteMethod="DeleteQuotePet" TypeName="BasePage" OnSelecting="odsPetList_Selecting">
                <SelectParameters>
                    <asp:Parameter Name="quoteId" Type="Object" />
                </SelectParameters>
                <DeleteParameters>
                    <asp:Parameter Name="petId" Type="Int32" />
                </DeleteParameters>
            </asp:ObjectDataSource>

    So we can see that the datalist highlighted in red the reference to the objectdatasource, and the delete command method in the code-behind.  In the objectdatasource we can see that I've hooked into the OnSelecting event which allows you to make changes to the select parameters, in this case quoteId, before the datasource goes off to do its business.  This is the event to use when you want to set the parameters programatically.  Let's take a look at the odsPetList_Selecting code:

        protected void odsPetList_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)

        {

            e.InputParameters["quoteId"] = this.QuoteId;

        }

    Pretty simply really.  The event provides argument e which has a collection of the input parameters that you can set programatically.

    The next question is how do we call the delete on the objectdatasource from the datalist?  For this we catch the dlPetList_DeleteCommand event and once again set the parameters for the object datasource and then force it to run its delete method.  For all the description text I just wrote, it's accomplished in all of two lines:

        protected void dlPetlist_DeleteCommand(object source, DataListCommandEventArgs e)

        {

            odsPetList.DeleteParameters["petId"].DefaultValue = Convert.ToString(dlPetlist.DataKeys[e.Item.ItemIndex]);

            odsPetList.Delete();

        }

  • Why Red-Gate Rocks My World

    So we had an issue recently where a bit of code that we do not have the source for and is obfusticated (grr) was making a series of inserts and updates to the database that were causing some issues elsewhere.

    Being that it was a many stepped process we weren't sure what the impact would be or even what all it was changing.

    Solution: Take a backup of the database, make a copy of it, run the code routine, then fire up Red-Gate's Data Compare tool to quickly find what was modified!

  • Win XP x64

    So I received my new winbook A730 which happens to have the AMD Turion 64 processor in it.  The workplace decided to use me as a test subject for windows xp 64 and I've been into it for a week so I figured I'd blog the results.

    Finding all the 64 bit drivers for my hardware was somewhat of a chore, but we were able to find everything except the drivers for the card reader which is a texas instruments model and the video card driver for the radeon isn't the same generation as the one in the laptop but it does work fine.

    Performance wise I see very little difference from XP 32.  Note that I haven't done any load testing but this is just my perception from normal day to day usage.

    The big plus is that x64 runs IIS 6.0 which is great for my web development goo though I had to set up IIS to run in 32 and 64 bit mode which required some KB surfing and when it runs in dual mode the ASP NET tab in IIS goes away which is moderately annoying.  The majority of software I use works just fine, Codesmith required some tweaks to the Xheo Licensing file and anything that uses the old Borland database engine simply does not work.

    I also seem to have internet explorer 32 and 64 bit but for the life of me I can't perceive any difference.

    Overall I'm satisified with how the laptop is running, however, and if you're interested in putting x64 on a primary development box it does work fine as long as you're willing to jump through the extra configuration hurdles for drivers and setup.  If you don't want to deal with that stuff, have exceptionally new hardware, or really aren't intrigued by being on a 64 bit operating system at this time there really isn't any compelling reason to mess with x64.

  • ObjectDataSource / Gridview Display Class SubObject Property

    How's that for a title?  I am trying to get a the maxinum number of hits for the keywords I'd use on Google for this one since it wasn't all that straightforward.

    Let's say you are using the object datasource to return a generic list of address objects.  Inside the address object is a "subobject" called address type which has properties of description and id.

    The question is, since the gridview's list of available columns doesn't drill into the subobject, how do I display the description property on AddressType?

    The answer lies within the template column.  <minirant>Template columns are a favorite of mine from the old datagrid days in that they allow you to do pretty much whatever your heart desires.  The problem is the usage of them is rather poorly documented so you usually have to go to blogs and newsgroups to find information like this.</minirant>  Anyways, here's the solution:

    <asp:TemplateField HeaderText="Type">

           <ItemTemplate>

                   <asp:Label ID="lblAddressType" runat="server" Text='<%# ((Address)(Container.DataItem)).AddressType.Description %>'></asp:Label>

           </ItemTemplate>

    </asp:TemplateField>

    As you can see I'm casting the dataitem as the parent object (Address) and then just calling the property like you would in a codebehind!

  • Seeking : Printer Management Solution

    Being a "web guy" I tend to have very little if any dealings with printing solutions so naturally I have to turn to the codebetter community.

    Looking for a print server type solution that does the following:

    1. Allows templates to be set up in a variety of formats (word, excel, pdf, whatever) that support sending in detail information in a standard format be it delimited or xml
    2. Manages the storing of the information sent in so that a record is kept of what was printed when, allow viewing and reprinting.
    3. Have a nice clean interface to be called from other programs to start batch jobs etc.
    4. Would be nice if print requests could be batched and ordered according to rules (print letters ordered by state, etc)

    Naturally we'd like it to be cost effective etc since we're not a huge shop.  Basically we'll be evaluating build vs buy.

    Any suggestions?

More Posts

Our Sponsors

Free Tech Publications