.Net

Spot the Stupid

Amazingly, this doesn’t work:

TableCell cell = new TableCell();
cell.Controls.Add(cell);

Huh.

.Net

Comments (1)

Permalink

Encoding HTML Entities in an ASP.NET DropDownList

I ran into this one today, alternative solutions are welcome…

If you’re programatically adding items to an ASP.NET dropdown list, you do it with something like this:

_cboDropDown.Items.Add(new ListItem(”Item Text”, “Item Value”));

Which is fine for most cases, but unlike with, say, the Text property of the Label control, ASP.NET will HTML encode the text before rendering it, so if you have something like:

_cboDropDown.Items.Add(new ListItem(”Brand X®”, “1131″));

It’ll show up in the dropdown as “Brand X®” instead of the intended “Brand X®”

What’s a poor developer to do?

I ended up using the Server.HtmlDecode() method, as follows:

_cboDropDown.Items.Add(new ListItem(Server.HtmlDecode(”Brand X®”), “1131″));

…and it’s all good, if a little ugly for my tastes. Has anyone got a better idea?

.Net

Comments (1)

Permalink

ASP.NET Web Administration without VS2005

I’ve recently gotten started with ASP.NET’s membership and roles features (after re-working a MySQL provider found here), and I’ve gotta say, for the simple purposes I’ve been using it, it’s easier to implement than Rails’ various LoginGenerator systems.

The only thing that sucks about it is the administration, which seems to require Visual Studio 2005 and is tied to your web.config settings, which may (should?) be different between dev and prod. In an unrelated Google, I stumped upon Rahul Soni’s guide to running the web admin tool without VS2005. Haven’t tried it yet, so this is more of a bookmark than anything else, but well worth noting - up until now I’ve been admining the users locally and then uploading a dump of the user table to the server with my changes, and that won’t work too well once people start changing passwords and whatnot…

.Net
Visual Studio

Comments (0)

Permalink

ScottGu takes on URL rewriting in ASP.NET

Scott Guthrie has written up what’s got to be the authoritative guide to URL rewriting in ASP.NET.

I haven’t yet implemented any of those tricks, but I do at least try to ensure that the majority of my pages are in their own folders and are the Default page, so URLs don’t look quite as ugly when I email them out and the technology behind the site is somewhat masked*. This all goes to hell, of course, on a postback, but Scott’s tips on using Control Adapters might do the trick for getting rid of the “Default.aspx” in the URL.

* (Not that I’m about to migrate away from ASP.NET of course…)

Technorati Tags: ,

.Net

Comments (0)

Permalink

C#’s params keyword makes variable parameter functions easy

I don’t know why I didn’t know this earlier, but C# has a nifty language element called params that lets you do multi parameter functions nice and easy. Instead of assigning an array as a parameter, which means explicitly creating and populating an array in your calling code, you can use do void foo(params int[] myparams) and call it with foo(1, 2, 3, 4) etc. This is much nicer for the caller, and judging from my preliminary tests, it looks like it’s backward compatible with older method calls, so you can add the params keyword to an existing function that takes an array without having to change all the old references.

Technorati Tags:

.Net

Comments (0)

Permalink

MySQL and ASP.NET performance

Karl Seguin’s got a post on a topic near and dear to my heart - MySQL on Windows. We’re running ASP.NET with a MySQL back end, which happens to be the same kind of setup that he’s looking at for an upcoming project. When we made the decision, it was the early days in the company and we couldn’t afford SQL Server licenses but needed to support some legacy ASP code, so we were driven by necessity. Karl actually took some basic metrics, so, uh, good for him.

Shortening his short version of the story, InnoDB on 5.x might be problematic, but ASP.NET connects at about the same speed as PHP does, and MySQL on Linux blew away SQL Server on Windows. It was a simple test that shouldn’t provide any real conclusions, but hey, it gives me a much stronger position than “uh, we run it because we had to.”

For the record, I love the MySQL/ASP.NET combo, at least if you’re tied to IIS like we are. For code, while we do have some PHP applications, I’m quicker and more comfortable (and thus more productive) with C#. For the db, there are a lot more web developers out there running MySQL and posting stuff online about it, so I’ve got a broader knowledge base to draw from when I have a riddle to solve.

Technorati Tags: ,

.Net
MySQL

Comments (1)

Permalink

Jason’s new law of string manipulation coding

Never do in 65 lines of messy string manipulation loops what you can accomplish in 3 lines of RegEx code.

Backreferences rock.

For the curious, I was trying to “englishify” hours of business information, so a set of 14 open/close strings would be translated into stuff like “Mon. - Fri.: 9 - 5″ or “Mon., Wed.: 9 - 5, Fri.: 8 - 4″ or “Mon - Wed., Fri.: 9 - 5, Sat - Sun.: 12 - 6″ or whatever, and the only think that rocks harder than backreferences in a task like this is TestDriven.NET. I was able to write the messiest code ever while testing it out, get it working, and then make it pretty and maintainable. It’s nice to be able to skip step and go straight to the pretty code, but sometimes, particularly with algorithms, it helps to push the bits around the screen for a while.

Technorati Tags: , ,

.Net
Programming

Comments (0)

Permalink

MSDN Webcast report: ASP.NET Soup To Nuts - Tips and Tricks

I had a chance to check out the “Tips and Tricks” MSDN Webcast from the ASP.NET Soup to Nuts series - there’s so much stuff in 2.0 that I figured it was well worth my time to get a refresher/review, especially since, like most MSDN webcasts, it happened at noon. Again, like every other MSDN webcast, my lunch wasn’t ready when it started. Wireless laptops are handy. :)

Here are my notes. Has anyone else had any experiences with these features?

The webcast was basically a response to questions that the Bill had been asked over the course of the series so far, so there wasn’t much of a slide deck, just a series of demos covering various bits of .Net, including…

Wizard control

I’ve implemented a few wizard-type multi step processes for data entry in the past, and while it’s a lot easier to do it in ASP.NET than, say, PHP, it still hurts a ton. The new Wizard control appears to take care of a lot of the mess for you, and it’s definitely going to get a workout when I have to do the next one of these.

Multiview control

Simplifying the above, this appears to be a replacement for manually managing Asp:Panel controls and their visibilities depending on what’s going on - I know I do a ton of that already, so if there’s a nicer way to handle these cases, bring it on!

Validation controls

I was already well aware of these (they do save a lot of time), but it struck me as they went through the demo that I always invoke them from code view (ASP.NET code, not the C# code file). I spend most of my time in this view, probably because of too many burns in the 2003 days, but I really should give the graphical editor another try. “Act like it’s a demo” might be a neat mantra to improve my productivity. :)

Binding to XML

This is just another example of DataSource objects, in this case an XmlDataSource. Nothing new here for me, but I need every reminder to use the built in controls that I can get - I tend to do this crap with code still.

urlMappings section of system.web

Where did this come from? Anything that remotely approximates .htaccess/mod Rewrite in the Apache world needs to be sent to me via FedEx - it’s the weakest part of IIS, as far as I’m concerned.

FileUpload control

I ported some of our 1.1 code to this a while back. It’s still not clear to me if the 2.0 version has the same problems with big files (several clients uploading really big files at the same time can cripple a server). Anyone?

Setting focus to fields on startup

Not too exciting, just putting textBox.Focus() into the Load event for the page. I guess Focus is new in 2.0. When they started talking about default controls I thought they were going to talk about dealing with the enter key in forms.

MaintainScrollPositionOnPostback

This could be a handy page directive for big pages. I need to review the directives in 2.0…

New properties for the Label Control

Things like AccessKey, AssociatedControlID, etc really show how the Label control has gone from a “why bother” widget in 1.1 to something that makes accessibility easy. I really got tired of trying to do things the “right” way and the ASP.NET way in 1.1 - they were really at odds back then.

Technorati Tags: , ,

.Net
MSDN

Comments (0)

Permalink

Data Binding a Repeater to a SortedList of structures

Kelly’s comment in the phone numbers in data bound controls post reminded me of something I did this morning:

The problem: I had a bunch of data that I needed to report on, based on date. Unfortunately, there were a bunch of different dates in the table that needed to be aggregated (various timestamps for events in a record’s life) that couldn’t be summed up in one SQL statement.

The solution: I love the System.Collections namespace, so I went with a structure containing the counters, and stored one structure per day in a SortedList. There were three counters that needed to be rolled up, so three DataReader.Read() loops later, I had my data structure.

But how to render it?

Once again, I had to use every fibre of my being to avoid implementing an OnDataBound event handler and doing a bunch of FindControl() calls. Yes, that was a good step away from the old ASP style of Response.Write() calls embedded in the table markup, but come on, it’s 2006 already (although this was ASP.NET 1.1)

The rendering code:

_report.DataSource = counters;
_report.DataBind();

So how do we bind a SortedList (or a Hashtable if you don’t care about the order)?

Within the ItemTemplate of my Repeater, I used this for the key (the date):

<%# ((DateTime)((DictionaryEntry)Container.DataItem).Key).ToString(”d”) %>

For the individual elements of the structure:

<%# ((Record)(((DictionaryEntry)Container.DataItem).Value)).aCounter %>

A few too many brackets for my liking, but hey, it beats hiding it in a CodeBehind.

.Net
Data Binding

Comments (0)

Permalink

FYI: Microsoft’s CheckedListBox doesn’t support DataBinding

Dear Microsoft,

If you decide that a .NET WinForms control, like, say, the CheckedListBox, doesn’t support databinding, how about blocking the damned call to DataBind()?

Sincerely,
Luke’s pants
(This is what I get for trying to do things “properly” instead of just coding a population loop in the first place.)

Technorati Tags:

.Net

Comments (0)

Permalink