asp.net - 后面的代码中无法识别 Web 控件

标签 asp.net visual-studio-2012

我正在通过 Visual Studio 2012 在我的第一个网站(使用 twitter oAuth)上工作。我觉得问这个问题很愚蠢,因为我确信它非常简单。

我无法让代码隐藏识别我的 .aspx 页面上的任何 Web 元素。我不确定是否需要更改选项中的某些内容,或者我是否只是遗漏了某些内容。

我通过工具箱向“index.aspx”添加了一个文本框,并为其指定了 id = 'txtUserName'。当我尝试将 txtUserName.Text 分配给 page_load 中的变量时,我收到一条错误消息,指出当前上下文中不存在“txtUserName”。

希望有人可以让我感到震惊,并且我错过了一个简单的标签或其他东西。

索引.aspx:

<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="index.aspx.cs" Inherits="TwitterTest.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>TwitterTest!!!</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Welcome to TwitterTest

    </div>
        <asp:TextBox ID="txtUserName" runat="server" Text=""></asp:TextBox>
    </form>
</body>
</html>

index.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mvc;
using TweetSharp;

namespace TwitterTest
{
    public partial class index : System.Web.UI.Page
    {
        static void Page_Load(object sender, EventArgs e)
        {
            string UserName = txtUserName.Text;
        }

    }
}

最佳答案

您的 Page_Load应该是 protected void Page_Load

关于asp.net - 后面的代码中无法识别 Web 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15423228/

相关文章:

c# - JavaScript 从 C# 代码隐藏中确认

asp.net - 如何捕获 ModalPopupExtenders JavaScript Hide 调用?

asp.net - IIS 6.0 未反射(reflect) .cs 文件更改

c# - Visual Studio 2013 中的浏览器链接

visual-studio-2012 - TF400324 - 源代码管理资源管理器中的 "Page not found"

c++ - MEX 无法找到 VS Express 2013 编译器 - MATLAB R2013a

visual-studio - Visual Studio : multiple projects with same name in one solution

asp.net - IIS URL 重写仅在一页中工作

c# - VS 2012 中不存在“分析代码克隆解决方案”命令

c++ - VS2012无法解析printf,