if else in datagrid item template

  • Thread starter Thread starter Allan Ebdrup
  • Start date Start date
A

Allan Ebdrup

I'm using dotNet 2.0 and VS 2005.
I have a datagrid that I'm binding to an object data source (ODS), the
objects in the ODS have an FromDate property and an Id property.
Now I want to display a hyperling if(FromDate>DateTime.Now) and a label if
not, how do I do this the best way?
I tried to do a if else in the item template, but that doesn't work:
------------------
<%#
if(((OFiR.Recruitment.Staffing.Reporting.PositionPostingReporting)Container.DataItem).FromDate
DateTime.Now) {%>

<asp:HyperLink Target="_blank"
NavigateUrl='<%#"/CPI/V2/Apps/PositionPosting/opening.aspx?id=" + Eval("Id")
%>' Text='<%# Eval("Title") %>' runat="server"></asp:HyperLink>

<%# } else {%>

<asp:Label runat="server" Text='<%# Bind("Title") %>'
id="Label1"></asp:Label>

<%# } %>
 
Hi Allan,

You can rely on the fact that HyperLink is similar to a Label when NavigateUrl
is empty and use a conditional IF to handle the bool logic inside the
NavigateUrl property itself:

<asp:HyperLink runat="server" Target="_blank" Text='<%# Eval("Title") %>'
NavigateUrl='<%# ((DateTime) Eval("FromDate") > DateTime.Now) ?
"/CPI/V2/Apps/PositionPosting/opening.aspx?id=" + Eval("Id") : string.Empty
%>'
/>
 
Hi Allan,

The "<%#" directive is for data binding expression only, I don't think that
can be used to store code block.

I suggest you create a simple WebUserControl with a property FromDate:

private DateTime _fromDate;

public DateTime FromDate
{
get { return _fromDate; }
set { _fromDate = value; }
}

Then you can edit the .ASCX source as:

<% if (FromDate > DateTime.Now) { %>
<asp:HyperLink ID="HyperLink1" Target="_blank"
NavigateUrl='<%# "/CPI/V2/Apps/PositionPosting/opening.aspx?id=" +
Eval("Id") %>'
Text='<%# Eval("Title") %>' runat="server"></asp:HyperLink>
<% } else { %>
<asp:Label runat="server" Text='<%# Eval("Title") %>'
id="Label1"></asp:Label>
<% } %>

Please note that we're using "<% %>" to eclose the code block.

And then we can use the WebUserControl in the ItemTemplate:

<%@ Register Src="WebUserControl1.ascx" TagName="WebUserControl1"
TagPrefix="uc1" %>
...

<asp:TemplateColumn>
<ItemTemplate>
<uc1:WebUserControl1 FromDate='<%# Eval("FromDate") %>'
id="WebUserControl1_1" runat="server">
</uc1:WebUserControl1>


Let me know if this is what you wanted.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Dave Sexton said:
Hi Allan,

You can rely on the fact that HyperLink is similar to a Label when
NavigateUrl is empty and use a conditional IF to handle the bool logic
inside the NavigateUrl property itself:

<asp:HyperLink runat="server" Target="_blank" Text='<%# Eval("Title") %>'
NavigateUrl='<%# ((DateTime) Eval("FromDate") > DateTime.Now) ?
"/CPI/V2/Apps/PositionPosting/opening.aspx?id=" + Eval("Id") :
string.Empty %>'
/>

Thanks that did the trick

Kind Regards,
Allan Ebdrup
 

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

Back
Top