【问题标题】:I cannot access Web Form controls on code-behind in Xamarin我无法访问 Xamarin 中代码隐藏的 Web 表单控件
【发布时间】:2015-11-08 00:10:54
【问题描述】:

我在 Xamarin IDE 上创建了新的 ASP.NET (C#) 项目。我的默认页面有一些 asp 控件,如标签、文本框和按钮。当我尝试从代码隐藏中访问它们时,我收到了这个错误:

error CS0103: The name xxxx does not exist in the current context.

这是我尝试访问它们的方式:

usernameTXT.Text = "John";

这是我的代码隐藏:

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace advise
{

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            usernameTXT.Text = "John";
        }
    }
}

这是我在 aspx 页面中的控件的样子:

<asp:TextBox id="usernameTXT" runat="server" CssClass="control"></asp:TextBox>

值得一提的是,在xxx.aspx.designer.cs中没有生成任何东西:

using System;
using System.Web;
using System.Web.UI;

namespace advise
{

    public partial class Default
    {

    }
}

请帮忙!我试图搜索这个问题没有任何帮助。这很奇怪,因为 Xamarin 为我做的代码隐藏自动与 Web 窗体相关联。

【问题讨论】:

  • 是否“受保护的 System.Web.UI.WebControls.TextBox usernameTXT;”是否存在于自动生成的XXXX.aspx.designer.cs 文件中?如果没有,“重建所有”并再次检查。
  • XXXX.aspx.designer.cs 中实际上没有生成任何东西。
  • @RobertN 我做了“全部重建”但没有任何进展!

标签: c# asp.net webforms xamarin


【解决方案1】:

错误 CS0103:名称 xxxx 在当前上下文中不存在。

是否“受保护的 System.Web.UI.WebControls.TextBox usernameTXT;”是否存在于自动生成的 XXXX.aspx.designer.cs 文件中?

  • 如果不是,请“全部重建”并再次检查。

如果 XXXX.aspx.designer.cs(其中 XXXX 是您的页面名称,在您的情况下为 Default)没有自动更新以匹配您的 asp: 定义,那么您的 aspx/xml 文件中有语法错误。

删除(或注释掉)导致编译错误的定义和 Run 项目,生成的 Web 浏览器页面将向您显示解析错误以及有关错误在您的 .aspx 文件中发生的位置的信息:

示例:

System.Web.Compilation.ParseException
Expecting </asp:TextBox> System.Web.UI.WebControls.TextBoxControlBuilder

Description: Error parsing a resource required to service this request. Review your source file and modify it to fix this error.
Details: Expecting </asp:TextBox> System.Web.UI.WebControls.TextBoxControlBuilder
Error origin: Parser
Error source file: /Users/administrator/Documents/Code/Xamarin/aspnet/aspnet/Default.aspx
Exception stack trace:
   at System.Web.Compilation.AspGenerator.Parse (System.IO.TextReader reader, System.String filename, Boolean doInitParser) in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/AspGenerator.cs:line 627
   at System.Web.Compilation.GenericBuildProvider`1[TParser].Parse () in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/GenericBuildProvider.cs:line 87
   at System.Web.Compilation.GenericBuildProvider`1[TParser].GenerateCode () in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/GenericBuildProvider.cs:line 102
   at System.Web.Compilation.GenericBuildProvider`1[TParser].GenerateCode (System.Web.Compilation.AssemblyBuilder assemblyBuilder) in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/GenericBuildProvider.cs:line 121
   at System.Web.Compilation.BuildManager.GenerateAssembly (System.Web.Compilation.AssemblyBuilder abuilder, System.Web.Compilation.BuildProviderGroup group, System.Web.VirtualPath vp, Boolean debug) in /private/tmp/source-mono-mac-4.2.0-branch/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.1/mcs/class/System.Web/System.Web.Compilation/BuildManager.cs:line 778
Error source context:
Error lines: 11, 11
9:      <asp:Button id="button1" runat="server" Text="Click me!" OnClick="button1Clicked" />
10:         <asp:TextBox id="usernameTXT" runat="server" CssClass="control">foobar</asp:TextBox>
11:         <asp:TextBox id="userneTXT" runat="server" CssClass="control">foobar</asp:TextBoxz>
12:     </form>
13: </body>

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多