T
Tumurbaatar S.
I'm adding a template column to a DataGrid dynamically:
private void CreateColumn()
{
TemplateColumn col = new TemplateColumn();
...//set some col properties
col.EditItemTemplate = new MyTemplate();
MyGrid.Columns.Add(col);
}
The MyTemplate class implementes InstantiateIn() method as:
public void InstantiateIn(Control container)
{
TableCell cell = container as TableCell;
TextBox txt = new TextBox();
txt.ID = "MyTextBox";
container.Controls.Add(txt);
RequiredFieldValidator req = new RequiredFieldValidator();
req.ID = "MyReqVal";
req.ControlToValidate = txt.ID;
container.Controls.Add(req);
}
Inside DataGrid.ItemDataBound event handler I set initial value
of the above MyTextBox as:
private void MyGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
(e.Item.Cells[N].Controls[0] as TextBox).Text = SomeValue;
}
}
And I call CreateColumn() from Page_Load() on the first visit
and from overrided LoadViewState() of the Page on every postback:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
CreateColumns();
}
And this works well. The MyGrid switches to edit mode normally and
renders the MyTextBox with a bounded value. The MyReqVal validates
MyTextBox properly at a client side.
But when I postback the page to save a text entered in MyTextBox,
the Page.IsValid is always FALSE. Also, MyTextBox is always EMPTY:
private void SaveBtn_Click(object sender, System.EventArgs e)
{
//at this point the IsValid is false and MyTextBox.Text is blank
if (IsValid)
{
...// cannot reach here!
}
}
What am I doing wrong?
private void CreateColumn()
{
TemplateColumn col = new TemplateColumn();
...//set some col properties
col.EditItemTemplate = new MyTemplate();
MyGrid.Columns.Add(col);
}
The MyTemplate class implementes InstantiateIn() method as:
public void InstantiateIn(Control container)
{
TableCell cell = container as TableCell;
TextBox txt = new TextBox();
txt.ID = "MyTextBox";
container.Controls.Add(txt);
RequiredFieldValidator req = new RequiredFieldValidator();
req.ID = "MyReqVal";
req.ControlToValidate = txt.ID;
container.Controls.Add(req);
}
Inside DataGrid.ItemDataBound event handler I set initial value
of the above MyTextBox as:
private void MyGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
(e.Item.Cells[N].Controls[0] as TextBox).Text = SomeValue;
}
}
And I call CreateColumn() from Page_Load() on the first visit
and from overrided LoadViewState() of the Page on every postback:
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
CreateColumns();
}
And this works well. The MyGrid switches to edit mode normally and
renders the MyTextBox with a bounded value. The MyReqVal validates
MyTextBox properly at a client side.
But when I postback the page to save a text entered in MyTextBox,
the Page.IsValid is always FALSE. Also, MyTextBox is always EMPTY:
private void SaveBtn_Click(object sender, System.EventArgs e)
{
//at this point the IsValid is false and MyTextBox.Text is blank
if (IsValid)
{
...// cannot reach here!
}
}
What am I doing wrong?