Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Dynamic columns and NamingContainer

Reply
Thread Tools

Dynamic columns and NamingContainer

 
 
Tumurbaatar S.
Guest
Posts: n/a
 
      11-27-2005
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 Programmatically".
The columns are template based and all they use a same template
producer. In the edit mode (ListItemType.EditItem) the template
adds textbox and validator controls to a column as:

public void InstantiateIn(System.Web.UI.Control container)
{
TextBox tb = new TextBox();
tb.Text = "";
tb.ID = "MyTextBox";
container.Controls.Add(tb);
CompareValidator val = new CompareValidator();
val.ControlToValidate(tb.ID);
container.Controls.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 ControlToValidate 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).







 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      11-27-2005
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.Controls.Add(tb);
RequiredFieldValidator val = new RequiredFieldValidator ();
container.Controls.Add(val);
container.DataBinding += new EventHandler(ItemContainer_DataBinding);

2- Add another method like this:
void ItemContainer_DataBinding(object sender, EventArgs e)
{
TableCell td= (TableCell)sender; //gets the container
//gets the naming container that you wanted
DataGridItem dgItem = ((DataGridItem )td.NamingContainer );
//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
RequiredFieldValidator val = td.Controls[1] as RequiredFieldValidator;
//set the validator properties
val.ControlToValidate = tb.ID;
val.ErrorMessage = "Cannot leave blank";
//add a value to databind to the TextBox
tb.Text = ((DataRowView)dgItem.DataItem)[0].ToString ();
}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"Tumurbaatar 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 Programmatically".
> The columns are template based and all they use a same template
> producer. In the edit mode (ListItemType.EditItem) the template
> adds textbox and validator controls to a column as:
>
> public void InstantiateIn(System.Web.UI.Control container)
> {
> TextBox tb = new TextBox();
> tb.Text = "";
> tb.ID = "MyTextBox";
> container.Controls.Add(tb);
> CompareValidator val = new CompareValidator();
> val.ControlToValidate(tb.ID);
> container.Controls.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 ControlToValidate 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).
>
>
>
>
>
>
>
>

 
Reply With Quote
 
 
 
 
Tumurbaatar S.
Guest
Posts: n/a
 
      11-27-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.Controls.Add(tb);
> RequiredFieldValidator val = new RequiredFieldValidator ();
> container.Controls.Add(val);
> container.DataBinding += new EventHandler(ItemContainer_DataBinding);
>
> 2- Add another method like this:
> void ItemContainer_DataBinding(object sender, EventArgs e)
> {
> TableCell td= (TableCell)sender; //gets the container
> //gets the naming container that you wanted
> DataGridItem dgItem = ((DataGridItem )td.NamingContainer );
> //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
> RequiredFieldValidator val = td.Controls[1] as RequiredFieldValidator;
> //set the validator properties
> val.ControlToValidate = tb.ID;
> val.ErrorMessage = "Cannot leave blank";
> //add a value to databind to the TextBox
> tb.Text = ((DataRowView)dgItem.DataItem)[0].ToString ();
> }
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "Tumurbaatar 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 Programmatically".
>> The columns are template based and all they use a same template
>> producer. In the edit mode (ListItemType.EditItem) the template
>> adds textbox and validator controls to a column as:
>>
>> public void InstantiateIn(System.Web.UI.Control container)
>> {
>> TextBox tb = new TextBox();
>> tb.Text = "";
>> tb.ID = "MyTextBox";
>> container.Controls.Add(tb);
>> CompareValidator val = new CompareValidator();
>> val.ControlToValidate(tb.ID);
>> container.Controls.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 ControlToValidate 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).
>>
>>
>>
>>
>>
>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FindControl() method and NamingContainer Dan ASP .Net 1 04-04-2007 08:42 PM
NamingContainer.ID not being set on UserControl issue Rob Blij ASP .Net 0 05-25-2006 03:31 PM
NamingContainer property on UserControl Rob Blij ASP .Net 0 05-25-2006 08:34 AM
Dynamic columns and NamingContainer Tumurbaatar S. ASP .Net 2 11-27-2005 01:54 PM
NamingContainer and UserControl msmail.microsoft.com ASP .Net 8 10-13-2004 03:15 AM



Advertisments