PC Review


Reply
Thread Tools Rating: Thread Rating: 6 votes, 1.67 average.

Call Function from aspx

 
 
Miro
Guest
Posts: n/a
 
      18th Apr 2010
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,

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      18th Apr 2010
On Apr 18, 7:20*am, "Miro" <m...@beero.com> 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
 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      18th Apr 2010
"Miro" <(E-Mail Removed)> wrote in message
news:O#(E-Mail Removed)...

> 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

 
Reply With Quote
 
Gregory A. Beamer
Guest
Posts: n/a
 
      18th Apr 2010


"Miro" <(E-Mail Removed)> wrote in message
news:O#(E-Mail Removed)...
> 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! |
************************************************

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      18th Apr 2010
"Gregory A. Beamer" <(E-Mail Removed)> wrote in message
news:E38E199E-FA4A-464C-A3B1-(E-Mail Removed)...

> 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

 
Reply With Quote
 
Miro
Guest
Posts: n/a
 
      19th Apr 2010
Thanks #2 worked. That is what I was looking for.

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:2D55D6FD-620D-476F-B143-(E-Mail Removed)...
> "Miro" <(E-Mail Removed)> wrote in message
> news:O#(E-Mail Removed)...
>
>> 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


 
Reply With Quote
 
Miro
Guest
Posts: n/a
 
      19th Apr 2010
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]" <(E-Mail Removed)> wrote in message
news:392C1D5D-D4A7-4884-A048-(E-Mail Removed)...
> "Gregory A. Beamer" <(E-Mail Removed)> wrote in message
> news:E38E199E-FA4A-464C-A3B1-(E-Mail Removed)...
>
>> 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


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      19th Apr 2010
On Apr 19, 6:22*am, "Miro" <m...@beero.com> 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 lookat
> a more complicated example online.
>
> Thanks,
>
> Miro
>
> "Mark Rae [MVP]" <m...@markrae.net> wrote in messagenews:392C1D5D-D4A7-4884-A048-(E-Mail Removed)...
>
>
>
> > "Gregory A. Beamer" <NoSpamMgbwo...@comcast.netNoSpamM> wrote in message
> >news:E38E199E-FA4A-464C-A3B1-(E-Mail Removed)...

>
> >> 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
 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      19th Apr 2010
"Alexey Smirnov" <(E-Mail Removed)> wrote in message
news:10ae6a46-fd73-4935-83ce-(E-Mail Removed)...

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

 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      19th Apr 2010
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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
aspx file call codebehind function Raymond Chiu Microsoft C# .NET 7 9th Jan 2009 02:50 PM
How to call function from one aspx page from another Jonathan Microsoft ASP .NET 8 11th Mar 2008 05:47 AM
How to call a Sub function from .ASPX file ? bienwell Microsoft ASP .NET 10 19th Aug 2005 01:42 PM
Windows function call from aspx code behind? =?Utf-8?B?SGFydG11dCBTY2hyb3Ro?= Microsoft ASP .NET 1 21st Jan 2004 07:51 PM
how to call a function in another aspx page? Matthew Louden Microsoft ASP .NET 2 20th Nov 2003 08:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:02 PM.