If Then statement inside of repeater control

G

Guest

Hello,

I am trying to use an If Then statement inside of a repeater control.
However, I am getting the following error:
Expression expected.

Any help would be appreciated.

Thanks in advance,

sck10


<asp:Repeater id="rptWebDocList" runat="server">
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>

<%# If Container.DataItem("Status") = " Total Not Reworked" Then %>

<tr><td width="280" colspan="3"><hr color=#0000FF></td></tr>
<tr>
<td width="120"><%# Container.DataItem("Status") %></div></td>
<td width="80"><%# Container.DataItem("StatusCount") %></div></td>
<td width="80"><%# Container.DataItem("PercentTotal") %>%</div></td>
</tr>

<% Else %>

<tr>
<td width="120"><%# Container.DataItem("Status") %></div></td>
<td width="80"><%# Container.DataItem("StatusCount") %></div></td>
<td width="80"><%# Container.DataItem("PercentTotal") %>%</div></td>
</tr>

<% End If %>

</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater></div></td>
 
S

Scott Mitchell [MVP]

Rather than trying to put a conditional statement in the <ItemTemplate>,
instead call a function from the <ItemTemplate>, passing in the
databound values that determine and are part of the output.

In your code portion, create a function with the same name and takes in
the same inputs, and returns a string. The string returned will be what
is displayed in the <ItemTemplate>

This explanation may not be that clear, but the following FAQ should
help: http://tinyurl.com/6muyn

It shows how to have the output customized based on a single field, but
the helper function could have an If statement and return a much more
complex HTML string.

hth

Hello,

I am trying to use an If Then statement inside of a repeater control.
However, I am getting the following error:
Expression expected.

Any help would be appreciated.

Thanks in advance,

sck10


<asp:Repeater id="rptWebDocList" runat="server">
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>

<%# If Container.DataItem("Status") = " Total Not Reworked" Then %>

<tr><td width="280" colspan="3"><hr color=#0000FF></td></tr>
<tr>
<td width="120"><%# Container.DataItem("Status") %></div></td>
<td width="80"><%# Container.DataItem("StatusCount") %></div></td>
<td width="80"><%# Container.DataItem("PercentTotal") %>%</div></td>
</tr>

<% Else %>

<tr>
<td width="120"><%# Container.DataItem("Status") %></div></td>
<td width="80"><%# Container.DataItem("StatusCount") %></div></td>
<td width="80"><%# Container.DataItem("PercentTotal") %>%</div></td>
</tr>

<% End If %>

</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater></div></td>


--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
S

Steven Cheng[MSFT]

Hi Sck10,

Thanks for your posting. As for the problem you mentioned, here are some of
my understandings:
1. The<% %> expression is only used in page(or UserControl, ascx) 's page
level template , but unable to be used in any asp.net web server control's
databinding template. We can only use <%# %> databinding expression to
bind value to asp.net server control's properties or template. For example:
<asp:TextBox ... Text="<% ...%>" ...> is not allowed,

we should use <asp:TextBox ... Text=<%# .. %> ..>
Also, the expression or code will be called when the TextBox (or its parent
container )'s DataBind method is called.

2. As for your problem, I think you should put the If ...else statement
logic into the <#% %> block . Forexample:

<ItemTemplate>

<%# GetHeader( Container.DataItem("Status") ) %>

<tr>
<td width="120"><%# Container.DataItem("Status") %></div></td>
<td width="80"><%# Container.DataItem("StatusCount") %></div></td>
<td width="80"><%# Container.DataItem("PercentTotal") %>%</div></td>
</tr>


</ItemTemplate>

GetHeader( string) is a helper function defined in the codebehind class
which return the correct html depend on the input param. such as:

#must be protected or public so that we can call it in aspx page's inline
code
protected string GetHeader(string status)
{
if(status== xxx)
{
return "<tr><td width=\"280\" colspan=\"3\"><hr
color=#0000FF></td></tr>";
}else
{
return string.Empty;
}
}


Hope helps. IF you have anything else unclear, please feel free to post
here. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Sck10,

Have you had a chance to check the suggestions in my last reply or have you
got any further ideas on this issue? If there're anything else we can help,
please feel free to post here. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

tcv

Joined
Nov 14, 2006
Messages
1
Reaction score
0
Hi all,


I have a similar case and I am using a datalist instead of reapeater. How do evaluate an xml data item (itemStatus) and change it (itemIndicator) content before binding ? Says , if itemStatus = in stock, I want to change out that item indicator to a different flash object. If itemStatus= not in stock, another flash object will be used? Thanks


Here is my code:

<form id="Form2" runat="server">
<asp:DataList BackColor="#d5d5d6"
id="cdcatalog"
RepeatColumns="4"
RepeateDirection="Horizontal"
gridlines="Both"
runat="server">

<ItemTemplate>
<%#Container.DataItem("itemTitle") Container.DataItem("itemIndicator")%%>
</ItemTemplate>

</asp:DataList>

xml
<catalog>
<cd>
<itemTitle>Empire Burlesque</itemTitle>
<itemStatus>in stock</itemStatus>
<itemIndicator>
<![CDATA[<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="12" height="12" id="blinking_red" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="blinking_green_lcd.swf" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="bgcolor" value="#d5d5d6" /><embed src="blinking_red.swf" menu="false" quality="high" bgcolor="#d5d5d6" width="12" height="12" name="blinking_red" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>]]>
</itemIndicator>

</cd>
</catalog>
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top