|
-
Mar 9th, 2005, 10:59 AM
#1
Thread Starter
Fanatic Member
asp:repeater - If/Then script in <ItemTemplate>
Here is an HTML extract from my ASPX document with a data repeater control:
Code:
<asp:repeater id="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>Name</th>
</tr>
<ItemTemplate>
<tr>
<td><%#Container.DataItem("Name_vc")></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:repeater>
However, I want to display a conditional value in my ItemTemplate dependant on the Name_vc field. I want to do something like this:
Code:
<ItemTemplate>
<td>
<%
If #Container.DataItem("Name_vc") = "Fred" THEN
Response.Write("Your name is Fred")
Else
Response.Write("Your name is not Fred")
End If
%>
</td>
</ItemTemplate>
I get a compiler error message: "BC30201: Expression expected." It highlights the line I have coloured red.
Does anybody know how I can get this to work?
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Mar 10th, 2005, 09:38 AM
#2
Frenzied Member
Re: asp:repeater - If/Then script in <ItemTemplate>
The way I do it is create a method that handles all the logic and passes back a string to be output.
Sorry it's in C# but my VB.NET ain't too hot!
Code:
<ItemTemplate>
<td>
<%# displayName((string) DataBinder.Eval(Container.DataItem, "Name_vc")) %>
</td>
</ItemTemplate>
and the method
Code:
string diplayName(string nameField) {
if (nameField == "Fred") {
return "Your name is Fred";
} else {
return "Your name is not Fred";
}
}
HTH
DJ
-
Mar 10th, 2005, 11:08 AM
#3
Thread Starter
Fanatic Member
Re: asp:repeater - If/Then script in <ItemTemplate>
Thanks.
However, my actual requirements are slightly more complex than that. What actually I want to do is insert a Hyperlink control for certain values and leave it blank for other values...
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Mar 10th, 2005, 11:14 AM
#4
Frenzied Member
Re: asp:repeater - If/Then script in <ItemTemplate>
In which case:
Code:
<ItemTemplate>
<td>
<asp:Label id="lblTest" runat="server" Text=<%# displayName((string) DataBinder.Eval(Container.DataItem, "Name_vc")) %> />
</td>
</ItemTemplate>
DJ
-
Mar 10th, 2005, 11:24 AM
#5
Thread Starter
Fanatic Member
Re: asp:repeater - If/Then script in <ItemTemplate>
I don't understand...
How will the above code become an asp:hyperlink under certain conditions?
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
Mar 10th, 2005, 11:41 AM
#6
Frenzied Member
Re: asp:repeater - If/Then script in <ItemTemplate>
I've misunderstood - ignore last post - let me just think this through...
DJ
-
Mar 10th, 2005, 11:47 AM
#7
Frenzied Member
Re: asp:repeater - If/Then script in <ItemTemplate>
OK if you place a placeholder control into the itemtemplate you will then have a control to tie dynamically created controls to.
Code:
<ItemTemplate>
<td>
<asp:PlaceHolder id="lblTest" runat="server" />
</td>
</ItemTemplate>
Then you need to create a method to handle the repeater ItemDataBound event. When this method is called you can then create dynamic controls (such as a Hyperlink) and add them to the PlaceHolder ControlCollection.
Have a search on Google and if you can't come up with any code let me know and I'll come up with an example.
HTH
DJ
-
Mar 11th, 2005, 03:13 AM
#8
Thread Starter
Fanatic Member
Re: asp:repeater - If/Then script in <ItemTemplate>
Thanks a lot for your advice.
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|