test.aspx 源代码如下
<%@ page language="VB" debug="true" src="vbCb.vb" inherits="myVbCodeBehind.vbCb" %> <html> <head></head> <body> <form runat="server"> Select a flower, and click the submit button please: <br> <asp:ListBox id="lstFlowers" runat="server" rows="3"> </asp:ListBox><br><br> <asp:Button id="btnSubmit" runat="server" text="Submit" onclick="showSelection" /><br><br> <asp:Label id=lblMessage runat="server" /> </form> </body> </html>
后端 vbCb.vb 代码如下
Option Strict Off Imports System Imports System.Web.UI.WebControls Namespace myVbCodeBehind Public Class vbCb : Inherits System.Web.UI.Page Public lstFlowers As System.Web.UI.WebControls.ListBox Public lblMessage As System.Web.UI.WebControls.Label Public btnSubmit As System.Web.UI.WebControls.Button Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not IsPostBack Then lblMessage.Text="No Selection Yet" lstFlowers.Items.Add(new ListItem("Tulip")) lstFlowers.Items.Add(new ListItem("Rose")) lstFlowers.Items.Add(new ListItem("Redbud")) lstFlowers.SelectedIndex=0 End If End Sub Protected Sub showSelection(ByVal obj As Object, ByVal e As EventArgs) lblMessage.Text="You have selected " + _ lstFlowers.SelectedItem.Text End Sub End Class End Namespace
或者后端 cSharpCb.cs 代码如下
namespace myCsCodeBehind { using System; using System.Web.UI.WebControls; public class cSharpCb : System.Web.UI.Page { public System.Web.UI.WebControls.ListBox lstFlowers; public System.Web.UI.WebControls.Label lblMessage; public System.Web.UI.WebControls.Button btnSubmit; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { lblMessage.Text="No Selection Yet"; lstFlowers.Items.Add(new ListItem("Tulip")); lstFlowers.Items.Add(new ListItem("Redbud")); lstFlowers.Items.Add(new ListItem("Poppy")); } } protected void showSelection(object obj, EventArgs e) { lblMessage.Text="You have selected " + lstFlowers.SelectedItem.Text; } } }
如果是cs代码,则test.asp首行需要设置为
<%@ page language="VB" debug="true" src="vbCb.vb" inherits="myVbCodeBehind.vbCb" %>
?
也可以编译为dll(放在网站根目录bin文件夹下),只需要引用dll即可 vbc /out:vbCb.dll /t:library .\vbCb.vb c#代码如下 csc /t:library /r:System.dll /r:System.Web.dll cSharpCb.cs test.aspx首行如下 <%@ page language="VB" debug="true" codebehind=="vbCb.vb" inherits="myVbCodeBehind.vbCb" %> <%@ page language="cs" debug="true" codebehind="cSharpCb.cs" inherits="myCsCodeBehind.cSharpCb" %>