迭代面板中的文本框控件 C#
我见过很多其他人也有类似的问题,但我在这里找不到我的逻辑缺陷。任何帮助将不胜感激。
我有一个面板,我在其中添加了许多标签和文本框控件,即:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这仅从集合控件中选择 TextBox 类型的控件。
(与
control is TextBox
或(control as TextBox) != null
相同)如果控件包含在
editQuestionsPanel.Controls
中:选择全部子控件使用下一个扩展方法:
使用:
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
:To select all child controls use next extension method:
Using:
当您动态添加控件时,您需要在每个请求上执行此操作 - 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.
一个经常犯的错误:
Container.Controls
仅包含此容器中的第一级子控件。即:TextBox1在PanelA中,PanelA在PanelB中,在PanelB.Controls中无法获取TextBox1。我的解决方案是编写一个扩展方法:
现在 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:
Now TextBox1 is in
PanelB.AllControls()
. To filter all controls with type, usingPanelB.AllControls().OfType<TextBox>()
如果其他答案没有帮助,请尝试执行代码但添加递归性。如果是 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
你可以这样做:
You can do something like this instead:
试试这个
Try this