ViewState and posted back data

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?
 
S

Sundararajan

Please check whether the control ids remain the same when u post back the
data. u can do this by downloading viewstate decoders and analysing ur view
state.
because i was facing a similar problem days before which was solved after
analysing the viewstate information by viewstate decoders
regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top