Click to See Complete Forum and Search --> : using container.dataitem in an if statement?
MdWebtress
08-26-2005, 10:23 AM
I'm using ASP.NET, and I'm trying to us an if statement for a container in a datarepeater. Here's an example of what I'm trying to do:
<%# if Container.dataitem("someField") = 1 then %>
do this...
<% else %>
do something else...
<% end if %>
But, of course, I'm getting errors that container isn't declared. I know that <%# container.dataitem("someField") %> will work, but I'm not sure how to use that in an if-then-else statement.
Memnoch
08-26-2005, 10:28 AM
Do it like this
In the HTML
<%# MyFunction(Databinder.Eval(Container.DataItem, "SomeField")) %>
in code
Public MyFunction (ByVal strValue As Type) As ReturnType
If strValue = 1 Then
Return SomeValue
Else
Return SomeOtherValue
End If
End Function
MdWebtress
08-26-2005, 11:10 AM
I know that would work, but... The one part that I want to show is a table with bound data in the datarepeater. I guess I could always use a panel..
EDIT: Seems it didn't like me setting the panels visibility in the code-behind section (the panels were in the datagrid).
martini.mara
11-26-2007, 11:02 AM
Do it like this
In the HTML
<%# MyFunction(Databinder.Eval(Container.DataItem, "SomeField")) %>
in code
Public MyFunction (ByVal strValue As Type) As ReturnType
If strValue = 1 Then
Return SomeValue
Else
Return SomeOtherValue
End If
End Function
hi.
You can not returna server control
ex.
Public MyFunction (ByVal strValue As Type) As ReturnType
If strValue = 1 Then
Return "<asp:Button id='id' runat='server'"
what's the solution?
it should be render or...?
hsoumare
07-17-2009, 01:53 PM
Hi, I have the same problem as discussed above, but I need to have different server controls depending on the value of a container data item. How would I go about this?
Below is an example clarifying:
<%#InstrumentSelector2(Container.DataItem("instrument"))%>
<%If InstrumentSelector2(Container.DataItem("instrument"))Then%>
<td>
<font class="regular">
<b>Horizontal Location (pixels):</b>
<asp:TextBox ID="txtEditTermXLoc" Text='<%#Container.DataItem("x_pixel_loc")%>' Columns="3" MaxLength="5" Runat="Server" />
</font>
</td>
<% End If%>
I tried creating a server side function that would return the correct xml but the server controls didnt pop up. Any help would be appreciated.