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

x
?
Solved

Create Unlimited Dynamic Controls

Posted on 2008-06-16
19
Medium Priority
?
611 Views
Last Modified: 2008-06-19
OK - I have a simple form and which will allow the user to enter filter criteria in a text box - what I am trying to do, is to let the user click a button and add a new dropdownlist and text box - the ddl will have AND|OR|=, etc. and the textbox will let them put in the text qualifier, etc.

So every time the user clicks the add new button - a new ddl and txt is added, along with the option to remove "row" of controls...

I am using an updatepanel etc... Any hep pn how this is possible would save my week!!!!
0
Comment
Question by:tbaseflug
[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
  • 11
  • 8
19 Comments
 
LVL 21

Expert Comment

by:naspinski
ID: 21799245
since your new controls are made at runtime dynamically, there is not really an easy way to do this.  You have to continually pass your new controls each postback.  You could set a session variable to a List<yourControl> and check for that List every time your page loads (!IsPostBack) and if it is present, repopulate those controls.  But then you would have to do manual viewstate maintenance.
0
 

Author Comment

by:tbaseflug
ID: 21799250
So something like this wouldn't work?
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox t = new TextBox();
        PlaceHolder p = new PlaceHolder();
        Page.Controls.Add(p);
        p.Controls.Add(t);
    }

Open in new window

0
 

Author Comment

by:tbaseflug
ID: 21799269
Would this address the issue:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

And assuming so, how would I build/add the controls
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 21

Expert Comment

by:naspinski
ID: 21799288
   protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox t = new TextBox();
        PlaceHolder p = new PlaceHolder();
        Page.Controls.Add(p);
        p.Controls.Add(t);
    }

wont work because each postback you call (asynch) will just make a new textbox.  It will not 'know' that you already made one or more.
0
 

Author Comment

by:tbaseflug
ID: 21799309
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21799311
looking at that link, looks a bit convoluted to me... give me a bit here
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21799342
just wrote a quick example... I am actually going to blog about this one, I will post it on here when I am done.  This is a much easier approach (I think).
<!--ASPX-->
<asp:ScriptManager ID="sm" runat="server" />
<asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
        <asp:Button ID="btn" runat="server" Text="Add Control" onclick="btn_Click" />
        <asp:Panel ID="pnl" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>
 
// C#
List<LiteralControl> persistControls = new List<LiteralControl>();
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["persistControls"] != null) // if you already have some controls populated
    {
        persistControls = (List<LiteralControl>)Session["persistControls"]; // pull them out of the session
        foreach (LiteralControl lc in persistControls)
            pnl.Controls.Add(lc); // and push them back into the page
    }
}
protected void btn_Click(object sender, EventArgs e)
{
    LiteralControl lc = new LiteralControl("<div style=\"border:solid 2px navy;padding:3px;margin:3px\">NEW CONTROL</div>");
    pnl.Controls.Add(lc);// add it to your page
    persistControls.Add(lc);// add it to the list
    Session["persistControls"] = persistControls; // put it in the session
}

Open in new window

0
 

Author Comment

by:tbaseflug
ID: 21799366
This is PERFECT!  What about, under my original scenario - how hard would it be to had a button added next to each control that allowed the user to remove that specific control?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21799380
That requires a bit more thought, give me some time here.
0
 
LVL 21

Accepted Solution

by:
naspinski earned 2000 total points
ID: 21799427
a bit more involved, but nothing new here, just a little List manipulation:
<!--aspx-->
<asp:ScriptManager ID="sm" runat="server" />
<asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
        <asp:Button ID="btn" runat="server" Text="Add Control" onclick="btn_Click" />
        <asp:Button ID="btnClear" runat="server" Text="Reset" onclick="btnClear_Click" />
        <asp:Panel ID="pnl" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>
 
//C#
List<LiteralControl> persistControls = new List<LiteralControl>();
Random rand = new Random(); // for display so we can get a simple difference in controls
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["persistControls"] != null) // if you already have some controls populated
    {
        persistControls = (List<LiteralControl>)Session["persistControls"]; // pull them out of the session
        int count = 0;
        foreach (LiteralControl lc in persistControls)
        {
            Button btn = new Button();
            btn.Click += new EventHandler(btnDelete_Click);
            btn.Text = "Delete";
            btn.CommandArgument = count.ToString();
            pnl.Controls.Add(lc); // and push them back into the page
            pnl.Controls.Add(btn);
            pnl.Controls.Add(new LiteralControl("<br />")); // for formatting
            count++;
        }
    }
}
protected void btn_Click(object sender, EventArgs e)
{
    LiteralControl lc = new LiteralControl("<span style=\"border:solid 2px navy;margin:3px\"> NEW CONTROL [ "+ rand.Next(1000,9999).ToString() + "] </span>");
    Button btn = new Button();
    btn.Click += new EventHandler(btnDelete_Click);
    btn.Text = "Delete";
    btn.CommandArgument = persistControls.Count.ToString();
    pnl.Controls.Add(lc); // and push them back into the page
    pnl.Controls.Add(lc);// add it to your page
    persistControls.Add(lc);// add it to the list
    pnl.Controls.Add(btn);
    pnl.Controls.Add(new LiteralControl("<br />")); // for formatting
    Session["persistControls"] = persistControls; // put it in the session
}
protected void btnClear_Click(object sender, EventArgs e)
{
    Session["persistControls"] = null;
    Response.Redirect(Request.Url.ToString());
}
protected void btnDelete_Click(object sender, EventArgs e)
{
    int deleteThisOne = int.Parse(((Button)sender).CommandArgument);
    persistControls.Remove(persistControls[deleteThisOne]);
    Session["persistControls"] = persistControls; // put it in the session
    Response.Redirect(Request.Url.ToString());
}

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21799998
Here is a full explanation.  Thanks for the idea!
http://naspinski.net/post/Adding-Persistent--Controls-is-AspNet.aspx

Let me know if you have any more questions.
0
 

Author Comment

by:tbaseflug
ID: 21802395
This is great and certainly appreciate all of the hard work and effort here!  I dont suppose that these is anyway, which the delete and reset button clicked, to avoid a post-back?  Was hopinh to keep it all async but if not, this will certainly work.
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21802737
I was messing with that, and if you replace both instances of:
Response.Write()

With:
ph.Controls.Clear();
Page_Load(null, null);

You will get somewhat desired results, but the delete buttons don't always work successively. I don't really have time to look into it right now :P
0
 

Author Comment

by:tbaseflug
ID: 21802851
thanks - I was also trying up.Update() with similar issues - in that some deletes you have to hit twice.

Last question I have for you - if I wanted to add a series of additional controls (really just a few textboxes and 1 dropdown, etc.) - do I place the code for them in the literal control or replace the literal with new instances of each of the controls that I want tio add?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21803432
You can just replace the literals.  I just used those as they are simple and can be declared with one line :P
0
 

Author Comment

by:tbaseflug
ID: 21803523
if I wanted to add any type of formatting 9to the layout of the dynamic controls) - could I wrap them in a span/div (within a literal) and only show "add" the one literal/container control?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21803577
yup

What I would do is make the pnl.CssClass = "someclass"; so then you could just manipulate the CSS
0
 

Author Comment

by:tbaseflug
ID: 21803762
so, could I do something like this?

        LiteralControl lc = new LiteralControl("<span style=\"border:solid 1px navy;margin:3px\">  <asp:TextBox ID= rand.Next(1000, 9999).ToString() runat=\"serve\"></asp:TextBox></span>");
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21803848
no, since textbox is an as.net control you would have to declare it and add it seperately
LiteralControl lc = new LiteralControl("<span style=\"border:solid 1px navy;margin:3px\">");
TextBox txt = new TextBox();
txt.ID = "someID";
 
//then add them both:
ph.Controls.Add(lc);
ph.Controls.Add(txt);

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

618 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