1

我在我的页面中创建了一个列表作为全局变量。

public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();

在一个名为 during 的函数中Page_Load,我添加了一些这样的元素:

foreach (var childControl in allControlsLinkButton)
{
    if (childControl.CssClass == "linkButtonSalleActive" ||  childControl.CssClass == "linkButtonSalle")
    {
        allControlsLinkButtonSalles.Add(childControl);
    }
}

在那之后,当我这样做时:

foreach (LinkButton value in allControlsLinkButtonSalles)
{
    literal2.Text += " <br /> Text " + value.Text;
}

肯定有3个元素出现。但是,当我尝试这样做时:

literal2.Text += " First element " + allControlsLinkButtonSalles.First().Text;

发生错误。这怎么可能?

这是消息:

说明:执行当前 Web 请求期间发生未处理的异常。检查堆栈跟踪以获取有关错误及其在代码中的位置的更多信息。

异常详细信息:System.InvalidOperationException:序列不包含任何元素。

源错误:

Ligne 605 : } Ligne 606 : Ligne 607 : literal2.Text += "First" + allControlsLinkBut​​tonSalles.First().Text; Ligne 608 : Ligne 609 : //allControlsLinkBut​​tonSalles[0].CssClass = "linkBut​​tonSalleActive"; 堆栈跟踪:

[InvalidOperationException:序列不包含元素。] System.Linq.Enumerable.First(IEnumerable `1 源)+269 test2MasterPage.Page_init() in c:\Users....\Documents\Visual Studio 2012\WebSites\test1\ test2MasterPage.aspx.cs:607 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +9807957 System.Web.UI.Control.OnInit(EventArgs e) +92 System.Web.UI.Page.OnInit( EventArgs e) +12 System.Web.UI.Control.InitRecursive(控件命名容器)+134 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔 includeStagesAfterAsyncPoint)+489

这是完整的代码:

public static List<DataTable> ListTable = new data().GetTable();
public static List<string> SallesList = new data().SallesListCreation(ListTable[0]);

//DataTable dt = new data().
public static int Load_Counter = 0;
List<Button> allControlsButton = new List<Button>();
public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();

List<LinkButton> allControlsLinkButtonAffichages = new List<LinkButton>();
List<LinkButton> allControlsLinkButtonSemaine = new List<LinkButton>();

protected void Page_Load(object sender, EventArgs e)
{
    literal2.Text += "<br /> counter : " + Load_Counter.ToString();
    DateTime today = DateTime.Now;
    string sToday = DateTime.Now.ToString("dd/MM/yyyy");
    string finDate = today.AddDays(+6).ToString("dd/MM/yyyy");
    literaltest.Text = "Semaine du " + sToday + " au " + finDate;

    PlaceHolder1.Controls.Add(new LiteralControl("<br /><br /><br /> kyofu<br /><br />"));

    foreach (string sallesel in SallesList)
    {
        PlaceHolder1.Controls.Add(CreateLinkButton(sallesel + "lkbtn", sallesel, "linkButtonSalle"));
    }

    Page_init();
}

protected void Page_init()
{
    List<LinkButton> allControlsLinkButton = new List<LinkButton>();
    GetControlList<LinkButton>(Page.Controls, allControlsLinkButton);
    DateTime today = DateTime.Now;
    string sToday = DateTime.Now.ToString("dd/MM/yyyy");

    // the list of controllers is filled
    foreach (var childControl in allControlsLinkButton)
    {
        if (childControl.CssClass == "linkButtonSalleActive" ||  childControl.CssClass == "linkButtonSalle")
        {
            allControlsLinkButtonSalles.Add(childControl);
            literal2.Text += " allControlsLinkButtonSalles " + childControl.Text;
        }

        if (childControl.CssClass == "linkButtonAffichage" ||  childControl.CssClass == "linkButtonAffichageActive")
        {
            allControlsLinkButtonAffichages.Add(childControl);
        }
        if (childControl.CssClass == "linkButtonSemaine" || childControl.CssClass == "linkButtonSemaineActive")
        {
            allControlsLinkButtonSemaine.Add(childControl);
            SemaineSync(childControl);
        }
    }
    literal2.Text += " taille " + allControlsLinkButtonSalles.Count();
    //literal2.Text += " Text " + allControlsLinkButtonSalles[1].Text;

    foreach (LinkButton value in allControlsLinkButtonSalles)
    {
        literal2.Text += " <br /> Text " + value.Text;
    }

    literal2.Text += " First " + allControlsLinkButtonSalles.First().Text;
    ListFilmsBySalle(SallesList[0]);
}

private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
where T : Control
{
    foreach (Control control in controlCollection)
    {
        //if (control.GetType() == typeof(T))
        if (control is T) // This is cleaner
            resultCollection.Add((T)control);

        if (control.HasControls())
            GetControlList(control.Controls, resultCollection);
    }
}
4

4 回答 4

0
Create a Static Class and Create some Attributes and Set your Values to those     

Attributes their Whenever you Want you can take it from their if Data has any change 

Again set those attributes so that you will get your data from the Class

Public static class Helper
     {
        public string SOMEPROPERTY
          {
             get;
             set;
          }
              .
              .
              .

        public List<LinkButton> SOMEPROPERTY
          {
             get;
             set;
          }


      }              
于 2013-08-09T12:09:45.130 回答
0

根据您的代码值, allControlsLinkButtonSalles仅在以下情况下添加到列表中childControl.CssClass == "linkButtonSalleActive"

我确信没有增加任何价值,请检查您在以下位置获得的内容literal2.Text:- literal2.Text += " taille "+ allControlsLinkButtonSalles .Count();

话虽如此,除非您有正当理由,否则不应在页面上使用静态变量或属性。

于 2013-08-26T09:18:57.970 回答
0

您没有正确使用静态关键字。静态变量,属于类本身,不属于当前实例。

尝试使用:

   public List<LinkButton> allControlsLinkButtonSalles
   {
       get
       {
           if(Session["allControlsLinkButtonSalles"] == null)
               Session["allControlsLinkButtonSalles"] = new List<LinkButton>();
           return (List<LinkButton>) Session["allControlsLinkButtonSalles"];

       }
       set
       {
           Session["allControlsLinkButtonSalles"] = value;
       }
   }

另一个要寻找的问题是,实际上有一些东西进入了列表,尝试调试它。

于 2013-08-09T11:00:08.437 回答
0

开始时遇到与您相同的错误,意识到我在您正在查看的List中没有控件,因为我没有LinkBut ​​ton与正在搜索的关于allControlsLinkBut​​tonSalles列表的任何一个类。

我在页面上的所有LinkBut ​​ton 上添加了“linkBut​​tonSalleActive”的 CssClass,但没有出现调试错误。

在没有Salle特定链接的情况下添加检查列表实际上是空的,并且您不应该有任何问题。

这是我使用的示例代码:

默认.aspx

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" id="literal2" /> <br />
        <asp:Label runat="server" id="literaltest" /> <br />
        <asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="1"/>
        <asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="2"/>
        <asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="3"/>
        <asp:LinkButton runat="server" CssClass="linkButtonSalleActive" text="4"/>
    </div>
    </form>
</body>
</html>

Default.aspx.cs(修改版你完整代码)

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

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        //DataTable dt = new data().
        public static int Load_Counter = 0;
        List<Button> allControlsButton = new List<Button>();
        public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();

        List<LinkButton> allControlsLinkButtonAffichages = new List<LinkButton>();
        List<LinkButton> allControlsLinkButtonSemaine = new List<LinkButton>();

        protected void Page_Load(object sender, EventArgs e)
        {
            literal2.Text += "<br /> counter : " + Load_Counter.ToString();
            DateTime today = DateTime.Now;
            string sToday = DateTime.Now.ToString("dd/MM/yyyy");
            string finDate = today.AddDays(+6).ToString("dd/MM/yyyy");
            literaltest.Text = "Semaine du " + sToday + " au " + finDate;

            //PlaceHolder1.Controls.Add(new LiteralControl("<br /><br /><br /> kyofu<br /><br />"));

            //foreach (string sallesel in SallesList)
            //{
            //    PlaceHolder1.Controls.Add(CreateLinkButton(sallesel + "lkbtn", sallesel, "linkButtonSalle"));
            //}

            Page_init();
        }

        protected void Page_init()
        {
            List<LinkButton> allControlsLinkButton = new List<LinkButton>();
            GetControlList<LinkButton>(Page.Controls, allControlsLinkButton);
            DateTime today = DateTime.Now;
            string sToday = DateTime.Now.ToString("dd/MM/yyyy");

            // the list of controllers is filled
            foreach (var childControl in allControlsLinkButton)
            {
                if (childControl.CssClass == "linkButtonSalleActive" || childControl.CssClass == "linkButtonSalle")
                {
                    allControlsLinkButtonSalles.Add(childControl);
                    literal2.Text += " allControlsLinkButtonSalles " + childControl.Text;
                }

                if (childControl.CssClass == "linkButtonAffichage" || childControl.CssClass == "linkButtonAffichageActive")
                {
                    allControlsLinkButtonAffichages.Add(childControl);
                }
                if (childControl.CssClass == "linkButtonSemaine" || childControl.CssClass == "linkButtonSemaineActive")
                {
                    allControlsLinkButtonSemaine.Add(childControl);
                    //SemaineSync(childControl);
                }
            }
            literal2.Text += " taille " + allControlsLinkButtonSalles.Count();
            //literal2.Text += " Text " + allControlsLinkButtonSalles[1].Text;

            foreach (LinkButton value in allControlsLinkButtonSalles)
            {
                literal2.Text += " <br /> Text " + value.Text;
            }

            /*
             * CHANGES HERE
             */

            literal2.Text += allControlsLinkButtonSalles.Count > 0 ?
                " First " + allControlsLinkButtonSalles.First().Text :
                String.Empty;
            //ListFilmsBySalle(SallesList[0]);
        }

        private void GetControlList<T>(ControlCollection controlCollection, List<T> resultCollection)
        where T : Control
        {
            foreach (Control control in controlCollection)
            {
                //if (control.GetType() == typeof(T))
                if (control is T) // This is cleaner
                    resultCollection.Add((T)control);

                if (control.HasControls())
                    GetControlList(control.Controls, resultCollection);
            }
        }
    }
}
于 2013-08-09T15:14:20.200 回答