Access generated htmlControls in codebehind after a postback

543 Views Asked by At

I'm new on ASP.NET and I'm a bit lost and don't know if I'm on the right approach.

In a very large form, I'm rendering a few tables with lot's of input fields.

In the aspx I have this:

<tbody runat="server" id="a2_tbody" ></tbody>

In codebehind I access the tbody and render the tr, td's and inputs with methods like this:

    private void generateA2TableRows(int num_rows, HtmlGenericControl parent) { 

    for(int i=0; i<num_rows; i++){
        string ctrlName = String.Empty;
        TableRow tr = new TableRow();

        TableCell c_designacao = new TableCell();
        HtmlInputText i_designacao = new HtmlInputText("text");
        ctrlName = "a2_designacao_" + i;
        i_designacao.ID = ctrlName;
        i_designacao.Name = ctrlName;
        i_designacao.Attributes.Add("class", "validate[custom[blackList]]");
        c_designacao.Controls.Add(i_designacao);
        tr.Controls.Add(c_designacao);

        TableCell c_ss = new TableCell();
        HtmlSelect seg_social = new HtmlSelect();
        ctrlName = "a2_seg_social_" + i;
        seg_social.Items.Insert(0, "Selecione");
        seg_social.SelectedIndex = 0;
        seg_social.Items.Insert(1,"Sim");
        seg_social.Items.Insert(2,"Não");
        seg_social.ID = ctrlName;
        seg_social.Name = ctrlName;
        c_ss.Controls.Add(seg_social);
        tr.Controls.Add(c_ss);            

        TableCell c_n_utentes = new TableCell();
        HtmlInputText i_n_utentes = new HtmlInputText("text");
        i_n_utentes.Attributes.Add("class", "validate[custom[onlyPositiveNumbers]]");
        ctrlName = "a2_n_utentes_" + i;
        i_n_utentes.ID = ctrlName;
        i_n_utentes.Name = ctrlName;
        c_n_utentes.Controls.Add(i_n_utentes);
        tr.Controls.Add(c_n_utentes);

        parent.Controls.Add(tr);
    }

}

But now I need to access all the inputs to validate and other processing. I've been trying this ways:

This one only returns htmlControls that I've written on the aspx.

 foreach (Control ctrl in Form.Controls) {
        if (ctrl is HtmlControl)
        {
            HtmlControl htmlCtrl = ctrl as HtmlControl;

            Response.Write("ID: " + htmlCtrl.ID + "             CLASS = " + htmlCtrl.Attributes["class"] +"         type = "+ htmlCtrl.GetType() + "<br/>");
        }
    }

This way I get the rendered elements, but only the name. I've tried to recunstruct the ID of the element and use the FindControl method but the "Form" doesn't return any element that way.

        foreach (Object formObj in Request.Form) {
        string formObjID = formObj.ToString().Replace("$", "_");
        Control ctrl = Form.FindControl(formObjID);

    }

Can anyone unstuck me from this one? The main objective is really just to access the rendered elements so I can validate them and serialize the Data to an XML, and really don't know if this is the right approach.

Thanks in Advance.

1

There are 1 best solutions below

0
On

Hi the approach will be to regenerate all the controls for each request in that way you can access each control. Can create all the controls in the OnInit event.