[英]Having Trouble Using LINQ to Objects on the Controls Collection of a Page
我需要存储表单中的数据以从会话中恢复。 以下是我针对文本框的通用方法的粗略尝试:
加载会话:
Dictionary<string, string> AppInfo = (Dictionary<string,string>) Session["ApplicantionInfo"];
this.Controls.OfType<TextBox>()
.ToList<TextBox>()
.ForEach( x => x.Text = AppInfo[x.ID] );
保存会议:
Session["ApplicationInfo"] = this.Controls.OfType<TextBox>()
.ToList<TextBox>()
.ToDictionary<TextBox,string>(kvp => kvp.ID);
但是,似乎控件集合不适用于我。 我究竟做错了什么?
快速查看this.Controls.OfType<TextBox>()
在运行时不会产生任何结果。
请参阅使用Linq进行递归控制搜索 。
这就是我访问表单控件的方式。
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].Visible = false;
}
怎么样
public static List<Control> GetAllControls(List<Control> controls, Type t, Control parent)
{
foreach (Control c in parent.Controls)
{
if (c.GetType()== t)
controls.Add(c);
if (c.HasControls())
controls = GetAllControls(controls, t, c);
}
return controls;
}
并打电话
var allControls = new List<Control>();
GetAllControls(allControls, typeof(TextBox), Page);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.