【问题标题】:The name <control name> does not exist in the current context inline当前上下文内联中不存在名称 <控件名称>
【发布时间】:2014-04-21 18:48:56
【问题描述】:

我在我的 default.aspx 上执行 foreach 内联时遇到问题,我在后面的代码中创建的集合列表 lst 出现“当前上下文中不存在”错误。我不确定缺少什么。 我可以从后面的代码中看到控件,所以我知道 page_load 正在触发。

我应该在我的设计师身上看到 lst 吗?如果没有正确生成是原因,我不是 100% 肯定的。我添加了一个测试文字并添加了它。我在调试和运行时编译了我的项目。然后我在构建之后也为每个添加了我的内联。

  • 命名空间匹配
  • Autoevent 设置为 true
  • 输入 !Page.IsPostback

我可以遍历 lst 并将值附加到文字并查看通过的值。

感谢您的帮助。

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/MasterPages/WebSite.master"     CodeBehind="Default.aspx.cs" Inherits="Admin._Default" AutoEventWireup="true"%>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<h2></h2>
<div>
    <% foreach (Services service in lst)
       {  %>

    <% } %>
    <asp:Literal ID="ltrl_ServiceList" runat="server" />
    </div>
</asp:Content>

背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using RecycleAdmin.Utilities;
using System.Data.SqlClient;
using System.Reflection;

namespace Admin
{

public partial class _Default : System.Web.UI.Page
{
    //Create my properties

    protected void Page_Load(object sender, EventArgs e)
    {



        getListOfClosuresFollowups();
        if (!Page.IsPostBack)
        {
            DataTable dt = getTable();
            List<Services> lst = dt.ToCollection<Services>();

            foreach (Services service in lst)
            {
                ltrl_ServiceList.Text += service.service_nm.ToString();
            }
        }
    }
}

【问题讨论】:

  • 'lst' 似乎是 Page_Load 中的一个局部变量,而不是页面本身的属性。
  • lst 是一个局部变量,您必须将其声明为实例级成员。
  • 您使用 for 循环的方法更类似于 Razor 语法,但 ASP.NET Web 窗体使用 Repeater control 来完成同样的事情。

标签: c# asp.net .net


【解决方案1】:

好的,您已经创建了一个名为 lst 的局部变量,但是服务器标记如何知道它的存在???

至少,您需要将其声明为 protected 类字段或属性,并且页面的标记部分可以访问它:

class .... 
{
    protected List<Services> lst;
}

顺便说一句,很遗憾您需要在那里执行for 循环:take a look at Repeater control.

【讨论】:

  • 感谢 Matias 的帮助和解答。我不是故意使用中继器控件或任何控件。我想更好地理解使用列表和泛型,而不仅仅是将我的数据集绑定到控件并让它为我完成所有工作。我今天在这个问题上学到了一些如果我只是扑通一声加入中继器就不会学到的东西。
  • @user2316023 没问题!如果你只是学习,没关系。否则,使用像 ASP Classic 这样的 ASP.NET 将是一个巨大的失败。
【解决方案2】:

与此处的其他答案/cmets 一样,我同意您需要定义变量。我可能要补充的是,您将其值存储在页面的视图状态中。这甚至可以与母版页一起完成,以跨多个内容页安全地保存变量。我是一名 VB.net 程序员,但这里有一些示例 MSDN 代码或click here 了解更多信息。

在视图状态中存储属性:

public String Text
        {
            get 
            { 
                object o = ViewState["Text"]; 
                return (o == null)? String.Empty : (string)o;
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

这是我找到的另一个有用的链接:MSDN How to: Save Values in View State

【讨论】:

    猜你喜欢
    • 2014-12-09
    • 1970-01-01
    • 2013-10-02
    • 2014-04-22
    • 2017-06-17
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多