名称<control name =“”>在当前上下文内联</control>中不存在

时间:2014-04-21 18:48:56

标签: c# asp.net .net

我遇到的问题是我在default.aspx上进行内联操作,并且我在我的代码后面创建的集合列表中出现“当前上下文中不存在”错误。我不确定缺少什么。 我可以看到我的代码背后的控件,所以我知道page_load正在解雇。

我应该在设计师那里看到吗?如果没有正确生成是我的原因,我不是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();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

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

class .... 
{
    protected List<Services> lst;
}
顺便说一下,你需要在那里进行for循环很遗憾:take a look at Repeater control.

答案 1 :(得分:0)

与其他答案/评论一样,我同意您需要定义您的变量。我可能添加的是您将其值存储在页面的viewstate中。这甚至可以与母版页一起完成,以便跨多个内容页面安全地保存变量。我是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