Using a datafield value as a function argument

D

Dave

I'm one baffled newbie. I know this is trivial, but I can't figure it out.

Using inline aspx code, I have a function called dbIntegerToString(i as
Integer, s as String) as String

Down in the html code, I have a datagrid, a stub of which follows:

<asp:TemplateColumn>
<ItemTemplate>
<%# Container.DataItem("Lessons") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText = "Recommendation">
<ItemTemplate>
<%# Container.DataItem("Recommendation") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText = "DOT">
<ItemTemplate>
<%= dbIntegerToString(3, "Category") %>
</ItemTemplate>
</asp:TemplateColumn>

All this works fine.

But my issue is in the last ItemTemplate. What I want to do, instead of
passing '3', is to pass a data value I read in when all the other data
values were read (in another vb procedure). The field is called "DOT", and
is in the same SELECT statement as the other fields you see.

Replacing '3' with 'Container.DataItem("DOT")' doesn't work (Compiler Error
Message: BC30451: Name 'Container' is not declared.), and other feeble
attempts also result in errors.

Is there anybody out there that can set me straight on this?

Thanks in advance,

Dave
 
K

Ken Cox [Microsoft MVP]

Hi Dave,

It may just be a syntax issue. I took your code and played with it. The code
below *seems* to do something like what you might need.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<%@ Page Language="vb" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>dgcontainer</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="visualbasic" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub
Function dbIntegerToString(ByVal i As Integer, ByVal s As String) As
String
Return "Test" & i.ToString
End Function
Function CreateDataSource() As system.data.DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Lessons", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("Recommendation", GetType(String)))
dt.Columns.Add(New DataColumn _
("Category", GetType(Double)))
dt.Columns.Add(New DataColumn _
("DOT", GetType(Boolean)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
</script>
</head>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
AutoGenerateColumns="False">
<columns>
<asp:templatecolumn>
<itemtemplate>
<%# Container.DataItem("Lessons") %>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn HeaderText="Recommendation">
<itemtemplate>
<%# Container.DataItem("Recommendation") %>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn HeaderText="DOT">
<itemtemplate>
<%#
dbIntegerToString(Container.DataItem("Lessons"),Container.DataItem("Category")
) %>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>
 
D

Dave

Ken,

Beeyootiful. Just changing
'<%=dbIntegerToString(Container.DataItem("DOT")...' to
'<%#dbIntegerToString(Container.DataItem("DOT")...' fixed it.

If I may explore this a little bit, what does the '#' signify? I read in a
tutorial somewhere that you use a '=' when calling a function. And I just
inferred that the '#' referred to a database value. Perhaps I'm reading the
wrong tutorials?

Dave

Ken Cox said:
Hi Dave,

It may just be a syntax issue. I took your code and played with it. The code
below *seems* to do something like what you might need.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
[snip]
<itemtemplate>
<%#
dbIntegerToString(Container.DataItem("Lessons"),Container.DataItem("Category
")
) %>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>



Dave said:
I'm one baffled newbie. I know this is trivial, but I can't figure it out.
[snip]
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top