Bastardizing The Repeater Control

posted on 2004-10-03 at 22:58:34 by Joel Ross

Maybe I should actually get back to some technical posts, huh?

Anyway, on my current project, I created a screen with a data grid on it. Later, we got the HTML layout from our design team, and it was up to me to skin the page. The skinning required that I change the data grid to a Repeater. The design wasn't too complex, but enough that I felt the move to a Repeater was warranted.

Basically, we have a row of data. Clicking anywhere in the row should cause a post back, and that row should be added to another Repeater on the page. Basically, you select an overview row to move it to a more detailed row.

The design called for clicking anywhere in the row to cause the post back, as well as the button labeled for that action.

Now where does the bastardization come in? Well, to make the HtmlTableRow post back automatically, I had to add the post back code by hand. So in the repeater's ItemDataBound event, I get a reference to the row, and add an onClick attribute to it, which calls   doPostBack(), and add the button's clientId as the parameter.

So basically:

row.Attributes["onClick"] = "javascript:__doPostBack('" + link.ClientID + "', '');";

It's a little bit of a hack, but it works right? Well, not quite. First, the ClientID uses the _ as a separator, but the javascript uses the $. So add a Replace for _ with $? Yes and no. Yes, that works. No, because if you don't explicitly give the controls an ID (as well as the hierarchy of controls), the control will get an ID automatically - usually _ctrln. So what to do then? Here's the line I'm using (and it works perfectly every time!):

row.Attributes["onClick"] = "javascript:__doPostBack('" + link.ClientID.Replace("_", "$").Replace("$$", "$_") + "', '');";

Now to me, that's a bastardization of the Repeater control. But it works.

Categories: ASP.NET