[RESOLVED] How to programtically set template fields label value two one of two fields returned in a sqldatasource

 I have a sql data souce that returns several columns of data, and they are displayed in a DetailsView on a pretty simple vb.net page.  I'm stuggling with one field though for the details view.  It's a template field, and if ClientType=1 (this value is determined else where on the page), then the label in the template field should have Text='<%# Bind("ClientName") %>', but if the ClientType = 0, then the label in the template field should have Text='<%# Bind("ClientTemporaryName") %>'

How do I tell the details view to set the text for ClientName to one or the other? 

<asp:TemplateField HeaderText="Client" SortExpression="ClientName">
<ItemTemplate>
<asp:Label ID="lblClientNameForSummary" runat="server" Text='?????'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


 

I would personally put that logic into a SQL view so you only have to show one field in the label.  Otherwise, you will need to loop through each item in the DetailsView and update the text field from your code-behind.

 

Matt

I would put the logic in my SQL SELECT statement, with something like this:

SELECT ClientType,
 Case When ClientType = 0 Then ClientTemporaryName
      When ClientType = 1 Then ClientName
 END AS ClientNameForSummary
FROM  Clients

Then bind your lblClientNameForSummary with this: Text='<%# Bind("ClientNameForSummary") %>'

SELECT CASE is my new best friend... slightly behind ISNull!

Thank you..  So simple now.   

thoughts on "[RESOLVED] How to programtically set template fields label value two one of two fields returned in a sqldatasource "

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>