如何跨多个页面共享VB代码? [英] How to share VB code across multiple pages?

查看:64
本文介绍了如何跨多个页面共享VB代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.NET框架的新手。


我的一堆网页需要相同的功能。现在,我在每个页面中都将

作为Subs。


我认为必须有一种方法可以将我的Subs保存在一个单独的文件中,然后

每个网页都链接到它。你们能告诉我这是怎么回事吗?非常感谢!

解决方案

>我的一堆网页需要相同的功能。现在,我在每个页面中放置了

Subs。

我认为必须有一种方法可以将我的Subs保存在一个单独的文件中然后
每个网页链接到它。你们能告诉我怎么做这个吗?非常感谢!




有多种方法。一种方法是只创建一个新的类文件(.vb

或.cs文件),然后以这种方式引用该函数。


我通常会制作一个名为sharedFunctions的新类文件,然后在其中放入

特定函数。然后我可以从

项目中的任何文件中引用它们:sharedFunctions.myFunction()


-Darrel

>我认为必须有一种方法可以将我的Subs保存在一个单独的文件中,然后

将每个网页链接到它。你们能告诉我怎么做这个吗?非常感谢!


ASP.Net是完全编译和面向对象的。所以,作为一个新的ASP.Net程序员,你需要做的第一件事就是重新培训你的想法。

ASP.Net应用程序中没有页面。只有课程。一个

System.Web.UI.Page是一个类,你创建的模板只是

类定义的一部分。


所以,你没有在文件中定义Subs。它们是课堂上的方法。


现在,我说这是因为你需要创建一个

包含你的公共子类的类,并在所有Page类中使用它。它没有
直接访问传递给页面的HttpContext,但

记得它将被一个Page使用,它有一个HttpContext 。

你可以在课堂上引用它:


''使用请求作为HttpContext的一部分

公开Sub DoSomething()

Dim Request As HttpRequest = CType(HttpContext.Current.Handler,

System.Web.UI.Page).Request

如果Request.QueryString(" SomeParameter")=" foo"然后

''做点东西

结束如果

结束子


它真的会帮到你反思.Net的面向对象性质,在

中为了最大限度地利用它。


HTH,


Kevin Spencer

Microsoft MVP

..Net开发人员

大事由
$ b组成$ b很多小东西。


< an *********** @ yahoo.com>在消息中写道

新闻:11 ********************** @ g43g2000cwa.googlegr oups.com ...我是新来的.NET框架。

我的一堆网页需要相同的功能。现在,我把Subs放在每个页面上。

我认为必须有一种方法可以将我的Subs保存在一个单独的文件中然后
让每个网页链接它。你们能告诉我怎么做这个吗?非常感谢!



您好,Thx。


收到您的回复后,我创建了一个文件称为MyClass.vb在

网页的同一个文件夹。


MyClass.vb有这样的类:


公开Class MyClass

Public Sub MySub()

做点什么

结束子

结束班


我在我的网页上这样调用MySub:


调用MyClass.MySub()


这是编译错误:


BC30456:''MySub''不是''ASP.Page1_aspx''的成员。


显然我做了不要以正确的方式做到这一点。我想我正在做这件事,就像Java一样。我是一名Java程序员。


难道我不能告诉Page1.aspx在哪里找到这个功能吗?


darrel写道:

我的一堆网页需要相同的功能。现在,我把Subs放在每个页面上。

我认为必须有一种方法可以将我的Subs保存在一个单独的文件中然后
让每个网页链接它。你们能告诉我怎么做这个吗?非常感谢!



有多种方法。一种方法是只创建一个新的类文件(.vb
或.cs文件)然后以这种方式引用该函数。

我通常创建一个名为sharedFunctions的新类文件然后把
特定的功能放在其中。然后我可以从
项目中的任何文件中引用它们:sharedFunctions.myFunction()

-Darrel



I am new to .NET framework.

A bunch of web pages of mine need the same function. Right now, I put
the Subs in each individual page.

I think there must be a way to save my Subs in a separate file and then
have each web page link to it. Could you guys please let me know how
to do this? Thanks a lot!

解决方案

> A bunch of web pages of mine need the same function. Right now, I put

the Subs in each individual page.

I think there must be a way to save my Subs in a separate file and then
have each web page link to it. Could you guys please let me know how
to do this? Thanks a lot!



There are a variety of ways. One way is to just make a new class file (a .vb
or .cs file) and then reference the function that way.

I typically make a new class file called sharedFunctions and then put
specific functions within it. I can then reference them from any file in the
project: sharedFunctions.myFunction()

-Darrel


> I think there must be a way to save my Subs in a separate file and then

have each web page link to it. Could you guys please let me know how
to do this? Thanks a lot!
ASP.Net is fully-compiled and object-oriented. So, one of the first things
you need to do as a new ASP.Net programmer is to retrain your thinking.
There are no pages in an ASP.Net application. There are only classes. A
System.Web.UI.Page is a class, and that template you create is just part of
the class definition.

So, you don''t ever define Subs in files. They are methods in classes.

Now, the reason I''m sayiing this is that you need to create a class that
contains your common Subs, and use that in all of your Page classes. It
doesn''t have direct access to the HttpContext that is passed to a page, but
remember that it will be used BY a Page, which does have the HttpContext.
And you can reference it in your class thusly:

'' Using Request as a part of the HttpContext
Public Sub DoSomething()
Dim Request As HttpRequest = CType(HttpContext.Current.Handler,
System.Web.UI.Page).Request
If Request.QueryString("SomeParameter") = "foo" Then
''Do Something
End If
End Sub

It will really help you to reflect on the Object-oriented nature of .Net, in
order to leverage it to its fullest extent.

HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.

<an***********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...I am new to .NET framework.

A bunch of web pages of mine need the same function. Right now, I put
the Subs in each individual page.

I think there must be a way to save my Subs in a separate file and then
have each web page link to it. Could you guys please let me know how
to do this? Thanks a lot!



Hi, Thx.

After I got your reply, then I created a file called "MyClass.vb" in
the same folder of the webpages.

MyClass.vb has a Class like this:

Public Class MyClass
Public Sub MySub()
Do Something
End Sub
End Class

I called the MySub like this in my webpage:

Call MyClass.MySub()

This is the compile error:

BC30456: ''MySub'' is not a member of ''ASP.Page1_aspx''.

Apparently I did not do it in the right way. I think I am doing it
like Java. I was a Java programmer.

Shouldn''t I tell Page1.aspx where to find that function?

darrel wrote:

A bunch of web pages of mine need the same function. Right now, I put
the Subs in each individual page.

I think there must be a way to save my Subs in a separate file and then
have each web page link to it. Could you guys please let me know how
to do this? Thanks a lot!



There are a variety of ways. One way is to just make a new class file (a .vb
or .cs file) and then reference the function that way.

I typically make a new class file called sharedFunctions and then put
specific functions within it. I can then reference them from any file in the
project: sharedFunctions.myFunction()

-Darrel




这篇关于如何跨多个页面共享VB代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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