Click to See Complete Forum and Search --> : unable to retain the viewstate


vikrant3mahajan
July 9th, 2009, 08:11 AM
guys i a m trying to build a webpage using c# as language in asp.net....

page consist of a button and i want is that when i click on that button a new textbox is added to the page dynamically on runtime..
My program works fine it adds a Textbox to the page ..but i want is that when i click again the button after clicking once i am able to add another text box but retaining the previous one...
but when i do that my previous one is lost

can any one please help me in this............


this is the code back file

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{


public bool IsControlAdded
{
get
{
if (ViewState["IsControlAdded"] == null)
ViewState["IsControlAdded"] = false;



return (bool)ViewState["IsControlAdded"];



}

set
{

ViewState["IsControlAdded"] = value;

}

}



protected void Page_Load(object sender, EventArgs e)

{

if(IsControlAdded)

AddUserControl();

}


















protected void btnLoad_Click(object sender, EventArgs e)
{
if (!IsControlAdded) AddUserControl();
}
protected void btnSubmit_Click(object sender, EventArgs e)
{

}
TextBox t; static int x=0;
private void AddUserControl()
{
x =x+ 50;

t = new TextBox();
t.ID = Convert.ToString(x);
t.Style.Add("position", "Relative");
t.Style.Add("runat", "server");
t.Style.Add("left", "" + x + "px");
t.Style.Add("top", "" + x + "px");



Page.Form.Controls.Add(t);
IsControlAdded = true;

}
}

Rohit Kukreti
July 18th, 2009, 12:09 PM
Try this code.....
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
static int x = 0;
protected int NumberOfControls
{
get { return (int)ViewState["NumControls"]; }
set { ViewState["NumControls"] = value; }
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
//Initiate the counter of dynamically added controls

this.NumberOfControls = 0;
else
//Controls must be repeatedly be created on postback

this.createControls();
}

// This routine creates the controls and assigns a generic ID

private void createControls()
{
int count = this.NumberOfControls;

for (int i = 0; i < count; i++)
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + i.ToString();
//Add the Controls to the container of your choice
tx.Style.Add("position", "Relative");
tx.Style.Add("runat", "server");
tx.Style.Add("left", "" + (i+1)* 50 + "px");
tx.Style.Add("top", "" + (i + 1) * 50 +"px");
Page.Form.Controls.Add(tx);
}
}

// example of dynamic addition of controls

// note the use of the ViewState variable

private void addSomeControl()
{
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
tx.Style.Add("position", "Relative");
tx.Style.Add("runat", "server");
tx.Style.Add("left", "" + x + "px");
tx.Style.Add("top", "" + x + "px");
Page.Form.Controls.Add(tx);
this.NumberOfControls++;
}

protected void btnLoad_Click(object sender, EventArgs e)
{
x = x + 50;
TextBox tx = new TextBox();
tx.ID = "ControlID_" + NumberOfControls.ToString();
tx.Style.Add("position", "Relative");
tx.Style.Add("runat", "server");
tx.Style.Add("left", "" + x + "px");
tx.Style.Add("top", "" + x + "px");
Page.Form.Controls.Add(tx);
this.NumberOfControls++;
}

}