I have a GridView which shows order header details (e.g. order number, customer name). Each row in the GridView has a 'view details' button, which displays another Panel containing order line details (orderline number, quantity, price). All the detail lines for that order are displayed in a PlaceHolder control which generates a Label and a TextBox for each line. The quantity against the order line is shown inside a TextBox, which the user can change the value of, and then hit a Save button. However on postback the controls don't exist.

I have previously been able to generate dynamic controls in the PageInit, however that only worked for me because the controls were using data that did not require the user to select something first. With the scenario I have, the dynamic controls are generated based on the order that the user selects.

This is the code that generates the dynamic controls (removed some code to simplify):

protected void gvOrders_RowCommand(object sender, GridViewCommandEventArgs e)
    if (e.CommandName == "EditOrder")
        // show panel
        pnlEditOrder.Visible = true;

        // get order id
        Int64 OrderHeaderID = Convert.ToInt64(e.CommandArgument.ToString());

        // get order line details
        Business obj = new Business();
        DataTable dt = obj.GETPOLineDetails(OrderHeaderID);

        foreach (DataRow dr in dt.Rows)
            TextBox tbQuantity = new TextBox();
            tbQuantity.Text = dr["fldLineQuantity"].ToString();
            tbQuantity.ID = "txtDetailLineID" + dr["fldOrderLineID"].ToString();
            tbQuantity.Attributes.Add("data-detaillineid", dr["fldOrderLineID"].ToString());
            tbQuantity.Enabled = false;

            phDetailLines.Controls.Add(new LiteralControl("<p>"));
            phDetailLines.Controls.Add(new LiteralControl("</p>"));

Code to capture values when 'Save' button is pressed:

foreach (Control item in phDetailLines.Controls)
    // capture the values here

When the user edits the quantitiy in the text boxes, they will then click 'Save' button and I want to loop through the text boxes and capture the values, however the loop never gets entered because the controls don't exist. I've double/tripple checked and the ID of the placeholder is correct as "phDetailLines". How do I make the generate the controls in PageInit so that they exist during postback?

Related posts

Recent Viewed