Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

conditional output in itemtemplate of datalist

I am showing two different columns in the itemtemplate section of datalist. first column appears fine in first <td> but for second column I want to check the value(say boolean) and depending on that I want to show different images in the <td>. This in classic asp is easy as we can put an "If" statement. Any idea how to do this in asp.net especially inside the itemtemplate secion of datalist control as it is always databinder expression

Thanks in advance

RAM
Thursday, May 22, 2003


You could do it in (at least) two different ways:

1- Use the ternary conditional expression (?:) inside the databinding tag.

Suppose you want to decide between displaying contents of 'field2' or 'field3' based on the value of 'field1' in the Text property of some text box:

...
<ItemTemplate>
  <asp:TextBox runat="server" id="txtWhatever" Text=<%# ((bool)DataBinder.Eval(Container.DataItem, "field1") == true? DataBinder.Eval(Container.DataItem, "field2") : DataBinder.Eval(Container.DataItem, "field3") ) %> />
</ItemTemplate>
...

The problem with this approach is that if the condition is somewhat complex, you end with a real long and messy code. It is difficult to perform tests on other page elements also.

2- The cleaner approach, in my opinion, is to process the DataList.ItemDataBound event.

From the .Net Framework help:

"The ItemDataBound event is raised after an item is data bound to the DataList control. This event provides you with the last opportunity to access the data item before it is displayed on the client. After this event is raised, the data item is nulled out and no longer available."

This means that during this event, you have access to the particular Item (DataListItem instance) generated for each of you DataSource's items. As expected, this event gets rised as many times as the number of items that the DataList will display once rendered.  Once the execution exits this procedure, that particular item will be actually rendered as HTML and sent to the response HTTP stream.

The event args for this event contains the DataListItem instance in the e.Item property.

So if you were going to replicate the same behavoir of the last example, you would need to define first youe template column:

...
<ItemTemplate>
  <asp:TextBox runat="server" id="txtWhatever" />
</ItemTemplate>
...

And in the ItemDataBound event:

void Item_DataBound(Object sender, DataListItemEventArgs e)
  {

    // get a reference to the control you want to handle
    TextBox txtControl = (TextBox)e.Item.FindControl("txtWhatever");

    // perform some weird and crazy conditions
    //here to set the

    string someValue = DoSomeComplexConditional();

    // if you want , you can use also the same databinding
    // expressions to perform test on the fields of
    // the record associated with this DataListItem:

    if( (bool)DataBinder.Eval(e.Item.DataItem, "field1") )
    }
          someValue = GetSomeOtherValue();
    {

    // set the control properties
    txtControl.Text = someValue;
}


As you see, the trick is to use the Item.FindControl() method to get the specific control from the Item.Controls collection.

If you wanted, you could also add new controls to the DataListItem in this event. Like this:

...
TextBox txtControl = new TextBox();
txtControl.Id = "txtAdditional";
txtControl.Text = "Look!, dynamic controls!";
e.Item.Controls.Add(txtControl);
...

For instance, you might decide not to especify controls for the column at design time:

...
<ItemTemplate>
</ItemTemplate>
...

And add a different control (or controls) for every item of your list.


Hope it helps.

Sergio
Friday, May 23, 2003

<OT>  By the way, It would be nice if the forum had some way to format posted source code in a fixed width font =(

Sergio
Friday, May 23, 2003

Thanks Sergio for your input..

I got another suggestion from Scott and here is the link for that

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=226500

See this is the wonderful benefit for the forum as the same problem can be solved in three different ways!!

RAM
Friday, May 23, 2003

*  Recent Topics

*  Fog Creek Home