473,238 Members | 1,834 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,238 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 2843
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.