简体   繁体   English

如何在asp.net中使用vb.net的类代码

[英]How to use class code of vb.net in asp.net

I'm having trouble with using my vb.net code in asp.net webpage. 我在使用asp.net网页中的vb.net代码时遇到问题。

<%@ Page  Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="WebForm1.aspx.vb" Inherits="Banking_Application.WebForm1" CodeFile="~/WebForm1.aspx.vb" %>

class name is WebForm1. 类名是WebForm1。 I've written code in a separate file and I want to use it in page. 我已经在单独的文件中编写了代码,并且希望在页面中使用它。

Like 喜欢

<% Dim total As Integer
    Dim val As tests.WebForm1
    val = New tests.WebForm1
    total = val.TotalBranches()
    total.ToString()
%>

I'm new to vb.net and asp.net. 我是vb.net和asp.net的新手。

All suggestions are welcome. 欢迎所有建议。

Thanks! 谢谢!

I think you are missing what is happening with the mechanics of asp.net. 我认为您错过了asp.net的机制正在发生的事情。

You either: 您要么:

  • Write code directly in the aspx page (within the html) 直接在aspx页面(在html内)中编写代码
  • Or, you wrtie code in the code behind page... your WebForm1.aspx.vb page. 或者,您在页面后面的代码中编写代码... WebForm1.aspx.vb页面。 From the code behind page, you can access and manipulate the server controls that is on the aspx (html) side... 从页面后面的代码中,您可以访问和操纵位于aspx(html)一侧的服务器控件。

Well first of all i need to clarify a few things for you: 首先,我需要为您澄清一些事情:

in Asp.net, you can embed code in your aspx page using <% %> code block, or write it in a separate file and use it from there. 在Asp.net中,您可以使用<% %>代码块将代码嵌入到aspx页面中,或将其写入单独的文件中并从那里使用。

in your page declaration, you specify the code behind of your page using the CodeBehind= attribute, so if you want to place your code in WebForm1.vb , your page declaration should include CodeBehind="WebForm1.vb" . 在页面声明中,使用CodeBehind=属性指定页面后面的代码,因此,如果要将代码放置在WebForm1.vb ,则页面声明应包含CodeBehind="WebForm1.vb"

  • note: CodeFile="~/WebForm1.aspx.vb" is not needed. 注意:不需要CodeFile="~/WebForm1.aspx.vb"

the structure of the code behind for you aspx page, should look like that: aspx页面背后的代码结构应如下所示:

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Public Function test1()
        Return "test"
    End Function
End Class

and you can add more functions as needed. 您可以根据需要添加更多功能。 in the example above, i have added a function called test1 for the purpose of this example. 在上面的示例中,我为此示例添加了一个名为test1的函数。

now, after you have created your code behind in the correct structure, you can call its methods from the aspx page as if it was the same page: 现在,以正确的结构创建完代码后,您可以从aspx页面调用其方法,就好像它是同一页面一样:

<% =test1()
%>

this will return "test" as specified by the function in code behind. 这将返回由函数在后面的代码中指定的"test"
you do not need to instantiate the class. 您无需实例化该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM