迭代面板中的文本框控件 C#

发布于 2024-09-16 08:09:27 字数 1313 浏览 5 评论 0原文

我见过很多其他人也有类似的问题,但我在这里找不到我的逻辑缺陷。任何帮助将不胜感激。

我有一个面板,我在其中添加了许多标签和文本框控件,即:

myPanel.Controls.Add(txtBox);

这些控件是在迭代方法之前调用的方法中创建和添加的。

我想迭代每个文本框并使用其 Text 属性作为另一个方法中的参数,但我没有任何运气。这是我的迭代尝试:

public void updateQuestions()
{
    try
    {
        foreach (Control c in editQuestionsPanel.Controls)
        {
            if (c is TextBox)
            {
                TextBox questionTextBox = (TextBox)c;

                string question = questionTextBox.Text;

                writeNewQuestionToTblQuestions(question);
            }
        }
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message);
    }
}

我遇到的问题是,当我到达此 updateQuestions() 方法时,控件不在面板中。以下是所涉及的过程:

单击命令按钮并从数据库中读取问题,对于每个问题调用一个方法,该方法向 editQuestionsPanel.Controls 添加 2 个标签和一个文本框。该面板位于 PlaceHolder 内,然后使其可见。

单击 PlaceHolder 内的按钮时,将调用 updateQuestions() 方法,并且 editQuestionsPanel.Controls.Count = 1。由于数据库中有大约 12 个问题,因此应该在 36 左右。Panel 内的一个控件是 类型:

System.Web.UI.LiteralControl  

它不包含任何控件。

我确信在生命周期的某个地方面板的控件正在被清除,但我不知道如何逐步完成生命周期。我有一个 Page_load 方法,单击按钮后就会调用该方法,但是单击调用 updateQuestions() 的按钮后,editQuestionsPanel.Controls.Count 已经回到 1,因此必须在此之前清除它,但我不知道如何纠正这个问题...

您能提供的任何帮助来帮助我解决这个问题将不胜感激 - 它杀了我!

I have seen many others with similar problems but I cannot find the flaw in my logic here. Any help would be greatly appreciated.

I have a Panel which I have added numerous label and textbox controls to, ie:

myPanel.Controls.Add(txtBox);

These controls are created and added in a method called previous to the iteration method.

I want to iterate through each textbox and use its Text property as a parameter in another method but I am not having any luck. Here is my attempt to iterate:

public void updateQuestions()
{
    try
    {
        foreach (Control c in editQuestionsPanel.Controls)
        {
            if (c is TextBox)
            {
                TextBox questionTextBox = (TextBox)c;

                string question = questionTextBox.Text;

                writeNewQuestionToTblQuestions(question);
            }
        }
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message);
    }
}

The problem I am having is that the controls are not in the Panel when I arrive at this updateQuestions() method. Here is the process involved:

A commandButton is clicked and the questions are read from a DB, for each question a method is called which adds 2 labels and a textbox to editQuestionsPanel.Controls. This panel is inside a PlaceHolder which is then made visible.

When a button inside the PlaceHolder is clicked, the updateQuestions() method is called and the editQuestionsPanel.Controls.Count = 1. As there are approx 12 questions in the DB it should be around 36. The one control inside the Panel is of type:

System.Web.UI.LiteralControl  

It contains no controls.

I am sure that somwhere in the lifecycle the Panel's controls are being cleared but I do not know how to step thru the life cycle. I have a Page_load method which is called as soon as a button is clicked but once the button which calls updateQuestions() is clicked the editQuestionsPanel.Controls.Count is already back to 1 so it must be cleared before this but I do not know how to correct this...

Any help you can give to help me solve this would be greatly appreciated - its killing me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

﹏雨一样淡蓝的深情 2024-09-23 08:09:27

这仅从集合控件中选择 TextBox 类型的控件。

(与 control is TextBox(control as TextBox) != null 相同)

如果控件包含在 editQuestionsPanel.Controls 中:

using System.Linq;

IEnumerable<TextBox> textBoxes = editQuestionsPanel.Controls.OfType<TextBox>();    
foreach (TextBox textBox in textBoxes)
{
    // do stuff
}

选择全部子控件使用下一个扩展方法:

public static IEnumerable<T> GetChildControls<T>(this Control control) where T : Control
{
    var children = control.Controls.OfType<T>();
    return children.SelectMany(c => GetChildControls<T>(c)).Concat(children);
}

使用:

IEnumerable<TextBox> textBoxes = editQuestionsPanel.GetChildControls<TextBox>();

This selects from collection controls only that which are of type TextBox.

(the same as control is TextBox or (control as TextBox) != null)

If controls are contained in editQuestionsPanel.Controls:

using System.Linq;

IEnumerable<TextBox> textBoxes = editQuestionsPanel.Controls.OfType<TextBox>();    
foreach (TextBox textBox in textBoxes)
{
    // do stuff
}

To select all child controls use next extension method:

public static IEnumerable<T> GetChildControls<T>(this Control control) where T : Control
{
    var children = control.Controls.OfType<T>();
    return children.SelectMany(c => GetChildControls<T>(c)).Concat(children);
}

Using:

IEnumerable<TextBox> textBoxes = editQuestionsPanel.GetChildControls<TextBox>();
初雪 2024-09-23 08:09:27

当您动态添加控件时,您需要在每个请求上执行此操作 - asp.net 不会为您执行此操作!

在初始化或加载阶段添加控件,然后它们将填充回发值。

When you add controls dynamically, you need to do that on every request - asp.net doesn't do that for you!

Add the controls in the Init or Load phase, then they will get populated with the postback values.

活雷疯 2024-09-23 08:09:27

一个经常犯的错误:Container.Controls 仅包含此容器中的第一级子控件。即:TextBox1在PanelA中,PanelA在PanelB中,在PanelB.Controls中无法获取TextBox1。

我的解决方案是编写一个扩展方法:

public static IEnumerable<Control> AllControls(this Control ctl)
{
   List<Control> collection = new List<Control>();
   if (ctl.HasControls())
   {
       foreach (Control c in ctl.Controls)
       {
             collection.Add(c);
             collection = collection.Concat(c.AllControls()).ToList();
       }
   }
   return collection;
}

现在 TextBox1 位于 PanelB.AllControls() 中。要使用类型过滤所有控件,请使用 PanelB.AllControls().OfType()

A frequently made mistake: Container.Controls only contains the first level child controls in this container. That is: TextBox1 in PanelA, PanelA in PanelB, you can't get TextBox1 in PanelB.Controls.

My solution is to write an extension method:

public static IEnumerable<Control> AllControls(this Control ctl)
{
   List<Control> collection = new List<Control>();
   if (ctl.HasControls())
   {
       foreach (Control c in ctl.Controls)
       {
             collection.Add(c);
             collection = collection.Concat(c.AllControls()).ToList();
       }
   }
   return collection;
}

Now TextBox1 is in PanelB.AllControls(). To filter all controls with type, using PanelB.AllControls().OfType<TextBox>()

魔法少女 2024-09-23 08:09:27

如果其他答案没有帮助,请尝试执行代码但添加递归性。如果是 editQuestionsPanel => 您的代码将无法工作面板=>文本框

If the other answers don't help, try doing your code but add recursivity. Your code would not work if it's editQuestionsPanel => Panel => Textbox

梦回梦里 2024-09-23 08:09:27

你可以这样做:

var questions = from tb in editQuestionsPanel.Controls.OfType<TextBox>()
                select tb.Text;

foreach(var question in questions)
{
    writeNewQuestionToTblQuestions(question);
}

You can do something like this instead:

var questions = from tb in editQuestionsPanel.Controls.OfType<TextBox>()
                select tb.Text;

foreach(var question in questions)
{
    writeNewQuestionToTblQuestions(question);
}
心欲静而疯不止 2024-09-23 08:09:27

试试这个

        int Count = 0;
        foreach (Control ctr in Panel1.Controls)
        {
            if (ctr is TextBox)
                Count++;
        }

Try this

        int Count = 0;
        foreach (Control ctr in Panel1.Controls)
        {
            if (ctr is TextBox)
                Count++;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文