Formview default mode

If your using a formview on a page it might be useful to set the default mode dynamically depending on whether the datasource returns a record or not.

For example I'm using a formview to populate a table - if a record already exists for my criteria I want to edit it, otherwise I want to add a new one.

I could test the datasource directly but I thought it would be neater to actually test the formview itself after it's databound. i.e.

protected void FormView1_DataBinding(object sender, EventArgs e)
    {
        if (FormView1.DataItemCount < 1)
        {
            FormView1.DefaultMode = FormViewMode.Insert;
        }
        else
        {
            FormView1.DefaultMode = FormViewMode.Edit;
        }
    }

Works a treat - I guess there could be lots of ways to achieve this, if anybody has any let me know. But it works for me :0)

Ta Dean

Blogged with Flock

Tags: , , , ,


Posted by: Dean
Posted on: 1/22/2008 at 8:07 PM
Tags: , , , , ,
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (3) | Post RSSRSS comment feed