ASP.NET generated html textarea instead of input type text in a datagrid

aka… learning is growing and growing means growing pains.

One of the wonderful things about ASP.NET is Microsoft’s ASP.NET Web Matrix. It includes a GUI Web Forms designer as well as many excellent templates for data driven pages. It is free, which means that coupled with IIS5.1 on Windows XP or even MONO on Linux, you can develop ASP.NET Web applications for free. There is no need to pay $$$ for Visual Studio.NET.

I’m new to ASP.NET, but since I’m not new to the web or programming, I often find myself trying to do slightly more advanced things with unfamiliar tools. This happened today with the datagrid control. I have a very simple SQL Table that drives a very simple FAQ for a website that is still powered by old ASP. I won’t vent how painful it is to maintain this old ASP code other than this sentance mentioning it. Amazingly, no one ever write a front end for maintaining this table of FAQ entries! Maybe some used Access to maintain it? I find it hard to believe someone actually edited things with direct SQL commands, or SQL Enterprise Manager. I wanted a very easy way to update the FAQ.

ASP.NET’s DataGrid control provides a very easy way to get this done. In fact, with ASP.NET Web Matrix, I was able to say “File->New->Data Pages->Editable Data Grid” and have a template ready for me to change to our environment. I change connectionString, SelectCommand, and the INSERT and UPDATE lines and I had a working page! That was so easy it was awesome! But who wants to edit html in a input type=text html control?

Google Groups didn’t show much about easy solutions for this. The only reply I found suggested using TemplateColumns and inserting the textarea directly. Well, I’ve seen DataGrid TemplateColumns before, and they are an excellent and powerful features, but I wanted a quick hack into editing a table. TemplateColumns would have been overkill.

I learned that ASP.NET doesn’t differentiate between input type=text and textarea. Instead ASP.NET decides which to display at page render time. If the ASP.NET TextBox control is multiline and has row and column counts greater than 1, it renders a textarea instead of an input box. (To be clear, it depends on teh values of TextBox members TextMode, Rows, and Columns.)

The biggest question to me, as a DataGrid newbie, was to find where to set these properties. It must be done programatically at run time, because we don’t have access to the controls at design time. Well, we might have some access to the controls are run time, but that involves the ColumnTemplates that we are trying to avoid. It turns out that the DataGrid.ItemDataBound event is where things need to be.

Add attribute OnItemDataBound="DataGrid_ItemBound" to the asp:datagrid tag.

Then set properties in that event function.
For example:

void DataGrid_ItemBound(object sender, DataGridItemEventArgs e) {
if ( e.Item.Cells[3].Controls.Count > 0 ) {
((TextBox)e.Item.Cells[3].Controls[0]).TextMode = TextBoxMode.MultiLine;
((TextBox)e.Item.Cells[4].Controls[0]).TextMode = TextBoxMode.MultiLine;
((TextBox)e.Item.Cells[3].Controls[0]).Rows = 20;
((TextBox)e.Item.Cells[4].Controls[0]).Rows = 20;
((TextBox)e.Item.Cells[3].Controls[0]).Columns = 40;
((TextBox)e.Item.Cells[4].Controls[0]).Columns = 40;
}
}

The event gets called for every binding. If you are familiar a little with the DataGrid, you know that even in edit mode, the grid is displayed, so the non-edit table cells need to be displayed, so the if block is important here!

flickr + xscreensaver = fun

Yesterday I fixed the livejournal script that comes with xscreensaver to work with any rss feed. No more boring fortune output when xscreensaver randomly picks Noseguy. Instead, a random quote from your favorite rss feed is displayed.

Today I hacked up JWZ’s xscreensaver-getimage-file perl script to pull images from Flickr instead of a file or directory on your computer.

It was easy to do using Flickr::API and LWP::UserAgent.

glscreensaver, blitspin, decayscreen, distort, and any other xscreensaver that normally would pull an image from a directory, will now pull a random photo from any users public flickr photos.

Here is how to install it on an ubuntu hoary system. This is probably true for warty, and debian, and many other distributions.
Continue reading “flickr + xscreensaver = fun”

ljlatest

this little piece of xscreensaver is kinda fun.

But I say fuck livejournal. I want to see plant.arslinux.com… so I need this patch:

--- /usr/lib/xscreensaver/ljlatest 2005-02-14 08:46:59.000000000 -0500
+++ ljlatest 2005-02-19 20:21:31.172904382 -0500
@@ -306,6 +306,9 @@

my ($ig0, $title) = m@< (TITLE [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi;
my ($ig1, $body) = m@< (DESCRIPTION [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi;
+ if(!$body) {
+ ($ig1, $body) = m@< (CONTENT:ENCODED [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi;
+ }
my ($ig2, $url) = m@< (LINK [^<>\s]*)[^<>]*>\s*(.*?)\s*@xsi;

$_ = "$title\n\n$body";


Isn’t that fun?

now just start your favorite xscreensaver like this:

fontglide -program 'ljlatest http://planet.arslinux.com/rss10.xml'

NEATO!

beborn beton

Wow, I usually don’t think much of beborn beton, but this “Harmony is Everything” mix of Your Hands on my Skin is excellent.

Rusty Recursion

showing rusty skills.

For whatever reason, I’m showing how rusty of a coder I am. I’ve written a little recursive function that takes a date and an EventLog object, and tries to find two EventLog entries that occur between that date. Its a simple binary search, which should work great since EventLogCollections are sorted, or at least that is what ONE article says. I cannot find that fact for sure in the Framework documentation.

Ah well. Its fun to be programming some again, even if it is for no reason except that I “can” for my current job.

debugging asp.net applications

I’ve got dbgCLR.exe running, and I see source, and my browser is still loading the page… waiting.

All I can say is that this is a very excellent way to develop a web application. I have a debugger when I need one!

sweet music drive to work

It was pretty sweet this morning driving to work. I listened to Keith Jarrett doing Autumn Leaves live at the Blue Note. s/pretty/totally/ It was totally sweet. I got to work a little early, so I sat for two or three minutes listening to the spectacular end of the set. It ended and I said “damn that was sweet”. Out loud. No one heard me. I openned the door, turning the stereo off after three bars of Nina Simone’s House of the Rising Sun played. I can’t wait to get into the car and listen to that.

I made a mix cd. Those two tracks are on it. It is to what I was listening. This cd is inspired by a friend of mine who said he couldn’t get into jazz. He’ll probably be getting a copy, unless I find some flaws in it. I’m still not exactly sure how well it will flow.

cool regular expression examples

http://msdn.microsoft.com/library/en-us/dnnetsec/html/THCMCh10.asp?frame=true

has some great regex examples about 1/2 down the page.

oh, and the CAS ASP.NET pages are essential for anyone deploying ASP.NET files on a web server which has its webroot on a network share, or any webserver to which you do not have admin rights.