Just Dean
Mr Collins is my Dad

Formview default mode

January 19, 2008 02:07 by Dean

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: , , , ,


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments

August 28. 2008 01:25

Gravatar

I'm using asp.net 2.0 w/ C# and am trying to do the same thing.
When I try your idea, the FormView never changes mode - in fact the "else" never executes.

I've taken out my ItemTemplate and tried other events (like OnLoad) but it still doesn't work. Did you do anything else to your FormView to get it to work?

Mark

August 28. 2008 01:32

Gravatar

Figured it out... I had to do this in the SqlDataSource Selected event.

if (e.AffectedRows < 1)
{
FormView1.ChangeMode(FormViewMode.Insert);
}
else
{
FormView1.ChangeMode(FormViewMode.Edit);
}

Mark

August 28. 2008 01:42

Gravatar

Glad you figured your way around it, I'll have to have a look at my solution as I only tested it in the context of my application.

Thanks for the input.

Dean

Dean

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

January 7. 2009 00:24

Gravatar