Solved

asp.net placeholder from db

Posted on 2014-03-17
7
28 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
  • 2
  • 2
7 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
Thank but hoping to get coding sample from my example.
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Hey, Have you tried the above solution ?
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now