如何从.ASPX文件中调用Sub函数? [英] How to call a Sub function from .ASPX file ?

查看:57
本文介绍了如何从.ASPX文件中调用Sub函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对ASP.NET中包含的文件有疑问。我有一个文件

包括所有Sub函数(例如FileFunct.vb)。这个文件中的一个函数是:b
$ b Sub TestFunct(ByVal strInput As String)

return(strInput& 测试)

结束子

我想从另一个.ASPX文件中调用FileFunct.vb中的这个函数

like这个:


<%@ import Namespace =" System" %>

<%@ import Namespace =" System.Data" %>


< html>

< head>

< title>测试页< / title>

< / head>


< body>

< script runat =" server"语言= QUOT; VB" SCR =" FileFunct.vb" >


Sub Page_Load(s As Object,e As EventArgs)

Dim result = TestFunct(" This is a string")

response.write("< BR> result ==>"& result)

End Sub

< / script>

< / body>

< / html>


我收到此错误:

''/''应用程序中的服务器错误。

-------------------------- --------------------------------------------------

----

编译错误

描述:编译所需资源时出错

来处理此请求。请查看以下具体错误详细信息

并适当修改源代码。


编译器错误消息:BC30451:未声明名称''TestFunct''。


来源错误:

第18行:Sub Page_Load(作为对象,e作为EventArgs)

第19行:

第20行:Dim result = TestFunct(" This is a string")

第21行:response.write("< BR> Result ==>" &结果)

第22行:

我有一个.ASPX文件,我不使用Visual Studio。在这种情况下NET。

我可以这样做吗?

提前致谢。

解决方案

让TestFunc共享并通过
获取它

ClassName.TextFunc


所以,它看起来像


公共类实用工具

公共共享TestFunct(..)

...

结束功能

结束班级


您可以像以下一样使用它:

Utility.TestFunct(...)
< br $>
卡尔


-

我的ASP.Net教程
http://www.openmymind.net/ - 新增和改进(是的,弹出窗口是

烦人)
http://www.openmymind.net/faq.aspx - 非官方新闻组常见问题解答(更多内容

来了!)

" bienwell" <双****** @ hotmail.com>在消息中写道

新闻:O7 ************** @ TK2MSFTNGP10.phx.gbl ...


我对ASP.NET中包含的文件有疑问。我有一个
包含所有Sub函数的文件(例如FileFunct.vb)。此文件中的其中一个功能是:

Sub TestFunct(ByVal strInput As String)
return(strInput&" test")
End Sub

我想从另一个.ASPX文件中调用FileFunct.vb中的这个函数
这样:

<%@ import Namespace =" System" ; %>
<%@ import Namespace =" System.Data" %>

< html>
< head>
< title>测试页< / title>
< / head>

< body>
< script runat =" server"语言= QUOT; VB" SCR =" FileFunct.vb" >

Sub Page_Load(s As Object,e As EventArgs)
Dim result = TestFunct(" This is a string")
response.write("< BR>结果==>"&结果)
结束子
< / script>
< / body>
< / html>

我有这个错误:'/''应用程序中的服务器错误。
---------------- -------------------------------------------------- ----------
----

编译错误
描述:编译资源时发生错误
必需
为此请求提供服务。请查看以下特定错误
详细信息
并适当修改源代码。

编译器错误消息:BC30451:未声明名称''TestFunct''。

源错误:

第18行:Sub Page_Load(s作为对象,e作为EventArgs)
第19行:
第20行:Dim result = TestFunct("这是一个字符串)
第21行:response.write("< BR> Result ==>"& Result)
第22行:

我有一个.ASPX文件,我不使用Visual Studio。 NET在这个
的情况下。
我可以这样做吗?

提前致谢。



你不是第四维想的! ( - 回到未来)


实际上,你并没有考虑面向对象。


ASP.Net是面向对象的,你最好熟悉它,或者

你几乎每天都会来这里,写下糟糕的代码直到你这么做。

让我详细说明,如果你愿意:


文件是源代码。您的应用程序没有文件。它有类。

文件定义一个类,但不是一个类。类是数据类型,并且

存在于正在运行的应用程序的上下文中。所以,当你在谈论你的应用程序是如何工作的时候,你需要做的第一件事就是思考关于类而不是文件的
。一个文件可以包含一个或多个类定义,

你需要熟悉使用.Net成功的类。


类是非常重要的.Net编程;对象由

类构成,类提供封装。面向对象的编程可以让b b $ b得到相当复杂的封装,并且封装可以为你节省很多的悲伤,

隐藏那些需要隐藏那些不需要的东西的东西需要

他们。


如果你的文件中包含一堆子和函数,你需要

在其中创建一个包含Subs和Functions的类。这些子和函数可以共享(意味着它们是单例对象,不需要运行

类实例),或者它们可以是实例(意思是必须创建包含它们的类的
实例才能使用它们。

共享数据和流程的优势在于它不需要class

instance,实质上是global,可用于整个应用程序。

这也是共享数据和流程的缺点。任何东西都可以获得它,并且更改它,并且在多线程应用程序中(与VB 6不同,.net是多线程的
),这可能会导致所有各种各样的问题。除非你熟悉这些问题,否则我会坚持使用需要

实例化的课程。实例化是创建

a类的副本(实例)的过程,其范围有限(可用性),并且是线程安全的。


一次你创建一个类的实例,你可以访问任何公共或朋友

(朋友比公众更受保护,但你不应该遇到问题

马上)来自引用该实例的任何其他类的成员。


从您的问题和您发布的代码中,我可以看到您需要一个

好​​位更多的教育和实践。我推荐使用.Net SDK,从以下网站下载免费的



http://www.microsoft.com/downloads/d...displaylang=en


了解ASP和ASP.Net之间的差异,以及VBScript或VB 6与VB.Net之间的
之间的区别非常重要。第一个是程序性的,单个线程是
,并且易于用于小型应用程序。 .Net是

面向对象,多线程,一旦你花了很多钱就很容易使用

的时间研究它,但是如果你不喜欢那么难以使用't。


-

HTH,


Kevin Spencer

微软MVP

..Net开发人员

每个人都会嗤之以鼻,

但是有些人更善于隐藏它。


" bienwell" <双****** @ hotmail.com>在消息中写道

新闻:O7 ************** @ TK2MSFTNGP10.phx.gbl ...


我对ASP.NET中包含的文件有疑问。我有一个
包含所有Sub函数的文件(例如FileFunct.vb)。此文件中的其中一个功能是:

Sub TestFunct(ByVal strInput As String)
return(strInput&" test")
End Sub

我想从另一个.ASPX文件中调用FileFunct.vb中的这个函数
这样:

<%@ import Namespace =" System" ; %>
<%@ import Namespace =" System.Data" %>

< html>
< head>
< title>测试页< / title>
< / head>

< body>
< script runat =" server"语言= QUOT; VB" SCR =" FileFunct.vb" >

Sub Page_Load(s As Object,e As EventArgs)
Dim result = TestFunct(" This is a string")
response.write("< BR>结果==>"&结果)
结束子
< / script>
< / body>
< / html>

我有这个错误:'/''应用程序中的服务器错误。
---------------- -------------------------------------------------- ----------
----

编译错误
描述:编译资源时发生错误
必需
为此请求提供服务。请查看以下特定错误
详细信息
并适当修改源代码。

编译器错误消息:BC30451:未声明名称''TestFunct''。

源错误:

第18行:Sub Page_Load(s作为对象,e作为EventArgs)
第19行:
第20行:Dim result = TestFunct("这是一个字符串)
第21行:response.write("< BR> Result ==>"& Result)
第22行:

我有一个.ASPX文件,我不使用Visual Studio。 NET在这个
的情况下。
我可以这样做吗?

提前致谢。



Kevin,


上次我将所有文件放在我的应用程序(项目名称)中使用Visual

Studio.NET,声明了一个类(AClass) )它具有一些功能,并将它们保存在一个文件(.vb文件)中。当我从其他.vb文件(AClass.Function1,...)调用函数

时,我的程序运行正常。我的申请是

面向对象的良好开发。现在,我想尝试另一种方式:

创建.ASPX文件,其中包含VB.NET脚本,HTML并调用一个函数

来自另一个.vb文件使用Visual Studio.NET。我可以这样做吗?

?这是我的问题。



" Kevin Spencer" <柯*** @ DIESPAMMERSDIEtakempis.com>在留言中写道

news:u7 ************** @ TK2MSFTNGP15.phx.gbl ...

你不是第四维思考! ( - 回到未来)

实际上,你并没有考虑面向对象。

ASP.Net是面向对象的,你最好得到熟悉它,或者
你几乎每天都会来这里,编写糟糕的代码,直到你
为止。让我详细说明一下,如果你愿意:

文件是源代码。您的应用程序没有文件。它有
类。文件定义了一个类,但不是一个类。类是数据类型,并且存在于正在运行的应用程序的上下文中。所以,当你在谈论你的应用程序如何工作时,你需要做的第一件事就是思考关于类而不是文件。一个文件可以包含一个或多个类
定义,你需要熟悉使用.Net成功的类。

类在.Net编程中非常重要;对象由
类构成,类提供封装。面向对象的编程
可以变得相当复杂,封装可以通过隐藏那些需要隐藏那些不需要它们的东西来为你节省很多麻烦。

如果你的文件中包含一堆子和函数,你需要创建一个包含Subs和Functions的类。这些子和函数可以共享(意味着它们是单例对象,不需要一个类实例来操作),或者它们可以是实例(意味着一个
实例)必须创建包含它们的类才能使用
它们)。共享数据和过程的优点是它不需要
类实例,并且实质上是全局的,可用于整个
申请。这也是共享数据和流程的缺点。任何事情都可以得到它,并改变它,并在一个多线程的应用程序(不像VB 6,.net是多线程的),这可能会导致各种各样的问题。除非你熟悉这些问题,否则我会坚持使用需要实例化的类。实例化是创建一个类的副本(实例)
的过程,该类的范围(可用性)有限,并且是线程安全的。

一旦创建了类的实例,你可以访问任何公共或
朋友(朋友比公众受到更多保护,但你不应该立即遇到问题)来自引用该实例的任何其他类的成员。

从您的问题和您发布的代码中,我可以看到您需要更多的教育和实践。我推荐.Net SDK,
免费下载:

http://www.microsoft.com/downloads/d...displaylang=en
了解ASP与ASP.Net,VBScript或VB 6与VB.Net之间的区别非常重要。第一种是程序性的,单线程的,易于用于小型应用程序。 .Net是面向对象的,多线程的,并且一旦花费大量的b $ b时间研究它就很容易使用,但如果你不这样做,那就非常难以使用。

-
HTH,

Kevin Spencer
微软MVP
.Net开发人员
每个人都嗤之以鼻,
人们更善于隐藏它。

bienwell <双****** @ hotmail.com>在消息中写道
新闻:O7 ************** @ TK2MSFTNGP10.phx.gbl ...


我对ASP.NET中包含的文件有疑问。我有一个
包含所有Sub函数的文件(例如FileFunct.vb)。此文件中的
函数之一是:

Sub TestFunct(ByVal strInput As String)
return(strInput&" test")
End Sub

我想从另一个.ASPX文件中调用FileFunct.vb中的这个函数
这样:

<%@ import Namespace =" System" ; %>
<%@ import Namespace =" System.Data" %>

< html>
< head>
< title>测试页< / title>
< / head>

< body>
< script runat =" server"语言= QUOT; VB" SCR =" FileFunct.vb" >

Sub Page_Load(s As Object,e As EventArgs)
Dim result = TestFunct(" This is a string")
response.write("< BR>结果==>"&结果)
结束子
< / script>
< / body>
< / html>

我有这个错误:'/''应用程序中的服务器错误。



-------------------------------------------------- ----------------



-

----

编译错误
描述:编译资源期间发生错误
需要
来处理此请求。请查看以下特定错误
详细信息
并适当修改源代码。

编译器错误消息:BC30451:未声明名称''TestFunct''。

源错误:

第18行:Sub Page_Load(s作为对象,e作为EventArgs)
第19行:
第20行:Dim result = TestFunct("这是一个字符串)
第21行:response.write("< BR> Result ==>"& Result)
第22行:

我有一个.ASPX文件,我不使用Visual Studio。 NET在这个
的情况下。
我可以这样做吗?

提前致谢。




Hi,

I have a question about file included in ASP.NET. I have a file that
includes all the Sub functions (e.g FileFunct.vb). One of the functions
in this file is :

Sub TestFunct(ByVal strInput As String)
return (strInput & " test")
End Sub
I''d like to call this function in FileFunct.vb from another .ASPX file
like this :

<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>

<html>
<head>
<title>Test page</title>
</head>

<body>
<script runat="server" language="VB" scr="FileFunct.vb" >

Sub Page_Load(s As Object, e As EventArgs)
Dim result=TestFunct("This is a string")
response.write("<BR>result ==> " & result)
End Sub
</script>
</body>
</html>

I''ve got this error:

Server Error in ''/'' Application.
----------------------------------------------------------------------------
----

Compilation Error
Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: BC30451: Name ''TestFunct'' is not declared.

Source Error:
Line 18: Sub Page_Load(s As Object, e As EventArgs)
Line 19:
Line 20: Dim result=TestFunct("This is a string")
Line 21: response.write("<BR>Result ==> " & Result)
Line 22:
I have a single .ASPX file and I don''t use Visual studio. NET in this case.
Can I do that ?

Thanks in advance.

解决方案

Make TestFunc shared and access it via

ClassName.TextFunc

so, it would look like

public class Utility
public shared TestFunct(..)
...
end function
end class

and you would use it like:

Utility.TestFunct(...)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"bienwell" <bi******@hotmail.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...

Hi,

I have a question about file included in ASP.NET. I have a file that
includes all the Sub functions (e.g FileFunct.vb). One of the functions
in this file is :

Sub TestFunct(ByVal strInput As String)
return (strInput & " test")
End Sub
I''d like to call this function in FileFunct.vb from another .ASPX file
like this :

<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>

<html>
<head>
<title>Test page</title>
</head>

<body>
<script runat="server" language="VB" scr="FileFunct.vb" >

Sub Page_Load(s As Object, e As EventArgs)
Dim result=TestFunct("This is a string")
response.write("<BR>result ==> " & result)
End Sub
</script>
</body>
</html>

I''ve got this error:

Server Error in ''/'' Application.
----------------------------------------------------------------------------
----

Compilation Error
Description: An error occurred during the compilation of a resource
required
to service this request. Please review the following specific error
details
and modify your source code appropriately.

Compiler Error Message: BC30451: Name ''TestFunct'' is not declared.

Source Error:
Line 18: Sub Page_Load(s As Object, e As EventArgs)
Line 19:
Line 20: Dim result=TestFunct("This is a string")
Line 21: response.write("<BR>Result ==> " & Result)
Line 22:
I have a single .ASPX file and I don''t use Visual studio. NET in this
case.
Can I do that ?

Thanks in advance.



You''re not thinking fourth-dimensionally! (- Back to the Future)

Actually, you''re not thinking Object-Orientationally.

ASP.Net is object-oriented, and you''d better get acquainted with it, or
you''ll be visiting here almost daily, and writing crappy code until you do.
Let me elaborate, if you will:

Files are source code. Your application doesn''t have files. It has classes.
A file defines a class, but IS not a class. A class is a data type, and
exists in the context of a running application. So, when you''re talking
about how your application works, the first thing you need to do is think
about classes, not files. A file can contain one or MORE class definitions,
and you need to get acquainted with classes to be successful with .Net.

Classes are very important in .Net programming; Objects are made from
classes, and classes provide encapsulation. Object-oriented programming can
get pretty darned complex, and encapsulation can save you a lot of grief, by
hiding those things which need hiding from those things that don''t need
them.

If you have a file with a bunch of Subs and Functions in it, you need to
create a class with Subs and Functions in it. These Subs and Functions can
be Shared (meaning that they are singleton objects that don''t require a
class instance to operate), or they can be Instance (meaning that an
instance of the class containing them must be created in order to use them).
The advantage to Shared data and process is that it doesn''t require a class
instance, and is, in essence "global," available to the entire application.
This is also the disadvantage of Shared data and process. Anything can get
to it, and change it, and in a multi-threaded app (unlike VB 6, .net is
multi-threaded), this can cause all sorts of problems. Unless you''re
familiar with the issues, I would stick with classes that require
instantiation. Instantiation is the process of creating a copy (instance) of
a class that is limited in its scope (availability), and is thread-safe.

Once you create an instance of a class, you can access any Public or Friend
(Friend is more protected than Public, but you shouldn''t run into issues
right away) members from any other class that references the instance.

From your question, and the code you posted, I can see that you require a
good bit more education and practice. I would recommend the .Net SDK, a free
download from:

http://www.microsoft.com/downloads/d...displaylang=en

It is extremely important to know the difference between ASP and ASP.Net,
between VBScript or VB 6, and VB.Net. The first are procedural,
single-threaded, and easy to use for small applications. .Net is
object-oriented, multi-threaded, and easy to use once you spend a great deal
of time studying it, but incredibly hard to use if you don''t.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"bienwell" <bi******@hotmail.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...

Hi,

I have a question about file included in ASP.NET. I have a file that
includes all the Sub functions (e.g FileFunct.vb). One of the functions
in this file is :

Sub TestFunct(ByVal strInput As String)
return (strInput & " test")
End Sub
I''d like to call this function in FileFunct.vb from another .ASPX file
like this :

<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>

<html>
<head>
<title>Test page</title>
</head>

<body>
<script runat="server" language="VB" scr="FileFunct.vb" >

Sub Page_Load(s As Object, e As EventArgs)
Dim result=TestFunct("This is a string")
response.write("<BR>result ==> " & result)
End Sub
</script>
</body>
</html>

I''ve got this error:

Server Error in ''/'' Application.
----------------------------------------------------------------------------
----

Compilation Error
Description: An error occurred during the compilation of a resource
required
to service this request. Please review the following specific error
details
and modify your source code appropriately.

Compiler Error Message: BC30451: Name ''TestFunct'' is not declared.

Source Error:
Line 18: Sub Page_Load(s As Object, e As EventArgs)
Line 19:
Line 20: Dim result=TestFunct("This is a string")
Line 21: response.write("<BR>Result ==> " & Result)
Line 22:
I have a single .ASPX file and I don''t use Visual studio. NET in this
case.
Can I do that ?

Thanks in advance.



Kevin,

Last time I put all the files in my application (project name) using Visual
Studio.NET, declared a class (AClass) which has some functions, and saved
them in a file (.vb file). My program worked fine when I called the function
from my other .vb files (AClass.Function1, ...). My application was
Object-Oriented well development. Now, I''d like to try another way :
creating .ASPX file that contains VB.NET script, HTML and call one function
from another .vb file without using Visual Studio.NET. Can I do that and How
? It''s my question.


"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u7**************@TK2MSFTNGP15.phx.gbl...

You''re not thinking fourth-dimensionally! (- Back to the Future)

Actually, you''re not thinking Object-Orientationally.

ASP.Net is object-oriented, and you''d better get acquainted with it, or
you''ll be visiting here almost daily, and writing crappy code until you do. Let me elaborate, if you will:

Files are source code. Your application doesn''t have files. It has classes. A file defines a class, but IS not a class. A class is a data type, and
exists in the context of a running application. So, when you''re talking
about how your application works, the first thing you need to do is think
about classes, not files. A file can contain one or MORE class definitions, and you need to get acquainted with classes to be successful with .Net.

Classes are very important in .Net programming; Objects are made from
classes, and classes provide encapsulation. Object-oriented programming can get pretty darned complex, and encapsulation can save you a lot of grief, by hiding those things which need hiding from those things that don''t need
them.

If you have a file with a bunch of Subs and Functions in it, you need to
create a class with Subs and Functions in it. These Subs and Functions can
be Shared (meaning that they are singleton objects that don''t require a
class instance to operate), or they can be Instance (meaning that an
instance of the class containing them must be created in order to use them). The advantage to Shared data and process is that it doesn''t require a class instance, and is, in essence "global," available to the entire application. This is also the disadvantage of Shared data and process. Anything can get
to it, and change it, and in a multi-threaded app (unlike VB 6, .net is
multi-threaded), this can cause all sorts of problems. Unless you''re
familiar with the issues, I would stick with classes that require
instantiation. Instantiation is the process of creating a copy (instance) of a class that is limited in its scope (availability), and is thread-safe.

Once you create an instance of a class, you can access any Public or Friend (Friend is more protected than Public, but you shouldn''t run into issues
right away) members from any other class that references the instance.

From your question, and the code you posted, I can see that you require a
good bit more education and practice. I would recommend the .Net SDK, a free download from:

http://www.microsoft.com/downloads/d...displaylang=en
It is extremely important to know the difference between ASP and ASP.Net,
between VBScript or VB 6, and VB.Net. The first are procedural,
single-threaded, and easy to use for small applications. .Net is
object-oriented, multi-threaded, and easy to use once you spend a great deal of time studying it, but incredibly hard to use if you don''t.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Everybody picks their nose,
But some people are better at hiding it.

"bienwell" <bi******@hotmail.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl...

Hi,

I have a question about file included in ASP.NET. I have a file that
includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is :

Sub TestFunct(ByVal strInput As String)
return (strInput & " test")
End Sub
I''d like to call this function in FileFunct.vb from another .ASPX file
like this :

<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>

<html>
<head>
<title>Test page</title>
</head>

<body>
<script runat="server" language="VB" scr="FileFunct.vb" >

Sub Page_Load(s As Object, e As EventArgs)
Dim result=TestFunct("This is a string")
response.write("<BR>result ==> " & result)
End Sub
</script>
</body>
</html>

I''ve got this error:

Server Error in ''/'' Application.



--------------------------------------------------------------------------


--

----

Compilation Error
Description: An error occurred during the compilation of a resource
required
to service this request. Please review the following specific error
details
and modify your source code appropriately.

Compiler Error Message: BC30451: Name ''TestFunct'' is not declared.

Source Error:
Line 18: Sub Page_Load(s As Object, e As EventArgs)
Line 19:
Line 20: Dim result=TestFunct("This is a string")
Line 21: response.write("<BR>Result ==> " & Result)
Line 22:
I have a single .ASPX file and I don''t use Visual studio. NET in this
case.
Can I do that ?

Thanks in advance.




这篇关于如何从.ASPX文件中调用Sub函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆