0

在我的应用程序中,我有一个母版页、一个使用母版页的网页、一个面板,在面板控件中,我有几个文本框控件和一个用于触发回发的按钮。我编写了以下代码来读取文本框中输入的值。但是,文本框的计数为 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);
    }
4

2 回答 2

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);
}
于 2013-10-13T22:48:10.397 回答
-1

尝试

protected void Button1_Click(object sender, EventArgs e)
    {
        ArrayList allActions = new ArrayList();
        ManageDef setStatus = new ManageDef();
        allActions = setStatus.GetAllTextBoxes(this.parent);
    }
于 2013-10-13T20:47:57.523 回答