placeholder from db

i have table like

ID PlaceHolder    Desc
1   TextBox           VIN
2   Radio Button   Yes/No

In the frontend, I can query but I don't know how to dynamically create user control.
Like if it is textbox, it will create a textbox. I am thinking using Place Holder but I don't know how to implement that.

Let assume we already done the frontend, I need to find a way to capture data and save into the database. I can handle the SQL part but I don't know how to capture the data in a FOR loop.

Thanks, and see any experts can help.
Who is Participating?
Manoj PatilConnect With a Mentor Sr. Software EngineerCommented:
Hey read this before working with dynamic controls.
Sometimes you may require to create controls dynamically in ASP.NET. Because of the ASP.NET page life cycle, you may loss the created controls on next post back (ASP.NET framework recreate the page on postback). You can resolve this issue by overriding LoadViewState() and SaveViewState() methods.
LoadViewState() method restores view-state information from a previous page request that was saved by the SaveViewState method. This method will not executed on the first time.
SaveViewState() method saves any server control view-state changes that have occurred since the time the page was posted back to the server. So in SaveViewState() method, store the values you wish to create. And in the LoadViewState() method, retrieve from the viewstate, which returned from SaveViewState() method.

Here is a sample implementation, in this text boxes are created based on the drop down list selection. In the SaveViewState() method, the value of the drop down list stored to the view state, and in the LoadViewState(), controls are recreated using the viewstate information.

private void CreateDynamicControls(int count)
    for (int i = 0; i < count; i++)
        var textBox = new TextBox();
        var textboxCell = new TableCell();
        var tableRow = new TableRow();

protected override object SaveViewState()
    var viewState = new object[2];
    //Saving the dropdownlist value to the View State
    viewState[0] = int.Parse(ddlRecipients.SelectedValue); ;
    viewState[1] = base.SaveViewState();
    return viewState;

protected override void LoadViewState(object savedState)
    //Getting the dropdown list value from view state.
    if (savedState is object[] && ((object[])savedState).Length == 2)
        var viewState = (object[])savedState;
        var count = int.Parse(viewState[0].ToString());

protected void cmdSubmit_Click(object sender, EventArgs e)
    //To get the textbox value, you can loop throw
    //the table cells and read the textbox controls
    foreach (TableRow row in tblRecipients.Rows)
        var textbox = row.Cells[0].Controls[0] as TextBox;

Open in new window

And finally you can easily add the newly created control in ASP:PANEL

Panel Panel1= new Panel();
Kyle AbrahamsSenior .Net DeveloperCommented:
I would personally add an asp:panel as a placeholder with nothing in it.

This shows the general setup:

You can add each control (rather than just a label) based on the type.

Note you may want to add a parentControl to each control.
For instance:
  Your Radiobutton should be a radiobuttonlist  with a controlId
  then your radiobuttonlist should have the 2 radiobuttons (yes / no).  This way you can have other controls in there.

To iterate over the controls:

(foreach Control c in Panel1.Controls)
   //note that c. can have it's own controls, you'll need to loop through those as well.

the last piece is tying that to the DB . . . I usually recommend using some other attribute of the control (say tag for instance) to store the field.  You can then build your sql statements from that.

This is high level for now . . . if you need a clearer picture at the details feel free to ask.
CoolDev2014Author Commented:
Thank but hoping to get coding sample from my example.
Kyle AbrahamsSenior .Net DeveloperCommented:
What you're attempting to do is not a trivial thing.  It all depends on your architecture and how you've laid everything out.  Your question was how do you loop over each control in the place holder . . . that's done in the foreach loop.  What you do with it from there can vary on your requirements.

If you have specific questions relating to something in your code, I'd be happy to help.
Manoj PatilSr. Software EngineerCommented:
Hey, Have you tried the above solution ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.