Condition in ASP Repeater
I am trying to put a condition into my ASP repeater tag. I have a table being created and one of the td items is a link. The problem is I need to create a link upon checking the value in one the variable that is in the repeater container. Here 开发者_Python百科is my code:
<td><%#((VWApp.Code.TrackDM)Container.DataItem).CdNo%></td>
<td><%#((VWApp.Code.TrackDM)Container.DataItem).ShippingNo%></td>
Now i need to check the value of ShippingNo and rather than displaying it need to display a link. i tried to write an if condition like this:
if(((VWApp.Code.TrackDM)Container.DataItem).ShippingNo. .. )
{
// do processing and generate a link that needs to displayed
}
But i get error when trying to do this. Can anybody guide me to the right way of doing this?
Any ideas and suggests are highly appreciated.
Use a <asp:Hyperlink>
inside your <td>
, and bind its Visible
property to an expression which will check ShippingNo and return true if it represents a valid link.
You can always go the old-fashioned route of not using a repeater at all, but rather inline code, where you can use whatever code you need. Something like:
<% foreach (SomeObject obj in MyObjectCollection) { %>
<td>
<% if (obj.SomeProperty == something) { %>
<a href="<%= /* build link */ %>">Click Me</a>
<% } else { %>
Some Text
<% } %>
</td>
<% } %>
It's the classic ASP way of doing things, but I've found it works better for some situations. Sometimes it's just easier to do things with real code as opposed to working within the boundaries of a Repeater or GridView.
Encapsulate your logic in a User Control and pass it the parameters it needs to makes display decisions:
<uc:linkdisplay id="linkdisplay1" runat="server"
CdNo='<%#((VWApp.Code.TrackDM)Container.DataItem).CdNo %>'
ShippingNo='<%#((VWApp.Code.TrackDM)Container.DataItem).ShippingNo%>' />
精彩评论