Dynamic Control

Discussion in 'ASP .Net Building Controls' started by Bishoy George, Jan 24, 2006.

  1. Hi,
    I made a page with a button , when I click that button ---> a new TextBox
    object is displayed.

    I made the following code but the bug is:
    Every time I click the button ---> a new TextBox appear but the previous one
    DISAPPEARS!!!!

    Could you fix it for me, please?

    //.aspx page
    //-----------------------------------------------------------------
    <%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false"
    Inherits="DynamicControls._Default" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>Default</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:Button id="btnGetTexBox" style="Z-INDEX: 101; LEFT: 16px; POSITION:
    absolute; TOP: 16px"
    runat="server" Text="Get TextBox!"></asp:Button></form>
    </body>
    </HTML>
    //----------------------------------------------------------------------
    // .aspx.cs page
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace DynamicControls
    {
    /// <summary>
    /// Summary description for _Default.
    /// </summary>
    public class _Default : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnGetTexBox;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!Page.IsPostBack)
    {
    Session["turn"] = -1;
    }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.btnGetTexBox.Click += new
    System.EventHandler(this.btnGetTexBox_Click);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnGetTexBox_Click(object sender, System.EventArgs e)
    {
    Session["turn"] = (int)Session["turn"] + 1;
    int turn = (int)Session["turn"];

    TextBox tb = new TextBox();
    tb.ID = "txtData" + Session["turn"].ToString();
    tb.TextMode = TextBoxMode.MultiLine;
    // Height="104px" Width="544px
    tb.Height = Unit.Pixel(100);
    tb.Width = Unit.Pixel(500);

    // Style = "Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 16px"
    int zIndex = 102 + (int)Session["turn"];

    tb.Style.Add("Z-INDEX",zIndex.ToString());
    tb.Style.Add("LEFT","16px");
    tb.Style.Add("POSITION","absolute");

    int top = 50 + (turn * 100);

    tb.Style.Add("TOP",top.ToString() + "px");

    this.Controls[1].Controls.Add(tb);
    }

    }
    }
     
    Bishoy George, Jan 24, 2006
    #1
    1. Advertising

  2. Bishoy George

    Riki Guest

    "Bishoy George" <> wrote in message
    news:%...
    > Hi,
    > I made a page with a button , when I click that button ---> a new TextBox
    > object is displayed.
    >
    > I made the following code but the bug is:
    > Every time I click the button ---> a new TextBox appear but the previous
    > one DISAPPEARS!!!!


    Controls that are created dynamically don't recreate themselves on postback.

    You should keep track yourself of the number of TextBoxes that have been
    added already,
    and recreate them all on postback. You could use ViewState to keep an
    integer variable for that.

    --

    Riki
     
    Riki, Jan 25, 2006
    #2
    1. Advertising

  3. "Riki" <> wrote in message
    news:%...
    > "Bishoy George" <> wrote in message
    > news:%...
    >> Hi,
    >> I made a page with a button , when I click that button ---> a new TextBox
    >> object is displayed.
    >>
    >> I made the following code but the bug is:
    >> Every time I click the button ---> a new TextBox appear but the previous
    >> one DISAPPEARS!!!!

    >
    > Controls that are created dynamically don't recreate themselves on
    > postback.
    >
    > You should keep track yourself of the number of TextBoxes that have been
    > added already,
    > and recreate them all on postback. You could use ViewState to keep an
    > integer variable for that.
    >
    > --
    >
    > Riki
    >


    How to do that? Could you give me a sample code?
    Thanks.
     
    Bishoy George, Jan 26, 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. Chris Thunell
    Replies:
    3
    Views:
    3,431
    Edward
    Jul 21, 2004
  2. Replies:
    5
    Views:
    5,590
  3. =?Utf-8?B?Y2luZHk=?=
    Replies:
    2
    Views:
    2,591
    Steven Cheng[MSFT]
    Jun 8, 2005
  4. Frankie
    Replies:
    2
    Views:
    6,000
    Frankie
    Jun 23, 2005
  5. Larry Bud
    Replies:
    1
    Views:
    351
    bruce barker
    Jan 10, 2007
Loading...

Share This Page