Dynamic Control

B

Bishoy George

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);
}

}
}
 
R

Riki

Bishoy George said:
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.
 
B

Bishoy George

Riki said:
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.

How to do that? Could you give me a sample code?
Thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top