<%# Container.DataItem( "*" )%> in a datalist

N

.Net Sports

I'm having problems trying to get a record out of a resultset when
working with a Datalist control.
<%# Container.DataItem( "myfield" )%> is how I am displaying the
field , but when trying to do an If ..then..end if condition and
referring to a field in the resultset while inside the <asp:datalist</
asp:datalist> environment, can't find anything that works.
TIA
netsports
 
N

.Net Sports

<script runat="server">
'Handle page load event
Sub Page_Load(Sender As Object, E As EventArgs)
dim scounty as string
scounty = request.querystring("county")
dim strConnection as SQLConnection
dim objCommand as SQLCommand
Dim objDataReader As SQLDataReader
strConnection = New SqlConnection("Data Source=myserver.net;Initial
Catalog=mydbase;User Id=myuser;Password=xyz;")
''check here for county queried and sql
objCommand = New SqlCommand( "SELECT * from card_details where
issubcat = 0 order by category ASC", strConnection)
strConnection.Open()
objDataReader = objCommand.ExecuteReader()
dlstCats.DataSource = objDataReader
dlstCats.DataBind()
objDataReader.Close
strConnection.Close
End Sub
</script>
=====
<%
dim scounty as string
scounty = request.querystring("county")
%>
<asp:DataList id="dlstCats" runat="server" RepeatColumns="3"
width="340px" ItemStyle-BackColor="#E7F8FF" AlternatingItemStyle-
BackColor="#F0F6F7" cellspacing="3">

<ItemTemplate>
<% if scounty = madison then %>
<!-- get a field out of the datalist recordset -->
<% end if%>
</ItemTemplate>
</asp:DataList>
 
R

Riki

.Net Sports said:
I'm having problems trying to get a record out of a resultset when
working with a Datalist control.
<%# Container.DataItem( "myfield" )%> is how I am displaying the
field , but when trying to do an If ..then..end if condition and
referring to a field in the resultset while inside the <asp:datalist</
asp:datalist> environment, can't find anything that works.

If blocks and loops are not allowed in databinding blocks.

Move the if block to a so-called helper function (inside your code section):

Protected Function myHelper(obj As Object)
If obj Is Nothing Then Return ""
If obj.ToString = 'myvalue' Then
Return 'myresult'
Else
' etc...
End If
End Function

Then, call the function from your databinding block:

<%# myHelper(Container.DataItem("myfield") %>

Riki
 
G

Guest

Hi there again,

You are trying to mix classic ASP code blocks with ASP.NET. Could you please
forget about code <%%> blocks ??

<script runat="server">

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim scounty As String
scounty = Request.QueryString("county")
Dim strConnection As SQLConnection
Dim objCommand As SQLCommand
Dim objDataReader As SQLDataReader
strConnection = New SqlConnection( _
"Data Source=myserver.net;" & _
"Initial Catalog=mydbase;" & _
"User Id=myuser;Password=xyz;")
''check here for county queried and sql
objCommand = New SqlCommand( _
"SELECT * from card_details " & _
"where issubcat = 0 order by category ASC", _
strConnection)
strConnection.Open()
objDataReader = objCommand.ExecuteReader()
dlstCats.DataSource = objDataReader
dlstCats.DataBind()
objDataReader.Close()
strConnection.Close()
End Sub

Public ReadOnly Property Country() As String
Get
Dim value As String = Request.QueryString("country")
Return IIf(value Is Nothing, String.Empty, value)
End Get
End Property

</script>


<asp:DataList ID="dlstCats" runat="server" RepeatColumns="3" Width="340px"
ItemStyle-BackColor="#E7F8FF" AlternatingItemStyle-BackColor="#F0F6F7"
CellSpacing="3">
<ItemTemplate>
<asp:Literal runat="server" ID="lt" Text='<%# Eval("MyField") %>'
Visible='<%# String.Equals(Country, "madison") %>' />
</ItemTemplate>
</asp:DataList>

Hope this helps

Milosz
 
N

.Net Sports

Hi there again,

You are trying to mix classic ASP code blocks with ASP.NET. Could you please
forget about code <%%> blocks ??

<script runat="server">

Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
Dim scounty As String
scounty = Request.QueryString("county")
Dim strConnection As SQLConnection
Dim objCommand As SQLCommand
Dim objDataReader As SQLDataReader
strConnection = New SqlConnection( _
"Data Source=myserver.net;" & _
"Initial Catalog=mydbase;" & _
"User Id=myuser;Password=xyz;")
''check here for county queried and sql
objCommand = New SqlCommand( _
"SELECT * from card_details " & _
"where issubcat = 0 order by category ASC", _
strConnection)
strConnection.Open()
objDataReader = objCommand.ExecuteReader()
dlstCats.DataSource = objDataReader
dlstCats.DataBind()
objDataReader.Close()
strConnection.Close()
End Sub

Public ReadOnly Property Country() As String
Get
Dim value As String = Request.QueryString("country")
Return IIf(value Is Nothing, String.Empty, value)
End Get
End Property

</script>

<asp:DataList ID="dlstCats" runat="server" RepeatColumns="3" Width="340px"
ItemStyle-BackColor="#E7F8FF" AlternatingItemStyle-BackColor="#F0F6F7"
CellSpacing="3">
<ItemTemplate>
<asp:Literal runat="server" ID="lt" Text='<%# Eval("MyField") %>'
Visible='<%# String.Equals(Country, "madison") %>' />
</ItemTemplate>
</asp:DataList>

Hope this helps

Milosz

--
Milosz







- Show quoted text -

OK, thankx!
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top