On 3 Apr, 14:42, "andy" <a...@lycos.co.ukwrote:
I have several forms with formviewa on them.
When a new record is inserted the users want to see the readonly
version of that record.
Default behaviour is to show the first record in the collection.
So if there are 5 entries and the user adds a 6th they want to see the
6th rather than the 1st after it's added.
Changing the criteria for the collection they see is not an option.
My quick and dirty solution is to sort on descending id so the most
recent record is first.
Which is fine until 2 people insert records and one gets to see the
"wrong" one.
A better method looks to be
I can get the record id back out of the stored procedure.
I can see you can navigate to a given formview page.
My problem is working out which page corresponds to the id I get back
out the insert.
Any ideas?
Now had a little more time to think about it...
All the solutions I could find online to this are no good to me since
I don't want to change the datasource to just the one record the user
inserts.
I just iterate through the pages until I find the one corresponds to
the record just added.
It will very likely be the first one since I sort on decreasing id.
In my business layer I retrieve the id using a return parameter out my
insert stored procedure and stick it in session.
Once I find the record I whip this back out of session so the user can
page etc.
protected void fvw_DataBound(object sender, EventArgs e)
{
FormView fvw = (FormView)(sender);
if (fvw.CurrentMode == FormViewMode.ReadOnly)
{
if (Session["Inserted_Id"] != null)
{
string ID = Convert.ToString( Session["Inserted_Id"]);
Session.Remove("Inserted_Id");
for (int i = 0; i < fvw.PageCount; i++)
{
if (fvw.DataKey[0].ToString() == ID)
{
// gotcha !
break;
}
else
{
fvw.PageIndex++;
Trace.Warn("Next page: " +
fvw.DataKey[0].ToString());
}
}
}
}
PS
The closest example I found doesn't work since there's just the one
datakey of the single page selected for formview.
I would guess that there's a datakeys of all of the entries for a
gridview so a slightly different technique possible there.