Using a datafield value as a function argument

Discussion in 'ASP .Net Datagrid Control' started by Dave, Dec 1, 2004.

  1. Dave

    Dave Guest

    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
     
    Dave, Dec 1, 2004
    #1
    1. Advertising

  2. 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>



    "Dave" <> wrote in message
    news:e%23N4HA$...
    > 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
    >
    >
     
    Ken Cox [Microsoft MVP], Dec 2, 2004
    #2
    1. Advertising

  3. Dave

    Dave Guest

    Re: Using a datafield value as a function argument - SOLVED

    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 [Microsoft MVP]" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:e%23N4HA$...
    > > I'm one baffled newbie. I know this is trivial, but I can't figure it

    out.
    > >

    [snip]
     
    Dave, Dec 2, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. richard

    get DataField value in UpdateCommand

    richard, Dec 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    425
    richard
    Dec 2, 2003
  2. Dinçer

    How to get DataField value of a cell

    Dinçer, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    6,846
    Eliyahu Goldin
    Dec 9, 2004
  3. Reckoner
    Replies:
    11
    Views:
    725
    Steven D'Aprano
    Jan 19, 2009
  4. AikidoGuy
    Replies:
    11
    Views:
    583
    Seebs
    Nov 21, 2011
  5. User1014
    Replies:
    1
    Views:
    197
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page