我在我的页面中创建了一个列表作为全局变量。
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" + allControlsLinkButtonSalles.First().Text; Ligne 608 : Ligne 609 : //allControlsLinkButtonSalles[0].CssClass = "linkButtonSalleActive"; 堆栈跟踪:
[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);
}
}