ViewState and posted back data

Discussion in 'ASP .Net Building Controls' started by Tumurbaatar S., Nov 27, 2005.

  1. 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?
    Tumurbaatar S., Nov 27, 2005
    #1
    1. Advertising

  2. Tumurbaatar S.

    Sundararajan Guest

    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
    --
    S.Sundararajan


    "Tumurbaatar S." wrote:

    > 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?
    >
    >
    >
    Sundararajan, Nov 29, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. damonf
    Replies:
    2
    Views:
    2,434
    damonf
    Nov 8, 2003
  2. David Stahl
    Replies:
    2
    Views:
    443
    David Stahl
    Apr 28, 2004
  3. Simon
    Replies:
    1
    Views:
    433
    Henri
    Nov 9, 2004
  4. rgouge
    Replies:
    0
    Views:
    337
    rgouge
    Jul 29, 2005
  5. Simon
    Replies:
    1
    Views:
    183
    Henri
    Nov 9, 2004
Loading...

Share This Page