Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A tough question regarding dynamic controls

Status
Not open for further replies.

ghesse

Programmer
Jun 16, 2003
51
US
Hello,

Here's a simple version of what I have:

protected PlaceHolder myph;

public int MyProperty
{
get
{
if( ViewState["myprop"] == null )
ViewState["myprop"] = 1;
return Int32.Parse(ViewState["myprop"].ToString());
}
set { ViewState["myprop"] = value; }
}

override protected void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
for(int i=1;i<=MyProperty;i++)
AddControl(i);
}

private void Page_Load( Object source, System.EventArgs e )
{
if( !IsPostBack)
{
AddControl(MyProperty);
}
}

protected void AddControl(int ID)
{
TextBox mytxt = new TextBox();
Button mybtn = new Button();

mytxt.ID = "textbox" + ID.ToString();
myph.Controls.Add(mytxt);

mybtn.ID = "button" + ID.ToString();
mybtn.Text = "And";
mybtn.Click+=new System.EventHandler(this.btnClick);
myph.Controls.Add(mybtn);
}

protected void btnClick(object source, EventArgs e)
{
MyProperty++;
}

Ok so as you can see what I'm doing is just creating text boxes and buttons dynamically. What I want to do is when the user clicks the button another textbox appears on the webform with a button allowing them to do it again.

What's happening here is that on the first click of the and button nothing happens on the postback. On the second click though things start to work right. How can I make it so on the first postback another textbox and button appear.

Good Luck,
--ghesse
 
ghesse - this phenomenom (2d click syndrom) has surface on several occassions here over the last several months -- I can't recall off hand the solution - make sure you do a pretty good search here at Tek-Tips -- jfrost et al contributors have addressed this I am sure.
 
Duh!

Figured this one out myself. Ok, just add
AddControl(MyProperty);
below the
MyProperty++;
In btnClick!

You don't need to track the state of a new control until the next postback.

--ghesse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top