Change FormView.ChangeMode to Edit after Insert

J

J055

Hi

I'd like to do the following with the FormView/ObjectDataSource but not sure
of the best approach.

1. Change the FormView mode to Edit if a valid QueryString ID exists or
Insert mode if it doesn't
2. If in Insert mode then when the form is posted it should get the new ID
from the inserted record and change the FormView Mode to Edit
3. The SelectParameters ID must be updated too as it needs to get the new
record.

My InsertMethod business object returns the new ID.

Many thanks
Andrew
 
W

Walter Wang [MSFT]

Hi Andrew,

1. You can do this by checking the QueryString when the page is first
loaded (non-postback):

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sid = Request.QueryString["ID"];
if (sid != null)
{
FormView1.DefaultMode = FormViewMode.Edit;
}
else
{
FormView1.DefaultMode = FormViewMode.Insert;
}
}
}

2. You can handle this in the Inserted event of ObjectDataSource and do a
redirect to current page with ID appended to the QueryString:

protected void ObjectDataSource1_Inserted(object sender,
ObjectDataSourceStatusEventArgs e)
{
int id = (int)e.ReturnValue;
Response.Redirect(Request.Url.ToString() + "?ID=" + id.ToString(),
true);
}

3. You can always provide the SelectPrarameters ID in the Selecting event
of ObjectDataSource:

protected void ObjectDataSource1_Selecting(object sender,
ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["id"] = Request.QueryString["ID"];
}

Hope this helps. Please feel free to post here if anything is unclear.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top