DropDownList SelectedIndexChanged doesn’t trigger UpdatePanel in SharePoint 2007 SP1

A week ago I had a nice page that was hosted in SharePoint and, among others, contained an updatepanel with a dropdownlist in it. Everything worked, and all was well. Because our testers had found some issues we had delivered two newer versions to our development environment. In the last version they found an issue that wasn’t there before: selecting a different value in the dropdown did not only update the updatepanel, but resulted in a complete postback. My first idea was: that’s an easy one, I’ll check the triggers. Boy, was I wrong…

A few hours later, we had everything working again. But rebuilding the entire usercontrol didn’t solve the issue, and neither did changeing all available settings for all controls ;). Weird thing was it did work when outside of a SharePoint environment. After some extensive searching, I found a post on Brian H. Madsen’s blog, called Using DropDownList with an UpdatePanel in MOSS 2007. As it turns out, SP1 introduces a bug for working with dropdownlists, making that the SelectedIndexChanged event isn’t captured by the update panel. Brian’s post contains the code below which, if placed in the page_load, solves this issue:

if (this.Page.Form != null)
{
    if (this.Page.Form.Attributes[“onsubmit”] == “return _spFormOnSubmitWrapper();”)
    {
        this.Page.Form.Attributes[“onsubmit”] = “_spFormOnSubmitWrapper();”;
    }
}

ScriptManager.RegisterStartupScript(this, this.GetType(), “UpdatePanelFixup”,
    “_spOriginalFormAction = document.forms[0].action;_spSuppressFormOnSubmitWrapper=true;”, true);

Getting the ModalPopupExtender to work in SharePoint 2007

Getting the ModalPopupExtender from the Ajax Control Toolkit to work (decently) in SharePoint was not exactly a walk in the park. With a default SharePoint installation, the modal popup is partly positioned ‘outside’ of the page (you only see the bottom right part of the popup in the top left corner of the browser). Postbacks are not executed or executed poorly and the page gets garbled up. A possible solution for the positioning of the popup is to set the X and Y property of the ModalPopupExtender. Downside is you never know (for sure) if the popup is positioned inside the visible part of the browser because of things like non-maximized browser, different resolutions and so on.
Today we seem to have solved our issues with the ModalPopupExtender in SharePoint. We haven’t tested it in all scenario’s yet, but we’ll get to that. At this point everything looks the way it is supposed to. And it seems to work, too… ;).

The extra steps we took to make these two play together the way we wanted them to, besides the usual steps to make Ajax work in ASP.NET 2.0, are:

* Because of our setup with close-images that postback (because we have to clear controls and that sort of stuff) and more, we couldn’t use the TargetControlID property for the ModalPopupExtender. Well, we could, but that would result in the background not being displayed properly half the time ;). This can be solved by setting the TargetControlID to a dummy control (like a hidden one) and showing the popup from code.

** We have a usercontrol with several usercontrols in it. This one usercontrol (the parent) was added to a page. The normal (postback) controls on the usercontrols didn’t work after one of the modal popups was shown. And I can image neither would any other ‘normal’ controls on the page, but we didn’t encounter this scenario. The problem was the page would freeze with a message in the taskbar stating ‘The page is busy submitting data to the server’ directly after an Ajax postback. The controls that performed a ‘normal’ postback did nothing: the serverside code just was not executed. We solved this by putting an updatepanel around everything inside the usercontrol. That way the normal controls would postback in an ajaxy way too, apparently solving the ‘The page is busy’ message.

We’ll be testing this solution over the next few days. If any problems pop up, I’ll keep you posted.

I8 converter – Download (sourcecode available)

One of my colleagues pointed out the new Fine-grained password policy feature in Windows Server 2008. As you can see in this post at The Sean Blog, some values have to be entered in the I8 format, which isn’t very user friendly. Because of that, he asked me if I could whip up a tool that converts days, hours, minutes and seconds to the I8 format. I quickly put together this small tool for him. It hasn’t been tested extensively, but I think it might be of some use to some of you.
For example, I used longs to store the info. When wanting to calculate huge values, this won’t work of course. This could be improved by using the multipliers divided by for instance 10,000,000 and add the appropriate amount of zeros (7) to the string after calculation, so the numerical value stays a lot smaller. Still, I think it does what it should do at this point. You can find the sourcecode over here, or if you would like te change anything: be my guest. Let me know if you find bugs or add cool features: I would like to know.

Info taken from Technet:

When you use the ldifde command to create PSOs, you must enter the values of these attributes in I8 format, which stores time in the intervals of -100 nanoseconds. (Schema: attributeSyntax = 2.5.5.16 (I8).) Windows Server 2003 Default Domain Policy employs this exact time unit for its corresponding time-related attributes. To set these attributes to appropriate values, convert time values in minutes, hours, or days to time values in the intervals of 100 nanoseconds, and then precede the resultant values with a negative sign.

Links
Tool: http://www.rickvandenbosch.net/downloads/I8Converter.zip
Source: http://www.rickvandenbosch.net/downloads/I8Converter Sourcecode.zip

Update
Because of a hosting shift, the tool is no longer available from the above locations. When interested: drop me a line. I’ll see if I still have the app/sourcecode floating around somewhere.

(Single-Entry, ) Single-Exit, my 2 cents

As a reaction to this article at Peter Ritchie’s MVP Blog, I thought I’d give my two cents on having a single exit point for methods. Here we go…

When writing code, I always create only one exit point except for the occasional throwing of an exception. Most important for me is to always know where your code (really) stops. In case of long and complex methods there’s a chance you’re going to miss a return statement when scanning through a method. That way you could end up studying code not even applicable to the situation, while missing the real point. I do think there is no real reason to hold on to creating methods with a single exit point other than personal favoritism. This feels the same like using curly brackets with an if-statement that is followed by only one statement. I always use curly brackets, but there are lots of developers who don’t.

To answer the questions at the end of Peter’s post: Yes, I do generally follow the Single Point of Exit From Methods Principle, and I do ignore it for exceptions.

BTW: the SESE version of the first method in Peter’s post could be a little less (cyclomatic) complex:

public static int CountCommas(string text)
{
    int result = 0;

    if (!string.IsNullOrEmpty(text))
    {
        int index = text.IndexOf(‘,’, 0);
        while (index > 0)
        {
            result++;
            index = text.IndexOf(‘,’, index);
        }
    }

    return result;
}

Now available: PowerCommands for Visual Studio 2008

One of the most annoying things in Visual Studio (to me) was the fact that complete solutions (solution folders, projects, regulare folder)  would be expanded upon opening. And with big solutions, that isn’t too much fun. There are macros available online, but the ones i found were pretty slow. This is only one of the things that the PowerCommands for Visual Studio 2008 solves for you. But there is more. Much more!

A quick top five of my favorite functions:




  • Collapse projects


  • Copy & paste references


  • Open Command Prompt (at the location of the file you execute it on)


  • Remove and Sort Usings (for all classes in a C# project)


  • Close All (on a tab)

To get the PowerCommands for Visual Studio 2008, or to learn more about all the features in there, visit them over here.

By the way: this fairly new website (the Visual Studio gallery) was released quite silently by Microsoft. It contains lots of products that aid developers while working in Visual Studio, together with some Extensibility stuff. Have a look over there! Another tip: the VSX team blog. It might be a nice one to keep track of new releases of new Extensibility products. There’s a post over there about the PowerCommands, too.

Enjoy!

PageRequestManagerServerErrorException (status code 500)

In a quick AJAX demo I had to create today, I ran into a (somewhat cryptic) PageRequestManagerServerErrorException. I needed a simple form of the Cascading Dropdown, one that doesn’t use a (web)service for its data, but gets it from a simple method. Here’s the setup, and the cause…

Lets start with the exact error message:
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

I needed 2 DropDownLists working together to enable users to first select a month, and then select a day. Of course, you want the second DropDownList to only display days available for the chosen month. Because of that I had two dropdownlists in an updatepanel, and set the first one to AutoPostback so it would trigger the updatepanel. The first one had items set to something like this: <select item>, January, February, March, and so on. The second one got its items from a method, based on the selected value in the first. This worked like a charm, and all was well. When I wanted to check every scenario, I found that selecting <select item> didn’t clear DropDownList2 like I coded it to, but it threw the Exception. What caused this?

When setting items throught the IDE, this HTML is generated:

<asp:ListItem>&lt;select item&gt;</asp:ListItem>
<asp:ListItem>First item</asp:ListItem>
<asp:ListItem>Second one</asp:ListItem>

Because the value property for the items wasn’t set explicitly, it is generated (or should we say substracted?) from the information that was entered. Therefore, the Value property of the first item was ‘<select item>’. I can imagine how something like that can mess up a JavaScript environment. ;) I opened up the items for the first DropDownList, and set the value properties for each item explicitly (although setting it to a decent value for the first item did the trick). That resulted in this HTML:

<asp:ListItem Value=”0″>&lt;select item&gt;</asp:ListItem>
<asp:ListItem Value=”1″>First item</asp:ListItem>
<asp:ListItem Value=”2″>Second one</asp:ListItem>

Notice the Value attribute in the ListItem tag? That’s the one that will keep your project from throwing a PageRequestManagerServerErrorException.

By |February 20th, 2008|.Net, Error, HowTo|1 Comment

.NET Framework versioning, or The impact of switching from Visual Studio 2005 to 2008

With the official release of Visual Studio 2008 coming closer and its release on MSDN last november, there are more and more people asking what the impact of migrating from VS2005 to VS2008 will be and if it will have as big an impact as switching from VS2003 to VS2005 had. The answer is pretty simple: no it won’t. Let’s see why, and what impact it will have.

VS2003 > VS2005
VS2003 was the development environment for developing applications based on version 1.1 of the .NET framework. With this came a CLR specific to that version of the .NET framework (version 1.1.4322). When Visual Studio 2005 was released, the .NET framework 2.0 came with it. Because of several architectural changes, like for instance Generics, this version of the framework got it’s own CLR too (version 2.0.50727.42). Knowing this, it is logical that taking an application that was built for version 1.1 of the framework (and CLR) would have to be modified to be able to run on the new version 2.0.

VS2005 > VS2008
VS2008 is the first member of the Visual Studio family that enables multi-targeting. You can have an application target the 2.0, 3.0 or 3.5 version of the .NET framework. That is pretty different from VS2002 (.NET 1.0) and VS2003 (.NET 1.1) that were bound to their specific versions of the .NET framework. The reason it can do that is the same reason switching from VS2005 to VS2008 will not be too hard: these versions of the .NET framework all use the same version of the CLR (version 2.0.50727.42). Although version 3.5 installs Service Pack 1 for .NET 2.0, which adds some methods and properties required for features like LINQ, these changes do not affect applications written for version 2.0 of the .NET Framework.

Impact
Switching to Visual Studio 2008 will not require you to recode parts of your application, but it does have some implications to switch. VS2008 changes your .sln file, rendering it unreadable for VS2005. The projectfiles however are not touched, or at least not in such a way that VS2005 won’t understand them.* The odd one out here is the Web Application Project, which might give some trouble in a multi-VS-development-environment. Have a look at this post at Steven Harman’s blog to see how to work around that.

Language features
Working on a .NET 2.0 project from VS2008 does give you quite some extras, next to the fact that the IDE itself has been greatly improved.** A nice example is from someone who posted this question on the MSDN forums:
(..) while creating properties when I used the code-snippet (…), to my surprise VS 2008 created automatic property. (…) I double-checked the target version by opening project properties and it still showed .NET Framework 2.0. I used this new class in the web page, and it worked fine. How is this possible? Is there a bug in VS 2008 and this code will fail if deployed in production where only .NET 2.0 is available, or did I do something wrong?

The thing here is that automatic properties are not so much a feature of the .NET framework, but of the compiler. So go switch to VS2008, and enjoy.

Happy developing!


* TIP: If you need to edit a .NET 2.0 solution from both VS2008 and VS2005, create two different solutions, one for both. This way, you will be able to open the solution from both versions of Visual Studio. Do be advised! When working in VS2008, you might be tempted to use new syntax that won’t work in VS2005. ;)

** Found a cool new feature or a nice improvement in Visual Studio 2008? Add it to my list @ Listas!

Hooray (x 2) for MSDN library Community content

There are several steps to creating a custom web service to be hosted in SharePoint. The MSDN library has a walkthrough on creating a custom web service. After having followed this walkthrough thoroughly, I though my web service was good to go. Unfortunately all I saw was an error: “Could not load type ‘ThisAndThat’ from assembly ‘SoAndSo'”. Just as I was about to search for an answer somewhere online, I remembered that the MSDN library features the Community content for little over a year now (hooray!).

At the bottom of the walkthrough were some community additions to this specific walkthrough. Some have been incorporated in the main article, others are there to be found for guys (and girls!) like you and I. One of them exactly described my problem. In that same community content item another user had added this comment:
HI – I resolved this issue with a VB.net service by prefixing the class name in the .asmx file with the assembly namespace
And that was it! The WebService works like a charm, hosted in SharePoint. (hooray!)

So there you have it: that’s the power of [wikipedia:web 2.0] for you. Users benefit from the fact that they are now able to add to the otherwise pretty static information in something like the MSDN library. ;)

By |February 1st, 2008|.Net, HowTo, Link|0 Comments

.NET framework source code now available

As stated on ScottGu’s blog in his latest post, the .NET Framework Library Source Code is now available. A detailed blog post about the steps to follow to enable .NET Framework source access in Visual Studio 2008 can be found at Shawn Burke’s blog in this post.

Taken from Scott’s post:
Once you follow the configuration steps in Shawn’s post above, you’ll be able to dynamically load the debug symbols for .NET Framework libraries and step into the source code. VS 2008 will download both the symbols and source files on demand from the MSDN reference servers as you debug throughout the framework code.

Finally getting some coding done

The last weeks have been filled with holidays and introductional days at my new employer. Because of that, I haven’t had the time to realy dive into Visual Studio 2008, although I have played around with it since the RTM version was available. Since this week I’ve been writing some serious code in VS2008 for my new personal site (more about this later!). I can only agree with what Pieter de Bruin said in his post about the new line of developer tools and additions: “If you are building .NET 2.0 applications, you really should take a look at all of the above”.

I’m already loving working with VS 2008. There have been numerous ‘big’ additions and changes which make a developers life so much easier. But how about all the little ones? The ability to run a unittest from the contextmenu of the code editor, the ‘remove unused usings’ possibility (finally!), the enhanced properties form for project properties and so on. All those changes are great! I’m curious what you think are the best additions/changes in Visual Studio 2008. Care to share? Add your favorite at my ‘Best Visual Studio 2008 enhancements‘ list at Listas.