Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net placeholder from db

Posted on 2014-03-17
7
Medium Priority
?
77 Views
Last Modified: 2016-06-20
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.
0
Comment
Question by:CoolDev2014
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39937617
I would personally add an asp:panel as a placeholder with nothing in it.

This shows the general setup:
http://forums.asp.net/t/1807044.aspx?Find+dynamically+added+controls+to+Panel+in+ASP+NET+using+C+

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.
0
 

Author Comment

by:CoolDev2014
ID: 39937690
Thank but hoping to get coding sample from my example.
0
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 39937737
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.
0
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 2000 total points
ID: 39937931
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();
        tableRow.Cells.Add(textboxCell);
        textboxCell.Controls.Add(textBox);
        tblRecipients.Rows.Add(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());
        CreateDynamicControls(count);
        base.LoadViewState(viewState[1]);
    }
    else
    {
        base.LoadViewState(savedState);
    }
}

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();
Panel1.Controls.Add(NewtextBox);
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 39938160
Hey, Have you tried the above solution ?
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question