Conditional label within DataList?

D

Danny Tuppeny

Hi all,

I've got a DataList that's bound to a datasource with two columns (well, two
that matter). One is called GigDate, and one is called RescheduledFromDate.

GigDate doesn't allow NULLs, but the RescheduledFromDate does. In general,
only a GigDate will be supplied, but if a Gig is rescheduled, the GigDate is
copied to RescheduledFromDate, and the GigDate then becomes the "new" date.

I'm using a DataList instead of a Grid because it's slightly more flexible
for the design (I'm using several rows to display each record).

What I'd like to display is something like this

---- If a gig has NOT been rescheduled ----
Gig Date: xx/yy/zz
---- / ----

---- If a gig HAS not been rescheduled ----
Gig Date: xx/yy/zz
(rescheduled from xx/yy/zz)
---- / ----

I can see a few ways to do this, but I don't like the ones I've managed to
get working. One is to have a label bound to RescheduledFromDate, with
Visible bound to an expression checking for null. However, this means the
Text property has to be a custom expression too. To keep the label Text
property bound to the field directly, I could have another two labels for
the text, with the Visible properties set to the same as the
RescheduledFromDate field (one containing "(rescheduled from " and the other
just ")").

Neither of these are particularly great, and I can see instances when they'd
work even less well! (eg, if I needed an image to be displayed, as well)

What I'd *like* to do, is something like a PlaceHolder, with Visible bound
to an expression checking if the field is null, and then place literal text,
and a bound label inside.

I've tried using the PlaceHolder control, but I don't see to be able to put
anything inside it in the designer. A panel might work, but I believe this
renders out as a Table or something, and I don't want anything that pollutes
the markup unnecessarily.

I'm sure I'm missing something simple. Any offers?

Thanks!

Danny Tuppeny
 
D

Danny Tuppeny

Danny Tuppeny said:
I've tried using the PlaceHolder control, but I don't see to be able to
put anything inside it in the designer. A panel might work, but I believe
this renders out as a Table or something, and I don't want anything that
pollutes the markup unnecessarily.

The Panel renders as a DIV in IE (maybe it was FF that got a table?), which
isn't *too* bad, but I'd stil rather have something that doesn't render
HTML. I've been through all the controls in the WebControls namespace on
msdn2.microsoft.com, but nothing seems to fit. I can see a *lot* of cases
when I'd like to conditionally display a group of controls without rending
an extra DIV, or moving them into a user control - I've got to be missing
something?
 
R

Randall Parker

Danny,

Not sure if this will fit your need but: In a similar problem what I do is query out
a dataset that has the two columns of information. But I only show one of those
database columns in the datalist. For the other visible column I have a third column
that I assign to the DataGrid's second column.

Then I do the query, then loop thru the dataset in code, and for each row set the
third column to an empty string (or DbNull.Value if you prefer) or to the string I
want to have displayed.

Only once I've made the pass thru the database in CodeBehind do I bind that dataset
to the DataGrid.

So basically you have to modify your dataset with a pass thru it to set an extra column.

e.g. "SELECT GigDate, RescheduledFromDate, 'HolderText' AS ToDisplayColumn FROM
YourTable";

Then loop thru and set the ToDisplayColumn to either "(rescheduled from xx/yy/zz)" or
to NULL or "".

You can also query out just the two columns and then add a column to the dataset
after the Fill call. I didn't do it that way. But it'd work just as well to get you
the third column. Then, again, you'd loop thru and set the third column in each row
with a foreach loop on the dataset.

Hope that helps...
 
D

Danny Tuppeny

Hi Randall,

That's certainly another way, but like the other methods, it doesn't really
sit right with me. Seems a like a lot of messing just to avoid using a Panel
because it outputs HTML :( It's also messy if I wanted to ouptut images
etc., since it means building the string in SQL, and throwing away all the
designer stuff (and ability to do server controls).

The designer's data support has *really* grown on me - I used to be of the
opinion "if you're not writing the code, it's bad code", but VWD has
completely changed my opinion, and since this site is a bit of a showcase,
I'm determined to write as little code as possible now (to show people why
they should be using asp.net 2.0, and not ASP :-( !!!)

I'll stick with the panel for now, and hope I don't have to do something
inline (that'd involve setting the panel to display: inline or float:
left!!). Thanks for you ideas anyhow :)

Danny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top