通过TextBox控件在面板C#迭代 [英] Iterating through textbox controls in a panel C#

查看:85
本文介绍了通过TextBox控件在面板C#迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多人用类似的问题,但我不能在这里找到我的逻辑缺陷。任何帮助将大大AP preciated。

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);

创建这些控件和添加的方法中调用previous的迭代法。

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

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

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);
    }
}

我遇到的问题是控制并不是在面板里,当我在这个updateQuestions()方法到达。这里涉及的过程:

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:

一个命令按钮被点击和问题是从数据库读取,对每个问题的方法被调用它增加了2标签和一个文本框editQuestionsPanel.Controls。该面板是一个占位符,然后可见里面。

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.

当点击占位符里面的按钮时,updateQuestions()方法被调用和editQuestionsPanel.Controls.Count = 1。由于有大约在DB 12个问题应该是36左右的面板内的一个控制类型是:

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.

我相信somwhere在生命周期小组的控件都被清除,但我不知道如何分步直通的生命周期。我有一个Page_Load方法这是作为一个按钮被点击,一旦所谓的,但一旦它调用updateQuestions()的按钮时,editQuestionsPanel.Controls.Count已经回到1,所以必须在此之前被清除,但我不知道如何纠正这种...

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...

任何帮助,您可以给你帮我解决这将是极大的AP preciated - 其杀死我。

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

推荐答案

当您添加控件动态,你需要做的对的每个的要求 - 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.

这篇关于通过TextBox控件在面板C#迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆