473,804 Members | 3,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic columns and NamingContainer

I dynamically add columns to DataGrid as described in MSDN
articles "Top Questions about the DataGrid Web Server Control"
and "Creating Web Server Control Templates Programmaticall y".
The columns are template based and all they use a same template
producer. In the edit mode (ListItemType.E ditItem) the template
adds textbox and validator controls to a column as:

public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
TextBox tb = new TextBox();
tb.Text = "";
tb.ID = "MyTextBox" ;
container.Contr ols.Add(tb);
CompareValidato r val = new CompareValidato r();
val.ControlToVa lidate(tb.ID);
container.Contr ols.Add(val);
}

During run-time this method throws exception like "... multiple controls
have
same ID: MyTextBox...". Of course, if there was no need to use the
validator,
I probably will not touch ID of the textbox and leave it with a default
value (empty?).
But the validator requires this ID for its ControlToValida te property.
Any workarounds?

P.S. In above method, the "container" parameter is type of TableCell.
But its Parent and NamingContainer properties are undefined (I checked
it during debugging with QuickWatch). Why?
I expected that TableCell's Parent will be DataGridItem and NamingContainer
will be DataGridItem or DataGrid itself. I ask it because I want to
reference
a parent column from above InstantiateIn() method to create IDs based on
a column information (column index or something else).



Nov 27 '05 #1
2 2229
The error you got means that you instantiated more than one column using the
same Template that contained more than one Textbox in the datagriditem with
the same ID. You can solve that by assigning the ID upon databinding:
1- Modify the InstaniateIn method to look like this:

TextBox tb = new TextBox();
container.Contr ols.Add(tb);
RequiredFieldVa lidator val = new RequiredFieldVa lidator ();
container.Contr ols.Add(val);
container.DataB inding += new EventHandler(It emContainer_Dat aBinding);

2- Add another method like this:
void ItemContainer_D ataBinding(obje ct sender, EventArgs e)
{
TableCell td= (TableCell)send er; //gets the container
//gets the naming container that you wanted
DataGridItem dgItem = ((DataGridItem )td.NamingConta iner );
//get the cell # within the tablerow
int cellNo=-0;
foreach (TableCell tc in dgItem.Cells)
{
if (tc == td) break;
cellNo++;
}
//the first control within the cell is the textbox
TextBox tb = td.Controls [0] as TextBox ;
//assign a unique ID for each cell within the same TableRow
tb.ID = "MyTextBox" + cellNo;
//the validator is the second control within the container
RequiredFieldVa lidator val = td.Controls[1] as RequiredFieldVa lidator;
//set the validator properties
val.ControlToVa lidate = tb.ID;
val.ErrorMessag e = "Cannot leave blank";
//add a value to databind to the TextBox
tb.Text = ((DataRowView)d gItem.DataItem)[0].ToString ();
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Tumurbaata r S." wrote:
I dynamically add columns to DataGrid as described in MSDN
articles "Top Questions about the DataGrid Web Server Control"
and "Creating Web Server Control Templates Programmaticall y".
The columns are template based and all they use a same template
producer. In the edit mode (ListItemType.E ditItem) the template
adds textbox and validator controls to a column as:

public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
TextBox tb = new TextBox();
tb.Text = "";
tb.ID = "MyTextBox" ;
container.Contr ols.Add(tb);
CompareValidato r val = new CompareValidato r();
val.ControlToVa lidate(tb.ID);
container.Contr ols.Add(val);
}

During run-time this method throws exception like "... multiple controls
have
same ID: MyTextBox...". Of course, if there was no need to use the
validator,
I probably will not touch ID of the textbox and leave it with a default
value (empty?).
But the validator requires this ID for its ControlToValida te property.
Any workarounds?

P.S. In above method, the "container" parameter is type of TableCell.
But its Parent and NamingContainer properties are undefined (I checked
it during debugging with QuickWatch). Why?
I expected that TableCell's Parent will be DataGridItem and NamingContainer
will be DataGridItem or DataGrid itself. I ask it because I want to
reference
a parent column from above InstantiateIn() method to create IDs based on
a column information (column index or something else).



Nov 27 '05 #2
Thank you!
I also found something like your recommendation.
My DataGrid already uses ItemCreated event so
I put there a code that sets these IDs.
Thank you again!

"Phillip Williams" <Ph************ **@webswapp.com > wrote in message
news:5D******** *************** ***********@mic rosoft.com...
The error you got means that you instantiated more than one column using
the
same Template that contained more than one Textbox in the datagriditem
with
the same ID. You can solve that by assigning the ID upon databinding:
1- Modify the InstaniateIn method to look like this:

TextBox tb = new TextBox();
container.Contr ols.Add(tb);
RequiredFieldVa lidator val = new RequiredFieldVa lidator ();
container.Contr ols.Add(val);
container.DataB inding += new EventHandler(It emContainer_Dat aBinding);

2- Add another method like this:
void ItemContainer_D ataBinding(obje ct sender, EventArgs e)
{
TableCell td= (TableCell)send er; //gets the container
//gets the naming container that you wanted
DataGridItem dgItem = ((DataGridItem )td.NamingConta iner );
//get the cell # within the tablerow
int cellNo=-0;
foreach (TableCell tc in dgItem.Cells)
{
if (tc == td) break;
cellNo++;
}
//the first control within the cell is the textbox
TextBox tb = td.Controls [0] as TextBox ;
//assign a unique ID for each cell within the same TableRow
tb.ID = "MyTextBox" + cellNo;
//the validator is the second control within the container
RequiredFieldVa lidator val = td.Controls[1] as RequiredFieldVa lidator;
//set the validator properties
val.ControlToVa lidate = tb.ID;
val.ErrorMessag e = "Cannot leave blank";
//add a value to databind to the TextBox
tb.Text = ((DataRowView)d gItem.DataItem)[0].ToString ();
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Tumurbaata r S." wrote:
I dynamically add columns to DataGrid as described in MSDN
articles "Top Questions about the DataGrid Web Server Control"
and "Creating Web Server Control Templates Programmaticall y".
The columns are template based and all they use a same template
producer. In the edit mode (ListItemType.E ditItem) the template
adds textbox and validator controls to a column as:

public void InstantiateIn(S ystem.Web.UI.Co ntrol container)
{
TextBox tb = new TextBox();
tb.Text = "";
tb.ID = "MyTextBox" ;
container.Contr ols.Add(tb);
CompareValidato r val = new CompareValidato r();
val.ControlToVa lidate(tb.ID);
container.Contr ols.Add(val);
}

During run-time this method throws exception like "... multiple controls
have
same ID: MyTextBox...". Of course, if there was no need to use the
validator,
I probably will not touch ID of the textbox and leave it with a default
value (empty?).
But the validator requires this ID for its ControlToValida te property.
Any workarounds?

P.S. In above method, the "container" parameter is type of TableCell.
But its Parent and NamingContainer properties are undefined (I checked
it during debugging with QuickWatch). Why?
I expected that TableCell's Parent will be DataGridItem and
NamingContainer
will be DataGridItem or DataGrid itself. I ask it because I want to
reference
a parent column from above InstantiateIn() method to create IDs based on
a column information (column index or something else).



Nov 27 '05 #3

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

Similar topics

1
17684
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
2
4554
by: Nicole | last post by:
I am creating template columns programmatically. I have read the msdn article on this and I'm so close. Article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchcreatingwebservercontroltemplatesprogrammatically.asp I have narrowed down the problem comes in when the _DataBinding handler is called and the literal text is being assigned, but I can't figure out beyond that. The main problem is that...
0
1637
by: Pat Sagaser via .NET 247 | last post by:
I'm using a repeater with a dynamic template. I don't know the fields to display (or how many) until runtime. I have everything working except for linking Button events to the repeaters ItemCommand (see below). I've found plenty of examples for doing it using <ItemTemplate> in the aspx file, but I'm stumped when it comes to doing it dynamically at run time. /////////////////////////// .apsx file: <%@ Page language="c#"...
0
1999
by: Pat Sagaser via .NET 247 | last post by:
I'm trying to add LinkButtons to a Repeater control using adynamic template. The docs state that you should be able tobubble the click event to the containing Repeater. There areplenty of examples in the documentation for doing this using an<ItemTemplate> tag, but I haven't found any indication for howyou would do this in a dynamic template (implementing theITemplate interface). I'm adding the LInkButton in the TemplateDataBinding...
0
1243
by: KT | last post by:
Hi Everybody I'm hoping that someone can help me as I'm totally stumped. I am trying to populate a datalist using the dynamic templates. I have used the example on Microsofts site, I can't get the data to bind using the databinder.eval. Here is my code... on the aspx page
3
13758
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
2
2947
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic sub-report will capture what grades the student has achieved in a list of different subjects and the reason I need it to be dynamic is that students take different subjects. Basically I've been trying to doctor the KB article on dynamic
0
1684
by: KA NMC | last post by:
I have a dataGrid that is populated by SQL table. The Datagrid has two dynamic columns - that I created to for calculations. I want to sort the grid on frmload by one of the dynamic columns is that possible - if so; what will be the best way to do this? Dim command1 As New SqlCommand("Select * FROM LateOrders_query_view order by requested_date desc", cnn1) Dim SqlReader As SqlDataReader cnn1.Open() Dim dt As New...
9
11933
by: lilOlMe | last post by:
Hi there! I have generated a GridView that looks something like: SportName| CompanyNameX |CompanyNameY |CompanyNameZ Hockey.....| Shipping------------ |Accounting-------- |Shipping------------ BaseBall...| Receiving-----------|Shipping------------|Accounting--------- Etc............| Accounting----------|Receiving---------- |Receiving----------- Where there are an unknown number of Company Names and an unknown number of Sport...
0
9587
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.