T
Tumurbaatar S.
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).
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).