Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 12th, 2011, 02:07 PM
Registered User
 
Join Date: Apr 2011
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic TextBox

I have created dynamic textbox now i want to insert data from created textbox, i am googling but didnt get solution that can work for me.

Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
foreach (DataRow datarow in ds.Tables[0].Rows)
{

TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox txt= new TextBox();

txt.ID = "txt_" + datarow["subject_name"].ToString() + "_examdate";
TextBox TextBox1 = new TextBox();
txt.ID = "txt_" + datarow["subject_name"].ToString() + "_starttime";

cell.Controls.Add(txt);



row.Cells.Add(cell);



table.Rows.Add(row);


}

}

protected void Button1_Click(object sender, EventArgs e)
{
IterateControls(this);

}


public void IterateControls(Control parent)
{
TextBox txt_Mathematics_starttime = (TextBox)this.Page.FindControl("txt_Mathematics_st arttime");

txt_examtype.Text = txt_Mathematics_starttime.Text;
}


any help would be appreciated
 
Old August 19th, 2011, 09:49 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The problem with this approach is the way that web forms deals with the controls in the page structure. When you create new controls and add them to the page, you must do so reliably on every page cycle. ASP.NET constructs the page control tree from what it finds explicitly defined in the markup and from whatever is added programmatically up to the point the code in question is running. So the controls you put onto a page during one page cycle will not be there automatically on the next cycle unless you explicitly put them there, so the code that looks for the controls won't find anything.

The way this is typically handled is that you put your control generation code in the page load such that it always runs (i.e. no branching for IsPostback). That way, you reconstruct the control structure before event handling takes place. That being said, you may still run into problems with proper restoration of view state which I think is restored prior to page load (it's been a while since I dealt with web forms). This could have adverse effects regarding event firing when control change events are analyzed. This happens because dynamically created controls will have one value when created by code (e.g. a blank text box) but not have their state restored to preserve the value from the last page draw, then a "new" value gets written to the control from the form post action causing an event to fire when it shouldn't.

Working with dynamically generated controls in web forms is very tricky.
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic TextBox Name from Form Janie Excel VBA 1 March 24th, 2011 09:16 PM
dynamic textbox creation badamsreekar ASP.NET 2.0 Professional 1 July 26th, 2007 10:40 AM
Dynamic Textbox viprsh81 ASP.NET 2.0 Professional 2 July 20th, 2006 04:46 AM
How to assign dynamic value to textbox in struts JasmineStanly JSP Basics 0 August 22nd, 2005 03:56 AM
Dynamic TextBox stu9820 ASP.NET 1.0 and 1.1 Basics 2 February 4th, 2004 04:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.