如何通过迭代其父 ControlPanel 来获取控件的值

发布于 2024-11-17 00:00:46 字数 1284 浏览 2 评论 0原文

我正在用一些控件动态填充 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末 2024-11-24 00:00:46

您必须将 item 转换为适当的控件类型才能访问其属性。

if (!(item is Label | item is LiteralControl))
{
      if(item is TextBox)
      {
        TextBox textBox = (TextBox)item;
        string textValue = textBox.Text;
      }
      ...

}

You'll have to cast item to the appropriate control type to access it's properties.

if (!(item is Label | item is LiteralControl))
{
      if(item is TextBox)
      {
        TextBox textBox = (TextBox)item;
        string textValue = textBox.Text;
      }
      ...

}
帅气尐潴 2024-11-24 00:00:46

或者,您可以使用 Linq 获取 Textbox 的 IEnumerable 和 DropDownLists 的 IEnumerable

IEnumerable<TextBox> txts = Panel_Controls.Controls.OfType<TextBox>();
IEnumerable<DropDownList> ddls = Panel_Controls.Controls.OfType<DropDownList>();

结果枚举已经具有正确的类型。这样您就可以单独迭代可枚举项,因为根据类型的不同,您对每个项目执行的操作也不同。

最终结果是循环内不会有大量 IF:您将有两个迭代块:

foreach(TextBox txt in txts)
{
    //your textbox code
}

foreach(DropDownList ddl in ddls)
{
    //your dropdownlist code
}

Alternatively, you can use Linq to get an IEnumerable of Textboxes and an IEnumerable of DropDownLists:

IEnumerable<TextBox> txts = Panel_Controls.Controls.OfType<TextBox>();
IEnumerable<DropDownList> ddls = Panel_Controls.Controls.OfType<DropDownList>();

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:

foreach(TextBox txt in txts)
{
    //your textbox code
}

foreach(DropDownList ddl in ddls)
{
    //your dropdownlist code
}
花伊自在美 2024-11-24 00:00:46

不能使用控件的 Text 属性吗?这样您就不必关心它是什么类型的控件。您需要该值是什么类型?字符串可以吗?

foreach (Control item in Panel_Controls.Controls)
{
   string value = item.Text;
   // do something with the value
}

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?

foreach (Control item in Panel_Controls.Controls)
{
   string value = item.Text;
   // do something with the value
}
海夕 2024-11-24 00:00:46

您应该将项目投射到文本框,如下所示:

TextBox textbox = item as TextBox;
if (textbox != null)
    string text = textbox.Text;

您可以对任何其他控件执行相同的操作

You should cast the item to the Textbox like:

TextBox textbox = item as TextBox;
if (textbox != null)
    string text = textbox.Text;

You can do the same for any other control

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文