Call Function from aspx

Discussion in 'ASP .Net' started by Miro, Apr 18, 2010.

  1. Miro

    Miro Guest

    I am really having trouble with this. I am trying to call a function from
    my .vb file within my .aspx file.
    My current results is that nothing happens.
    Wondering if someone can help me out.

    <form id="form1" runat="server">
    <div>
    <br />
    <asp:Label ID="Label1" runat="server" Text="<%= helloworld()
    %>"></asp:Label>
    <br />
    </div>
    </form>

    and from my .vb file:


    Partial Class _Default
    Inherits System.Web.UI.Page

    Function helloworld() As String
    Return "From.vb"
    End Function
    End Class

    How do I get the label1 to get the text from the helloworld function on
    load?

    I know I can use "on_load event", but I want to try to call a function like
    this just like as if it was databound.

    Thanks,
    Miro, Apr 18, 2010
    #1
    1. Advertising

  2. On Apr 18, 7:20 am, "Miro" <> wrote:
    > I am really having trouble with this.  I am trying to call a function from
    > my .vb file within my .aspx file.
    > My current results is that nothing happens.
    > Wondering if someone can help me out.
    >
    >     <form id="form1" runat="server">
    >     <div>
    >        <br />
    >     <asp:Label ID="Label1" runat="server" Text="<%= helloworld()
    > %>"></asp:Label>
    >     <br />
    >         </div>
    >     </form>
    >
    > and from my .vb file:
    >
    > Partial Class _Default
    >     Inherits System.Web.UI.Page
    >
    >     Function helloworld() As String
    >         Return "From.vb"
    >     End Function
    > End Class
    >
    > How do I get the label1 to get the text from the helloworld function on
    > load?
    >
    > I know I can use "on_load event", but I want to try to call a function like
    > this just like as if it was databound.
    >
    > Thanks,


    The function must be marked as protected or public in order to be
    accessible from the aspx page.

    Protected Function helloworld() As String

    Read more about Access Specifiers
    http://msdn.microsoft.com/en-us/library/ms973875.aspx

    Hope this helps
    Alexey Smirnov, Apr 18, 2010
    #2
    1. Advertising

  3. "Miro" <> wrote in message
    news:O#...

    > I know I can use "on_load event", but I want to try to call a function
    > like this just like as if it was databound.


    Firstly, I think you're making things harder than they need to be...

    However, consider this:

    Label1 <asp:Label ID="Label1" runat="server"><%=
    helloworld()%></asp:Label><br />
    Label2 <asp:Label ID="Label2" runat="server" Text="<%# helloworld()%>" /><br
    />
    Label3 <asp:Label ID="Label3" runat="server" Text="<%= helloworld()%>" />

    Label1 will always work.
    Label2 uses databinding syntax, so will work only if you call DataBind() in
    before the HTML has been streamed to the client, e.g. in Page_Load
    Label3, as it stands, will never work.


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Apr 18, 2010
    #3
  4. "Miro" <> wrote in message
    news:O#...
    > I am really having trouble with this. I am trying to call a function from
    > my .vb file within my .aspx file.
    > My current results is that nothing happens.
    > Wondering if someone can help me out.
    >
    > <form id="form1" runat="server">
    > <div>
    > <br />
    > <asp:Label ID="Label1" runat="server" Text="<%= helloworld()
    > %>"></asp:Label>
    > <br />
    > </div>
    > </form>
    >
    > and from my .vb file:
    >
    >
    > Partial Class _Default
    > Inherits System.Web.UI.Page
    >
    > Function helloworld() As String
    > Return "From.vb"
    > End Function
    > End Class
    >
    > How do I get the label1 to get the text from the helloworld function on
    > load?
    >
    > I know I can use "on_load event", but I want to try to call a function
    > like this just like as if it was databound.


    There are two ways to bind in .NET. One is declarative, in the tags, as you
    are attempting. Note that this can tightly couple your logic very easy.
    Follow Alexy's post to help. Mark has also suggested a simpler syntax,
    although I don't believe it gets around the issue Alexey mentioned.

    The second is to bind in events to controls. It is a much nicer syntax, esp.
    with ASP.NET controls. I know you don't want to go this direction, for some
    reason, but it is really a better way to handle the issue in almost every
    instance.


    --
    Peace and Grace,
    Greg

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    ************************************************
    | Think outside the box! |
    ************************************************
    Gregory A. Beamer, Apr 18, 2010
    #4
  5. "Gregory A. Beamer" <> wrote in message
    news:...

    > Mark has also suggested a simpler syntax, although I don't believe it gets
    > around the issue Alexey mentioned.


    It doesn't, but that's the artificial restriction that the OP has imposed...


    > The second is to bind in events to controls. It is a much nicer syntax,
    > esp. with ASP.NET controls. I know you don't want to go this direction,
    > for some reason, but it is really a better way to handle the issue in
    > almost every instance.


    Indeed. As I mentioned, the OP is making things unnecessarily difficult and
    complicated...


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Apr 18, 2010
    #5
  6. Miro

    Miro Guest

    Thanks #2 worked. That is what I was looking for.

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Miro" <> wrote in message
    > news:O#...
    >
    >> I know I can use "on_load event", but I want to try to call a function
    >> like this just like as if it was databound.

    >
    > Firstly, I think you're making things harder than they need to be...
    >
    > However, consider this:
    >
    > Label1 <asp:Label ID="Label1" runat="server"><%=
    > helloworld()%></asp:Label><br />
    > Label2 <asp:Label ID="Label2" runat="server" Text="<%# helloworld()%>"
    > /><br />
    > Label3 <asp:Label ID="Label3" runat="server" Text="<%= helloworld()%>" />
    >
    > Label1 will always work.
    > Label2 uses databinding syntax, so will work only if you call DataBind()
    > in before the HTML has been streamed to the client, e.g. in Page_Load
    > Label3, as it stands, will never work.
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Miro, Apr 19, 2010
    #6
  7. Miro

    Miro Guest

    It may look like I am making things difficult, but I am learning asp.net and
    javascript and trying to setup some javascript variables and items from
    functions.

    I dumbed down the example I was looking for ( a lot ) in hopes that it makes
    sense to me ( which now I think it does ), and now I can go back and look at
    a more complicated example online.

    Thanks,

    Miro

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Gregory A. Beamer" <> wrote in message
    > news:...
    >
    >> Mark has also suggested a simpler syntax, although I don't believe it
    >> gets around the issue Alexey mentioned.

    >
    > It doesn't, but that's the artificial restriction that the OP has
    > imposed...
    >
    >
    >> The second is to bind in events to controls. It is a much nicer syntax,
    >> esp. with ASP.NET controls. I know you don't want to go this direction,
    >> for some reason, but it is really a better way to handle the issue in
    >> almost every instance.

    >
    > Indeed. As I mentioned, the OP is making things unnecessarily difficult
    > and complicated...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Miro, Apr 19, 2010
    #7
  8. On Apr 19, 6:22 am, "Miro" <> wrote:
    > It may look like I am making things difficult, but I am learning asp.net and
    > javascript and trying to setup some javascript variables and items from
    > functions.
    >
    > I dumbed down the example I was looking for ( a lot ) in hopes that it makes
    > sense to me ( which now I think it does ), and now I can go back and look at
    > a more complicated example online.
    >
    > Thanks,
    >
    > Miro
    >
    > "Mark Rae [MVP]" <> wrote in messagenews:...
    >
    >
    >
    > > "Gregory A. Beamer" <> wrote in message
    > >news:...

    >
    > >> Mark has also suggested a simpler syntax, although I don't believe it
    > >> gets around the issue Alexey mentioned.

    >
    > > It doesn't, but that's the artificial restriction that the OP has
    > > imposed...

    >
    > >> The second is to bind in events to controls. It is a much nicer syntax,
    > >> esp. with ASP.NET controls. I know you don't want to go this direction,
    > >> for some reason, but it is really a better way to handle the issue in
    > >> almost every instance.

    >
    > > Indeed. As I mentioned, the OP is making things unnecessarily difficult
    > > and complicated...

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVP
    > >http://www.markrae.net


    Good that you know how it works now. Just one remark: in general there
    is no need to use the Label control (for client scripting). It can be
    also done in the following way

    Code-behind

    Protected Dim HelloWorld As String = "Hej"

    ASPX

    .....
    <script>
    var x = <%=HelloWorld %>
    </script>
    .....

    This would "inject" the value of the HelloWorld variable (it can be a
    function too) from ASP.NET

    Hope this helps
    Alexey Smirnov, Apr 19, 2010
    #8
  9. "Alexey Smirnov" <> wrote in message
    news:...

    > <script>


    Don't forget type="text/javascript"


    > var x = <%=HelloWorld %>


    Don't forget the semi-colon at the end of JavaScript lines... ;-)


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Apr 19, 2010
    #9
  10. Mark Rae [MVP] wrote:
    > "Alexey Smirnov" wrote in message
    >
    >> <script>

    >
    > Don't forget type="text/javascript"
    >
    >
    >> var x = <%=HelloWorld %>

    >
    > Don't forget the semi-colon at the end of JavaScript lines... ;-)


    ....and quotes around the string.

    This computer-programming thing needs so many fiddly bits, doesn't it <g>

    --
    Andrew
    Andrew Morton, Apr 19, 2010
    #10
  11. "Andrew Morton" <> wrote in message
    news:...

    > ...and quotes around the string.
    >
    > This computer-programming thing needs so many fiddly bits, doesn't it <g>


    :)


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
    Mark Rae [MVP], Apr 19, 2010
    #11
  12. Miro

    Miro Guest

    lol

    Yeah I was trying first to do it with just a label, and then set a variable
    in my .js file to a value from my asp.net function which I have now got
    working.

    I know once i got the function working with protected i can do a "
    protected dim bla as string = "hi"

    Thanks for all the help.

    I need to read up on 'protected/friend ..... and so on"

    Cheers'

    Miro

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "Andrew Morton" <> wrote in message
    > news:...
    >
    >> ...and quotes around the string.
    >>
    >> This computer-programming thing needs so many fiddly bits, doesn't it <g>

    >
    > :)
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Miro, Apr 19, 2010
    #12
  13. On Apr 19, 9:01 pm, "Miro" <> wrote:
    > lol
    >
    > Yeah I was trying first to do it with just a label, and then set a variable
    > in my .js file to a value from my asp.net function which I have now got
    > working.
    >
    > I know once i got the function working with protected i can do a   "
    > protected dim bla as string = "hi"
    >
    > Thanks for all the help.
    >
    > I need to read up on 'protected/friend .....  and so on"
    >
    > Cheers'
    >
    > Miro
    >
    > "Mark Rae [MVP]" <> wrote in messagenews:...
    >
    >
    >
    > > "Andrew Morton" <> wrote in message
    > >news:...

    >
    > >> ...and quotes around the string.

    >
    > >> This computer-programming thing needs so many fiddly bits, doesn't it <g>

    >
    > > :)

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVP
    > >http://www.markrae.net


    *.js is not processed by asp.net engine by default. The trick with
    "injection" will work when you will put your client script inside
    *.aspx
    Alexey Smirnov, Apr 20, 2010
    #13
  14. Miro

    Miro Guest

    Ahh that is good to know.

    I currently have it embedded in my aspx but I will be moving it to my .js
    file which then by what you are saying - it would be breaking it.

    I will test it out ...thanks

    Miro

    "Alexey Smirnov" <> wrote in message
    news:...
    > On Apr 19, 9:01 pm, "Miro" <> wrote:
    >> lol
    >>
    >> Yeah I was trying first to do it with just a label, and then set a
    >> variable
    >> in my .js file to a value from my asp.net function which I have now got
    >> working.
    >>
    >> I know once i got the function working with protected i can do a "
    >> protected dim bla as string = "hi"
    >>
    >> Thanks for all the help.
    >>
    >> I need to read up on 'protected/friend ..... and so on"
    >>
    >> Cheers'
    >>
    >> Miro
    >>
    >> "Mark Rae [MVP]" <> wrote in
    >> messagenews:...
    >>
    >>
    >>
    >> > "Andrew Morton" <> wrote in message
    >> >news:...

    >>
    >> >> ...and quotes around the string.

    >>
    >> >> This computer-programming thing needs so many fiddly bits, doesn't it
    >> >> <g>

    >>
    >> > :)

    >>
    >> > --
    >> > Mark Rae
    >> > ASP.NET MVP
    >> >http://www.markrae.net

    >
    > *.js is not processed by asp.net engine by default. The trick with
    > "injection" will work when you will put your client script inside
    > *.aspx
    Miro, Apr 20, 2010
    #14
  15. On Apr 20, 4:03 pm, "Miro" <> wrote:
    > Ahh that is good to know.
    >
    > I currently have it embedded in my aspx but I will be moving it to my .js
    > file which then by what you are saying - it would be breaking it.
    >
    > I will test it out ...thanks
    >
    > Miro
    >


    It's related only to that part where you get the values from the code-
    behind. That means that you should keep that part in aspx and the rest
    of the script can be moved to the external js-file.

    Like this

    <script language="javascript" type="text/javascript">
    var x = '<%= fromcodebehind %>';
    </script>
    <script src="external.js" type="text/javascript"></script>

    Hope this helps
    Alexey Smirnov, Apr 20, 2010
    #15
  16. Mark Rae [MVP], Apr 20, 2010
    #16
  17. Miro

    Miro Guest

    It works... that's what matters to me!

    Cheers'

    M.

    "Mark Rae [MVP]" <> wrote in message
    news:#...
    > "Alexey Smirnov" <> wrote in message
    > news:...
    >
    >> <script language="javascript" type="text/javascript">

    >
    > The language attribute of the <script /> tag was deprecated nearly
    > *fourteen* years ago - it should not be used at all:
    > http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.1
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Miro, Apr 20, 2010
    #17
  18. Miro wrote:
    > It works... that's what matters to me!
    >


    And we all forgot to decorate the Javascript, in case there's a "<" or "&"
    in it, to make sure it's XHTML-compliant

    <script type="text/javascript">
    //<![CDATA[
    // code goes here
    //]]>
    </script>

    --
    Andrew
    Andrew Morton, Apr 21, 2010
    #18
    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. John M
    Replies:
    1
    Views:
    1,118
    Kumar Reddi
    May 29, 2005
  2. Progman
    Replies:
    2
    Views:
    1,887
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 13, 2006
  3. MadHatter51
    Replies:
    1
    Views:
    850
    Hakan Lambracht
    May 17, 2006
  4. grbgooglefan
    Replies:
    2
    Views:
    411
    Pascal Bourguignon
    Jan 30, 2008
  5. grbgooglefan
    Replies:
    4
    Views:
    431
    Kenny McCormack
    Jan 30, 2008
Loading...

Share This Page