Results 1 to 8 of 8

Thread: asp:repeater - If/Then script in <ItemTemplate>

  1. #1

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    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.

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

  3. #3

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    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.

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

  5. #5

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    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.

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: asp:repeater - If/Then script in <ItemTemplate>

    I've misunderstood - ignore last post - let me just think this through...

    DJ

  7. #7
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

  8. #8

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    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
  •  



Click Here to Expand Forum to Full Width