【问题标题】:asp.net unable to reference variable from code behindasp.net 无法从后面的代码中引用变量
【发布时间】:2018-07-19 06:30:31
【问题描述】:

我已经在其他代码上完成了此操作,并且工作正常,但由于某种原因,在 VStudio 2017 中的一个新项目中,我无法从后面的 c# 代码中引用 asp/html 中的变量。然后,我使用其中的 about.aspx 开始了一个新的测试空白 asp web 项目,同样的事情参见下面的代码,我不知道为什么它不工作。我得到的“teststring”在当前上下文消息中不存在,但它确实存在。我在这里完全错过了什么吗?

ASP/HTML
<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="TestApp.About" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <h3>Your application description page.</h3>
    <p>Use this area to provide additional information.</p>
    <%=teststring %>
</asp:Content>

C# 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestApp

{
    public partial class About : Page

    {
        public string teststring;

        protected void Page_Load(object sender, EventArgs e)

        {
            teststring = "test";
        }
    }
}

【问题讨论】:

  • 它会给您带来什么确切错误?是在抱怨teststring吗?或者Title?您是否构建了解决方案?
  • 如果你断点 Page_Load 它真的命中了吗?您是否看到“使用此区域...”消息呈现在屏幕上?

标签: c# asp.net


【解决方案1】:

在 ASPX 顶部的 &lt;@ Page ...&gt; 标记处,将属性 Inherits="TestApp.About" 的值更改为 Inherits="TestApp_About"。下划线而不是点/句点。我能够复制这个问题。

【讨论】:

  • 所以我将其更改为 _ 并重建和相同的内容,然后我将其更改回 .并重建,现在它可以工作了。奇怪
猜你喜欢
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 2012-10-13
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多