Archive

Archive for March, 2008

List<object> Find with delegate

11 March, 2008 Leave a comment

Thought this was really cool.

I have a generic List of objects, for example:

List<User> users = GetAllUsers();

I want to find a user by username. Usually you would loop through looking for the item with the matching username and return the user that was found.

or you could do this:

User loggedOnUser = users.Find(delegate(User u) { return u.Username == "jbloggs"; });

Much less code, and you could match on any property of the object that you wanted.

Cheers

Categories: c#, Code

AJAX UpdatePanelAnimationExtender conditional enable actions

6 March, 2008 Leave a comment

Recently I have been working with AJAX to create some intelligent controls.

I have created a Format selector which looks something like the following…

Figure 1. Format Selector 

The purpose is to allow users to select from a predefined list of formats.

As it is done in Ajax I wanted to use the UpdateProgress control to show a nice “Updating” animated gif. I implemented that but it was a pretty poor solution as users could still interact with the controls whilst it was updating.

Enter the UpdatePanelAnimationExtender. I Played around with this and got the system doing a fade animation just following the example in the samples. This worked fine. But the user could still interact with the controls so I put in the following code to disable the controls onupating and enable the controls onupdated.

<Parallel duration="0">
    <EnableAction AnimationTarget="btnOk" Enabled="false" />
    <EnableAction AnimationTarget="lstFormatTypes" Enabled="false" />
    <EnableAction AnimationTarget="lstFormatCodes" Enabled="false" />
    <EnableAction AnimationTarget="txtDecimals" Enabled="false" />
    <EnableAction AnimationTarget="imgUp" Enabled="false" />
    <EnableAction AnimationTarget="imgDown" Enabled="false" />
</Parallel>

This is it in action after using the format type drop down:

Format Selection in action 

This seemed to work find until I started fiddling with the visibility of the controls in the postback methods being fired on autopostback of the drop downs. Example, if someone chose a format type of “Date” there is no need for the decimals text box, so I was setting the Panel that contained that control (and headings) to visible = false.

The outcome of this was when switching to date the animation would work fine, but when switching back to currency it would appear to hang with the animation remaining opaque but some controls enabled. I assume this was due to the fact that txtDecimal, imgUp and imgDown were no longer visible and therefore not accessible via javascript.

The fix was to change the EnableAction to be conditional like so:

<Parallel duration="0">
    <EnableAction AnimationTarget="btnOk" Enabled="false" />
    <EnableAction AnimationTarget="lstFormatTypes" Enabled="false" />
    <Condition ConditionScript="$get('lstFormatCodes') != null">
        <EnableAction AnimationTarget="lstFormatCodes" Enabled="false" />
    </Condition>
    <Condition ConditionScript="$get('txtDecimals') != null">
        <EnableAction AnimationTarget="txtDecimals" Enabled="false" />
    </Condition>
    <Condition ConditionScript="$get('imgUp') != null">
        <EnableAction AnimationTarget="imgUp" Enabled="false" />
    </Condition>
    <Condition ConditionScript="$get('imgDown') != null">
        <EnableAction AnimationTarget="imgDown" Enabled="false" />
    </Condition>
</Parallel>

With the conditional settings I am saying ONLY if the control exists  set the enabled status.

Problem solved.

Cheers

Post Script

The other easier way to get around this would be to put a panel around all the controls, and just enable/disable the panel. This removes the issue and is alot less code 🙂

Categories: AJAX Tags: