Question

I've been trying to follow a few tutorials and code examples but something isn't working as I'd expect.

I have a series of textboxes on a webpage and I need to loop through each one and save it's value to the database. The number of textboxes on the page will vary. I load them from a database. They are all added to a table object.

TableCell cellb = new TableCell();
TextBox txtAnswer = new TextBox();
txtAnswer.TextMode = TextBoxMode.MultiLine;
txtAnswer.Rows = 2;
txtAnswer.ID = "field_" + dataRow["fieldID"].ToString();
txtAnswer.Text = "answer"; //this will be got from the database

cellb.Controls.Add(txtAnswer);

so that adds the textbox to the table row. I then have a save button which has the following code

foreach (Control c in Page.Controls)
{
    foreach (Control childc in c.Controls) 
    {
        if (childc is TextBox)
        {
            TextBox tmpText = (TextBox)childc;
            tmpField = tmpText.ID.Split('_');
            fieldID = Convert.ToInt32(tmpField[1]);
            //save value to the database (eventually)
            debug.InnerHtml += tmpText.Text; //this just outputs the values for now
        }
    }
}

So the above should loop though all the page controls and find the textfields as added on the page_load. However, I am now wondering if it's because they don't exist. So when I save the page, it doesn't know of the controls. I can see the table control, but nothing inside it.... any ideas?!

Was it helpful?

Solution

Dynamic controls must be added on each page request. Preferably during the Init event. It sounds like they have not been added yet (again) by the time you iterate through your controls.

Also, if you know your TextBoxes are within a specific control you should probably find that control first and then, using the same approach you are using, iterate over the controls. Two reasons for this are: efficiency and also, on your code, you are only searching two levels down from the page control. This may be ok but it includes other controls that will not contain any of those textboxes.

OTHER TIPS

Why dont you use a Gridview Control and Then Make custom template with Texbox. Then ON Page Load you can add no of Textboxes you want and then loop the gridview and save the data.

First thing. Make sure you are not creating your controls inside an if (!isPostBack){} - as they need recreating on each postback.

Secondly, I don't believe your loop will find all controls as it will only really travel through the first level.

Ideally, you should search for the controls recursivley.

Here is a recursive method that I use - this will help find all controls of a given ID.

    /// <summary> 
    /// Finds a Control recursively. Note finds the first match that exists 
    /// </summary> 
    /// <param name="ContainerCtl">Should be the lowest container in the heirarchy, for eg dont choose Master page if you can pick the specific panel</param> 
    /// <param name="IdToFind">ID of the control you are looking for</param> 
    /// <returns>the control if found else null</returns> 
    public static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id) { return Root; }

        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null) { return FoundCtl; }
        }
        return null;
    }

Now, what I would do is:

When you create your TextBox's, store all the ID's in an Array. Then when you need to access them, loop through the Array and for each entry, call the above method. this will then return the TextBox you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top