Re: Dynamic controls based on user action

Discussion in 'ASP .Net' started by Eliyahu Goldin, Dec 13, 2006.

  1. Nadav,

    Although there are scenarios when it is appropriate to create controls
    dynamically, it fits better into object-oriented paradigm to use normal
    static controls and to operate their properties to hide/show them as needed.
    Controls with the Visible property set to 'false' don't get rendered to the
    client, which is the same effect as not creating them in the first place.

    If you do decide on creating controls dynamically, there is nothing
    particularly wrong with doing it in the button event handlers. You will need
    to re-create these controls on every postback where you need to access
    their properties.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "Nadav Popplewell" <> wrote in message
    news:...
    > Hi everybody,
    >
    > I've got a question about adding dynamic controls to a asp.net net.
    >
    > As I understand it, dynamic controls need to be added in the page_init
    > handler,
    > otherwise there are problems with the controls view state and event
    > handling.
    >
    > What do I do if the dynamic controls that need to be added depend on the
    > user interaction with the web page?
    >
    > For example,
    > If the user pressed button A I want to add Control X.
    > if the user pressed button B I want to remove Control X and add Control Y.
    > etc...
    >
    > Do I need to update the application's state and the repost (or do a
    > Transfer) from the buttons' event handlers and then read the application's
    > state in the page_init and add the appropriate controls?
    >
    > Thanks,
    > Nadav
    Eliyahu Goldin, Dec 13, 2006
    #1
    1. Advertising

  2. Nadav,

    Instead of going into this balagan with dynamic controls, why can't you
    simply have both A and B on the page, one next to another, and make only one
    of them visible? Several times I implemented tab control functionality with
    this technique.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "Nadav Popplewell" <> wrote in message
    news:...
    > Hi Eliyahu,
    >
    > I've just did some testing and there are some problems with creating the
    > controls
    > and the buttons handlers.
    >
    > The problem is that if I created control A and now in the postback I want
    > to
    > display control B INSTEAD of control A, I need to remove control A and add
    > Control B.
    > Now, what happens is that if controls A & B are placed inside a control
    > (placeholder or panel) after removing A and adding B the controlID that B
    > will have is NOT the same as the controlID that B will have when it is
    > created in the Page_Init().
    >
    > So what happens is, that after control B is created, it does not respond
    > to
    > events in the first postback (I assume it will also not save it's
    > viewstate).
    >
    > Basiclly, what I want is a Tab Control functionallity with some way of
    > testing in page load of the contained controls if they need to perform
    > DataBinding or not
    > (and of course, if you change tabs then the new tab needs to perform
    > databinding).
    >
    > I'm trying to implement this using MultiView, but so far I have some
    > problems.
    >
    > Thanks,
    >
    > Nadav
    >
    Eliyahu Goldin, Dec 13, 2006
    #2
    1. Advertising

  3. Hello Nadav,

    For your scenario, you want to dyanmically present some controls on the
    webform according to some user actions(like button click). Based on my
    experience, there are two means to do this:

    1. Put all the possible controls statically on the webform at design-time,
    and hidden all the controls by default. At runtime, show the certain
    control according to the user's actions(in postback events).

    2. You can still programmtically create those dynamic controls. However,
    since yoru dynamic controls are depend on some user interactive operations,
    you need to add some variables to persist the current status (according to
    user's action). for example, you can store the status in SessionState so
    that you can check the value in Page_init event and create the proper
    dynamic control accordingly. e.g.

    ========aspx=================
    .............
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn1" runat="server" Text="Show TextBox1"
    OnClick="btn1_Click" />
    <asp:Button ID="btn2" runat="server" Text="Show TextBox2"
    OnClick="btn2_Click" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit Button"
    OnClick="btnSubmit_Click" /><br />
    <br />
    <asp:placeHolder ID="PlaceHolder1" runat="server"></asp:placeHolder>

    </div>
    </form>
    </body>


    ======code behind========
    public partial class Part1_dynamiccontrol : System.Web.UI.Page
    {
    TextBox _txt1;
    TextBox _txt2;

    protected void Page_Init(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    Session["controlid"] = 1;
    }

    CreateDynamicControls();
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void CreateDynamicControls()
    {
    PlaceHolder1.Controls.Clear();


    int cid = (int)Session["controlid"];



    if (cid == 1)
    {
    _txt1 = new TextBox();
    _txt1.ID = "txt1";
    PlaceHolder1.Controls.Add(_txt1);

    }
    else
    {
    _txt2 = new TextBox();
    _txt2.ID = "txt2";
    PlaceHolder1.Controls.Add(_txt2);

    }



    }

    protected void btn1_Click(object sender, EventArgs e)
    {
    Session["controlid"] = 1;
    CreateDynamicControls();


    }
    protected void btn2_Click(object sender, EventArgs e)
    {
    Session["controlid"] = 2;
    CreateDynamicControls();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
    if (_txt1 != null)
    {
    Response.Write("<br/>txt1" + _txt1.Text);
    }

    if (_txt2 != null)
    {
    Response.Write("<br/>txt2" + _txt2.Text);
    }
    }
    }
    =============================

    Hope this helps.


    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Dec 14, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald
    Replies:
    2
    Views:
    837
    Robbe Morris [C# MVP]
    Jan 17, 2006
  2. Joe Bloggs
    Replies:
    1
    Views:
    747
    Sudsy
    Aug 3, 2003
  3. rjweytens
    Replies:
    6
    Views:
    16,083
    rjweytens
    Jun 25, 2004
  4. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,820
    runescience
    Feb 7, 2006
  5. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    460
    Eidolon
    May 4, 2004
Loading...

Share This Page