读取屏幕所有文本框的值

时间:2013-10-13 20:45:23

标签: asp.net

在我的应用程序中,我有一个母版页,一个使用母版页的网页,一个面板,在面板控件中我有几个文本框控件和一个触发回发的按钮。我编写了以下代码来读取在文本框中输入的值。但是,我的文本框得到0计数。你可以告诉我代码中有什么问题:

    // Function to read the entries of the text boxes.
    public ArrayList GetAllTextBoxes(Control parent)
    {
        ArrayList myActions = new ArrayList();
        foreach (Control c in parent.Controls)
        {
            // Check and see if it's a textbox  
            if ((c.GetType() == typeof(TextBox)))
            {
                // Since its a textbox read the text     
                myActions.Add(((TextBox)(c)).Text);
            }
            // Now we need to call itself (recursion) 
            if (c.HasControls())
            {
                GetAllTextBoxes(c);
            }
        }
        return myActions;
    }

    and this function is called like this on the click button function:

    protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList allActions = new ArrayList();
        ManageDef setStatus = new ManageDef();
        allActions = setStatus.GetAllTextBoxes(this);
    }

2 个答案:

答案 0 :(得分:0)

您可以这样写GetAllTextBoxes()

// Function to read the entries of the text boxes.
public ArrayList GetAllTextBoxes(Control parent)
{
    ArrayList myActions = new ArrayList();
    if(parent.GetType() == typeof(Panel))//You may want to add other containers as well
    {
        foreach (Control c in parent.Controls)
        {
            // Check and see if it's a textbox  
            if ((c.GetType() == typeof(TextBox)))
            {
                // Since its a textbox read the text     
                myActions.Add(((TextBox)(c)).Text);
            }
            // Now we need to call itself (recursion) 
            else if(c.GetType() == typeof(Panel))//Check for other containers if needed
            {
                if (c.HasControls())
                {
                    ArrayList myOtherActions = GetAllTextBoxes(c);
                    myActions.AddRange(myOtherActions);
                }
            }
        }
    }

    return myActions;
}

并称之为:

protected void Button1_Click(object sender, EventArgs e)
{
    ArrayList allActions = new ArrayList();
    ManageDef setStatus = new ManageDef();
    allActions = GetAllTextBoxes(Panel1);
    //Not sure if the buttton is inside the panel
    //allActions = GetAllTextBoxes(((Button)sender).Parent);
}

答案 1 :(得分:-1)

尝试

protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList allActions = new ArrayList();
        ManageDef setStatus = new ManageDef();
        allActions = setStatus.GetAllTextBoxes(this.parent);
    }