473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help accessing dynamically added textbox controls

My code dynamically adds template fields to a GridView control. Everything
seems to work OK, except when updating, because I haven't found a way to
reference the dynamically added textboxes. FindControl doesn't work, and the
textboxes cannot be accessed using
grid.Rows[e.RowIndex].Cells[x].Control[x]. Can someone help?

Thanks,

Keith
May 7 '06 #1
3 2846
Hi Keith,

There's certainly a way to do what you want.

It would help if you showed the code that's causing problems so we could
analyze it and probably fix it.

Ken
Microsoft MVP [ASP.NET]

"keithb" <kb******@dslextreme.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
My code dynamically adds template fields to a GridView control. Everything
seems to work OK, except when updating, because I haven't found a way to
reference the dynamically added textboxes. FindControl doesn't work, and
the textboxes cannot be accessed using
grid.Rows[e.RowIndex].Cells[x].Control[x]. Can someone help?

Thanks,

Keith

May 7 '06 #2
Keith,

You need to re-create dynamically added controls on every postback. Then you
should be able to find them.

Eliyahu

"keithb" <kb******@dslextreme.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
My code dynamically adds template fields to a GridView control. Everything
seems to work OK, except when updating, because I haven't found a way to
reference the dynamically added textboxes. FindControl doesn't work, and
the textboxes cannot be accessed using
grid.Rows[e.RowIndex].Cells[x].Control[x]. Can someone help?

Thanks,

Keith

May 7 '06 #3
Here is the code. See my comments below in grid_RowUpdating. Thanks, Keith

protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ComID = grid.DataKeys[e.RowIndex].Value.ToString();
/*

This is where I am having the problem

grid.Rows[e.RowIndex].Cells.count is 1, even though there are 14
columns.
the referenced column is the command field for edit. There is no
reference to any other columns.
It appears that the NewValues property is not being
automatically populated with the name/value pairs of the revised non-key
fields prior to raising the grid-rowUpdating event.
*/

BuildColumnsDynamically();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
InitializeComponent();
}
}

private void InitializeComponent()
{
this.Init += new System.EventHandler(this._Default_Init);
this.Load += new System.EventHandler(this.Page_Load);
CommandField cf = new CommandField();
cf.ShowEditButton = true;
grid.Columns.Add(cf);
BuildColumnsDynamically();
}
private void BuildColumnsDynamically()
{
DataTable dt = ComponentsAccess.GetEnvComMatrix();

int colcount = 0;
foreach (DataColumn col in dt.Columns)
{
if (colcount > 1)
{
//Declare the bound field
TemplateField chkColumn = new TemplateField();
chkColumn.HeaderTemplate = new
GridViewTemplate(ListItemType.Header, col.ColumnName);
chkColumn.ItemTemplate = new
GridViewTemplate(ListItemType.Item, col.ColumnName);
chkColumn.EditItemTemplate = new
GridViewTemplate(ListItemType.EditItem, col.ColumnName);
//Add the newly created bound field to the GridView.
grid.Columns.Add(chkColumn);
}
colcount++;
}

//Initialize the DataSource
grid.DataSource = dt;
//Bind the datatable with the GridView.
grid.DataBind();

}


public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
ListItemType templateType;
string columnName;
public GridViewTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}
public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
LinkButton lb = new LinkButton();
CheckBox ckh = new CheckBox();
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
Label lb1 = new Label();

switch (templateType)
{
case ListItemType.Header:
lc.Text = columnName;
container.Controls.Add(lc);
break;

case ListItemType.Item:
lb1.DataBinding += new EventHandler(lb_DataBinding);
lb1.Text = columnName;
lb1.Width = 5;
container.Controls.Add(lb1);
break;

case ListItemType.EditItem:

tb2.DataBinding += new EventHandler(tb_DataBinding);
tb2.Text = columnName;
tb2.Columns = 5;
container.Controls.Add(tb2);
break;
}
}

void tb_DataBinding(object sender, EventArgs e)
{

TextBox txtdata = (TextBox)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, columnName);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}

}

void lb_DataBinding(object sender, EventArgs e)
{

Label txtdata = (Label)sender;
GridViewRow container = (GridViewRow)txtdata.NamingContainer;
object dataValue = DataBinder.Eval(container.DataItem, columnName);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}

}
"Ken Cox - Microsoft MVP" <BA**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Hi Keith,

There's certainly a way to do what you want.

It would help if you showed the code that's causing problems so we could
analyze it and probably fix it.

Ken
Microsoft MVP [ASP.NET]

"keithb" <kb******@dslextreme.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
My code dynamically adds template fields to a GridView control.
Everything seems to work OK, except when updating, because I haven't
found a way to reference the dynamically added textboxes. FindControl
doesn't work, and the textboxes cannot be accessed using
grid.Rows[e.RowIndex].Cells[x].Control[x]. Can someone help?

Thanks,

Keith


May 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
4
by: Bas Groeneveld | last post by:
I am developing an ASP.NET application part of which consists of a data entry wizard defined by entries in a data table - ie the controls on each page of the wizard are determined by definitions in...
10
by: Bharat | last post by:
Hi Folks, Suppose I have two link button on a page (say lnkBtn1 and lnkBtn2). On the click event of the lnkbtn1 I have to add a dynamically created control. And On the click event of the lnkBtn2 I...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
3
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new...
0
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event,...
7
by: =?Utf-8?B?Li46OiBLZXZpbiA6Oi4u?= | last post by:
I have a problem with accessing controls that I have loaded dynamically and added to a web page. The scenario: I have a webpage that displays multiple instances of a user control on the page. ...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.