我在页面中创建了一个List作为全局变量。
public static List<LinkButton> allControlsLinkButtonSalles = new List<LinkButton>();
在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 source)+269 c:\ Users .... \ Documents \ Visual Studio 2012 \ WebSites \ test1 \ test2MasterPage.aspx.cs中的test2MasterPage.Page_init():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(Control namingContainer)+134 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean 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);
}
}
答案 0 :(得分:0)
您没有正确使用静态关键字。 静态变量属于类本身,不属于当前的实例。
尝试使用:
public List<LinkButton> allControlsLinkButtonSalles
{
get
{
if(Session["allControlsLinkButtonSalles"] == null)
Session["allControlsLinkButtonSalles"] = new List<LinkButton>();
return (List<LinkButton>) Session["allControlsLinkButtonSalles"];
}
set
{
Session["allControlsLinkButtonSalles"] = value;
}
}
另一个需要注意的是,实际上某些东西进入列表,尝试调试它。
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
起初遇到了和你一样的错误,意识到我在你正在查看的 List 中没有控件,因为我没有任何 LinkButton 的类正在搜索有关 allControlsLinkButtonSalles 列表的信息。
我在页面上的所有 LinkButton 上添加了“”linkButtonSalleActive“”的CssClass,但没有出现调试错误。
在没有 Salle 特定链接的时间添加检查列表实际上是空的,并且您不应该有任何问题。
以下是我使用的示例代码:
Default.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);
}
}
}
}
答案 3 :(得分:0)
根据您的代码,只有allControlsLinkButtonSalles
,
childControl.CssClass == "linkButtonSalleActive"
的代码值
我确信没有添加任何值,请检查您在literal2.Text
获取的内容: -
literal2.Text += " taille "+ allControlsLinkButtonSalles .Count();
说过你不应该在页面上使用静态变量或属性,除非你有正当理由这样做。