如何通过迭代其父 ControlPanel 来获取控件的值
我正在用一些控件动态填充 ControlPanel...有些是 DropDown,有些是 TextBox:
//inputArray is a JsonArray (thus the SelectToken methods)
foreach (var item in inputArray)
{
//Create Label
Label LabelTitle = new Label();
LabelTitle.Text = (string)item.SelectToken("title");
Panel_Controls.Controls.Add(LabelTitle);
//Create Control
if ((string)item.SelectToken("type") == "textinput")
{
TextBox TextBox_Control = new TextBox();
TextBox_Control.ID = (string)item.SelectToken("title");
Panel_Controls.Controls.Add(TextBox_Control);
}
if ((string)item.SelectToken("type") == "dropdown")
{
DropDownList DropDown_Control = new DropDownList();
DropDown_Control.DataSource = dropDownData;
DropDown_Control.DataBind();
Panel_Controls.Controls.Add(DropDown_Control);
}
}
稍后,我需要获取 DropDown 和文本框字段的值。我可以过滤掉标签和其他控件。我不知道如何获取 foreach 语句中控件的值。我猜我需要将控件强制转换为可以让我获得 .Value 属性的东西,因为通用 Control 不会给我 .Value 属性。
foreach (Control item in Panel_Controls.Controls)
{
if (!(item is Label | item is LiteralControl))
{
//How can I access the .Value of the controls here?
}
}
有人可以建议一种在 foreach 循环中从 TextBox 和 DropDowns 获取值的好方法吗?
非常感谢。
I'm dynamically populating a ControlPanel with some controls... Some are DropDowns, some are TextBoxes:
//inputArray is a JsonArray (thus the SelectToken methods)
foreach (var item in inputArray)
{
//Create Label
Label LabelTitle = new Label();
LabelTitle.Text = (string)item.SelectToken("title");
Panel_Controls.Controls.Add(LabelTitle);
//Create Control
if ((string)item.SelectToken("type") == "textinput")
{
TextBox TextBox_Control = new TextBox();
TextBox_Control.ID = (string)item.SelectToken("title");
Panel_Controls.Controls.Add(TextBox_Control);
}
if ((string)item.SelectToken("type") == "dropdown")
{
DropDownList DropDown_Control = new DropDownList();
DropDown_Control.DataSource = dropDownData;
DropDown_Control.DataBind();
Panel_Controls.Controls.Add(DropDown_Control);
}
}
Later on, I need to get the values of the DropDown and Text box fields. I can filter out the Label and other controls. I can't figure out how to get the values of the Controls within the foreach statement. I'm guessing I need to Cast the control as something that will let me get a .Value property, because the generic Control won't give me a .Value property.
foreach (Control item in Panel_Controls.Controls)
{
if (!(item is Label | item is LiteralControl))
{
//How can I access the .Value of the controls here?
}
}
Could someone suggest a good way of getting values from TextBox and DropDowns within the foreach loop?
Thanks so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须将 item 转换为适当的控件类型才能访问其属性。
You'll have to cast item to the appropriate control type to access it's properties.
或者,您可以使用 Linq 获取 Textbox 的
IEnumerable
和 DropDownLists 的IEnumerable
:结果枚举已经具有正确的类型。这样您就可以单独迭代可枚举项,因为根据类型的不同,您对每个项目执行的操作也不同。
最终结果是循环内不会有大量
IF
:您将有两个迭代块:Alternatively, you can use Linq to get an
IEnumerable
of Textboxes and anIEnumerable
of DropDownLists:The result enumerables already have the correct types. That way you can iterate through the enumerables individually, since what you do with each item is different, depending on the type.
The end result is that you will not have a buch of
IF
inside you loop: you will have two iteration blocks:不能使用控件的 Text 属性吗?这样您就不必关心它是什么类型的控件。您需要该值是什么类型?字符串可以吗?
Can't you use the Text property of the controls? That way you won't have to care what type of control it is. What type do you need the value to be? Will string do?
您应该将项目投射到文本框,如下所示:
您可以对任何其他控件执行相同的操作
You should cast the item to the Textbox like:
You can do the same for any other control