<asp:label> with the same text value....

Discussion in 'ASP .Net' started by Johnb41, Dec 13, 2004.

  1. Johnb41

    Johnb41 Guest

    I want an "order number" to be displayed more than once
    on my page.

    <script language="vb" runat="server">
    ...
    dim ordernumber as string = "123456"
    page.databind()
    ...
    </script>

    <p>text text text</p>
    <asp:label runat="server" text='<%# ordernumber %>' />
    <p>more text more text</p>
    <asp:label runat="server" text='<%# ordernumber %>' />
    <p>The order number could be displayed in other places on
    the page as well.</p>

    My error is "Name 'ordernumber' is not declared"

    Is this the right way to do this? Some help would be
    greatly appreciated!

    Thanks,
    John
     
    Johnb41, Dec 13, 2004
    #1
    1. Advertising

  2. "Johnb41" <> wrote in message
    news:0cfa01c4e138$2c1a8450$...
    >I want an "order number" to be displayed more than once
    > on my page.
    >
    > <script language="vb" runat="server">
    > ...
    > dim ordernumber as string = "123456"
    > page.databind()
    > ...
    > </script>
    >
    > <p>text text text</p>
    > <asp:label runat="server" text='<%# ordernumber %>' />
    > <p>more text more text</p>
    > <asp:label runat="server" text='<%# ordernumber %>' />
    > <p>The order number could be displayed in other places on
    > the page as well.</p>
    >
    > My error is "Name 'ordernumber' is not declared"
    >
    > Is this the right way to do this? Some help would be
    > greatly appreciated!


    Where did you put that script? What is the scope of ordernumber? It's the
    subroutine that "dim" line is in, and doesn't include the entire page.

    You need to declare such a thing at page level.

    Since I never use server-side script in a .aspx file, I can't tell you how
    to do this that way. If you were using codebehind, I'd tell you to put:

    Protected ordernumber As String = "123456"

    outside of any subroutine.


    John Saunders
     
    John Saunders, Dec 13, 2004
    #2
    1. Advertising

  3. Johnb41

    johnb41 Guest

    Thanks, it works when i declare the variable outside any sub routine.
    However, in my real page, the order number is grabbed from a hashtable
    item. I ran into a more serious problem that i wasn't able to fix...
    but i found a sufficient workaround.

    Inside my sub routine, i set the hashtable item (the order number) to
    be a session variable:

    session("ordernumber") = hashtable("OrderNumber")

    Then for the label, i did:

    <asp:label runat="server" text='<%# session("OrderNumber") %>' />

    Probably not the best solution, but it's good enough for me.
    Thanks again for your help!

    John
     
    johnb41, Dec 13, 2004
    #3
  4. "johnb41" <> wrote in message
    news:...
    > Thanks, it works when i declare the variable outside any sub routine.
    > However, in my real page, the order number is grabbed from a hashtable
    > item. I ran into a more serious problem that i wasn't able to fix...
    > but i found a sufficient workaround.
    >
    > Inside my sub routine, i set the hashtable item (the order number) to
    > be a session variable:
    >
    > session("ordernumber") = hashtable("OrderNumber")
    >
    > Then for the label, i did:
    >
    > <asp:label runat="server" text='<%# session("OrderNumber") %>' />
    >
    > Probably not the best solution, but it's good enough for me.


    Why not just declare the Hashtable outside of any sub, then reference it in
    the databinding expression like you're now doing with session?

    John Saunders
     
    John Saunders, Dec 13, 2004
    #4
  5. Johnb41

    johnb41 Guest

    That was the problem that i wasn't able to figure out.

    I added this line outside all sub routines:

    Dim Hashtable_OrderInfo as new Hashtable(Ctype(Session("OrderInfo"),
    Hashtable))

    A little history: The previous page added items to the hash table. To
    bring this info to the next page, i put the hashtable info into a
    session variable. Then on the next page i converted the session
    variable back into a hashtable (using the line of code above).

    The code works perfectly fine inside of a sub routine, but when it's
    outside of a routine, i get an error saying that i cannot use a session
    variable. The error gives me advice on how to fix it, but it still
    gives me the same error. I ran across this problem a couple days
    ago... after a full day of frustration, i gave up. So when this error
    came up today, I just didn't want to deal with it!

    John
     
    johnb41, Dec 13, 2004
    #5
  6. "johnb41" <> wrote in message
    news:...
    > That was the problem that i wasn't able to figure out.
    >
    > I added this line outside all sub routines:
    >
    > Dim Hashtable_OrderInfo as new Hashtable(Ctype(Session("OrderInfo"),
    > Hashtable))
    >
    > A little history: The previous page added items to the hash table. To
    > bring this info to the next page, i put the hashtable info into a
    > session variable. Then on the next page i converted the session
    > variable back into a hashtable (using the line of code above).
    >
    > The code works perfectly fine inside of a sub routine, but when it's
    > outside of a routine, i get an error saying that i cannot use a session
    > variable.


    Could you please post the error message?

    Also, try doing the above step by step so you can see what step it failed
    at. Try changing the hashtable to:

    Protected Hashtable_OrderInfo As Hashtable()

    and then, at the end of OnInit (or Page_Init, if that's how VB.NET does
    things):

    Dim ht As Hashtable = DirectCast(Session("OrderInfo"), Hashtable)
    Hashtable_OrderInfo = New Hashtable(ht)

    (use DirectCast when you know what the source type is, and you don't need it
    converted to another type - you just need the actual source type
    acknowledged)

    John Saunders
     
    John Saunders, Dec 13, 2004
    #6
    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. Harry
    Replies:
    3
    Views:
    968
    Matt Berther
    Apr 16, 2004
  2. Shapper
    Replies:
    2
    Views:
    742
    Scott Mitchell [MVP]
    May 7, 2005
  3. =?Utf-8?B?ZGF2aWQ=?=

    JavaScript assigns a value to label control text value?

    =?Utf-8?B?ZGF2aWQ=?=, May 24, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    23,907
    =?Utf-8?B?ZGF2aWQ=?=
    May 26, 2006
  4. CSharpner
    Replies:
    0
    Views:
    1,044
    CSharpner
    Apr 9, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    279
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page