Problem with FormView inside UpdatePanel

Today I struggled for a while with a FormView that for no apparent reason wasn’t updating some of my fields to the database. After sometime I noticed that the fields that were not getting inserted into the database where the ones I had wrapped inside an UpdatePanel. Coincidence? I don’t think so…

I did a few tests and noticed that that was the problem indeed. I can’t say exactly what is the problem but it seems like that the FormView doesn’t like to have it’s fields inside an UpdatePanel.

The way I found to get around this issue is to populate the parameters manually in the events of the ObjectDataSource (ODS). So if you’re trying to insert a record you might using the Inserting event of the ODS to populate the problematic paramters:

protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        e.InputParameters["FirstName"] = ((TextBox)FormView1.FindContro("TextBoxFirstName")).Text;
        e.InputParameters["LastName"] = ((TextBox)FormView1.FindContro("TextBoxLastName")).Text;
        e.InputParameters["City"] = ((TextBox)FormView1.FindContro("TextBoxCity")).Text;
    }

In the Inserting event of the ODS the InputParamters have already been populated (at least the ones outside the UpdatePanel) but the record hasn’t been inserted yet, so you are intercepting the parameters, adjusting it’s values and then letting it continue with the insertion.

I hope this code spares someone to have to go through the tests I had to do.

Comments

Posted by: mtugnoli
On: 6/29/2009 3:44:59 AM

I a formview bounded with LinqDataSource and a UpdatePanel in EditItemTemplate and update OK with :

protected void MyFormview_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
e.NewValues.Add("GruDes", GruDesTextBox.Text);
}

But when Update in Insert Mode this error occour :

FormView control must be in edit mode to update a record

Could you help me ?

Marco
Posted by: gabriel
On: 6/30/2009 8:23:21 AM

Hi Marco. Sorry but I'm not sure I understand your problem. You are trying update a record while your FormView is still in InsertMode? That is what I get from the error you posted.

When you are in InsertMode the ItemUpdating event shouldn't be hit. The proper event would be ItemInserting.

The asp.net site has some nice data access tutorials, you may want to check out the FormView Tutorial(http://www.asp.net/learn/data-access/tutorial-14-cs.aspx).

Also, if you're not sure you are having problems with the UpdatePanel, the preferred approach is to remove the UpdatePanel and complete your code without using ajax. Once you get it working like this you may plug the ajax controls in. In this way you will make sure where your problem is really happening.

I hope this helps!

Gabriel.

Leave your comment

Author

Email (never displayed)

Website

Comment  
HTML is NOT allowed. Use regular line breaks and those will be respected.