如何访问标记中ASP.NET代码隐藏中的对象?
我有以下事件处理代码:如何访问标记中ASP.NET代码隐藏中的对象?,asp.net,vb.net,Asp.net,Vb.net,我有以下事件处理代码: Public Class Details Inherits System.Web.UI.Page Public veh As Vehicle Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim id As Integer = Request("id") veh =
Public Class Details
Inherits System.Web.UI.Page
Public veh As Vehicle
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim id As Integer = Request("id")
veh = New DataRepository().vehicles.get(id)
End Sub
End Class
并尝试在我的标记中访问它,如下所示:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="Added.aspx.vb" Inherits="Auto_Dealer_Website.Added" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% veh.color()%>
</asp:Content>
但是编译器不会识别它。我错过了什么?如何引用我的veh
对象
已解决
在未首先生成项目之前,无法引用CodeBehind。添加等号以输出变量:
<%= veh.make %>
仍然不起作用。我也尝试过
,但运气不佳。“veh”未声明。由于它的保护级别,它可能无法访问。好的,看起来我只是需要重新编译!谢谢这样做不是好的做法。从你的角度来看,你不应该混合和匹配你的代码。这到底是个什么坏主意?你把代码混合在一起,这会给代码调试和维护带来更多麻烦。您应该将控件添加到UI中,然后从codebehind进行设置。同样出于性能方面的原因…它将使用反射,例如,如果你做同样的事情并使用中继器,它将需要使用反射来获得它的值。做一个谷歌搜索,你就会看到。