This is a migrated thread and some comments may be shown as answers.

[Solved] OnItemDataBound and ItemTemplate

2 Answers 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Taylor
Top achievements
Rank 1
Taylor asked on 07 Apr 2009, 08:36 PM
I'm in a bit of a pickle.  I'm trying to custom-format some data and edit the data as it occurs.

However, sometimes the data is not present.  For example, the data may be in the normal format:

A
>B
>C
>D
>E

(where > is a subelement of the parent)  However, sometimes my data comes back missing B and C, but still successfully renders "normally", but those fields are left blank.

My ItemTemplate displays things in a different format, i.e.

<ItemTemplate>
  <table border=0><tr><td><%# Eval( "A" ) %></td><td><%# Eval( "B" ) %></td></tr>
  <tr>...Rest of the elements here</tr>
  </table>
</ItemTemplate>

However, when I try to render "B" and it is missing, it causes all kinds of nasty issues.  Is there a way to check if this exists in the ItemDataBound event and if not, inject a new value?

Also, is there a way to possibly "skip" it on the ItemDataBound, i.e. if a column is missing, don't even bother showing the entire row?  Is there any kind of GridDataItem.ContainsKey() or something to verify existence of keys, etc?

Thanks!

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Apr 2009, 07:42 AM
Hi Taylor,

Try with the following approach and see if it helps to some extent.

ASPX:
 
<ItemTemplate> 
 <table border=0
   <tr> 
     <td visible='<%# DataBinder.Eval (Container.DataItem,"A")!=null?true:false %>' ><%# Eval( "A" ) %></td
     <td visible='<%# DataBinder.Eval (Container.DataItem,"B")!=null?true:false %>'><%# Eval( "B" ) %></td
   </tr> 
   
   <tr>...Rest of the elements here</tr> 
 
 </table> 
</ItemTemplate> 


Shinu
0
Taylor
Top achievements
Rank 1
answered on 08 Apr 2009, 12:40 PM
I get more or less the same error.

DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'A'

Code looks like:

<%# DataBinder.Eval( Container.DataItem, "A" ) != null ? Eval( "A" ) : "" %> 

So it's for sure failing on the DataBinder.Eval section of it...  Is there another way to verify if something is in a collection?
Tags
Grid
Asked by
Taylor
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Taylor
Top achievements
Rank 1
Share this question
or