locked
Redirect to item page after inserting RRS feed

  • Question

  • User-853298616 posted

    After inserting a new item, I would like to redirect to the page of that inserted item, instead of the list page.

    How can I accomplish this?

    Thks in advance

    Wednesday, October 22, 2008 12:40 PM

Answers

  • User1024101778 posted

    I am assuming you are inserting via the insert page as opposed to the list page for this example.

    You have to get the ID of the newly inserted item and redirect to that page.  To get the ID you must add an event handler to the LinqDataSource's Inserted event.  There you can capture the result from the event arguments.  Store that in a private member and use it to redirect in the DetailsView1_ItemInserted method.

     

            private object _insertedRow;

    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
    if (e.Exception == null || e.ExceptionHandled)
    {
    Response.Redirect(table.GetActionPath(PageAction.Details, _insertedRow));
    //Response.Redirect(table.ListActionPath);
    }
    }

    protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
    _insertedRow = e.Result;
    }
      

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 2:03 PM
  • User-330204900 posted

    This seems to work just as well:

    protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        Response.Redirect(table.GetActionPath(PageAction.Details, e.Result));
    }
    

    Hope this is OK [:D]

    Hi Jheyse is there any reason why you shouldn't do this?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 2:50 PM
  • User-330204900 posted

    So this would be OK: 

    protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
    	//_insertedRow = e.Result;
    	if (e.Exception == null || e.ExceptionHandled)
    		Response.Redirect(table.GetActionPath(PageAction.Details, e.Result));
    }
    

    [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 3:47 PM

All replies

  • User1024101778 posted

    I am assuming you are inserting via the insert page as opposed to the list page for this example.

    You have to get the ID of the newly inserted item and redirect to that page.  To get the ID you must add an event handler to the LinqDataSource's Inserted event.  There you can capture the result from the event arguments.  Store that in a private member and use it to redirect in the DetailsView1_ItemInserted method.

     

            private object _insertedRow;

    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
    if (e.Exception == null || e.ExceptionHandled)
    {
    Response.Redirect(table.GetActionPath(PageAction.Details, _insertedRow));
    //Response.Redirect(table.ListActionPath);
    }
    }

    protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
    _insertedRow = e.Result;
    }
      

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 2:03 PM
  • User-330204900 posted

    Thats great jheyse, I was going about this the long way, works greate. [:D]

    Wednesday, October 22, 2008 2:47 PM
  • User-330204900 posted

    This seems to work just as well:

    protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        Response.Redirect(table.GetActionPath(PageAction.Details, e.Result));
    }
    

    Hope this is OK [:D]

    Hi Jheyse is there any reason why you shouldn't do this?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 2:50 PM
  • User1024101778 posted

     Yeah, that would work too as long as you don't have an exception while inserting. :) 

    Wednesday, October 22, 2008 3:29 PM
  • User-330204900 posted

    So this would be OK: 

    protected void DetailsDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
    	//_insertedRow = e.Result;
    	if (e.Exception == null || e.ExceptionHandled)
    		Response.Redirect(table.GetActionPath(PageAction.Details, e.Result));
    }
    

    [:D]

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 3:47 PM
  • User-715420275 posted

    There is no e.Result available for the EntityDataSource but there is a e.Entity available. Is there anyway to accomplish this for the EntityDataSource?

    Thursday, December 24, 2009 1:21 AM
  • User-330204900 posted

    There is no e.Result available for the EntityDataSource but there is a e.Entity available. Is there anyway to accomplish this for the EntityDataSource?

     

    No in EF EntityDataSource it's e.Entity see:

    protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (e.Exception == null || e.ExceptionHandled)
            Response.Redirect(table.GetActionPath(PageAction.Details, e.Entity));
    }

    Hope thsi helps [:D]

    Thursday, December 24, 2009 1:02 PM
  • User1227492907 posted

    This doesn’t work for me, I believe it’s because I'm using stored procedures for INSERT,UPDATE,DELETE.<o:p></o:p>

    I set the return type of my INSERT stored procedure to the Object that was inserted.<o:p></o:p>

    <o:p> </o:p>Do you know how to get this redirect to work when using a stored procedure to INSERT data?<o:p></o:p>

    Friday, May 7, 2010 3:56 PM
  • User-330204900 posted

    Hi badmf_ahhhhhyeah, it should make no difference whether you are using SPROCs or not this is handles well before the Context is called to save the changes. 

    Friday, May 7, 2010 4:22 PM
  • User1227492907 posted

    I figured it out.  You have to create an output parameter in the stored procedure and set it's value to the Primary key of the item just inserted.  Then you map the output parameter in the Linq to SQL O/R to the object. 

    Friday, May 7, 2010 4:37 PM
  • User-715420275 posted

    I use EntityDataSource and .NET Framework 3.5 SP1 in my Dynamic Data WebSite.

    The _insertedRow has the PK value of 0 after insert. I use triggers in Oracle database to generate my PK on the insert. How can I capture this value on the front end so I can redirect the user to the details page with the item that was inserted?

    Thanks

    Tuesday, October 19, 2010 1:19 PM
  • User-330204900 posted

    With EF this is only slightly different:

    protected void DetailsDataSource_Inserted(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (e.Exception == null || e.ExceptionHandled)
        {
            Response.Redirect(table.GetActionPath(PageAction.Details, e.Entity));
        }
    }

    Hope that helps [:)]

    Tuesday, October 19, 2010 4:35 PM
  • User-647007878 posted

    Hi, Stephen. I am using EF as data model. I have found that if i use code below in my project in order to redirect to the Details page after Update item that if i fully remove data from my row on Edit.apsx page - the code does not work and as result i get Details.aspx page with the same data(row is not cleared )! But if i add data to existing data in row(i.e. i update row, not delete) then it is works!

    protected void DetailsDataSource_Updated(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (e.Exception == null || e.ExceptionHandled)
        {
            Response.Redirect(table.GetActionPath(PageAction.Details,e.Entity));
        }
    }
    Tuesday, November 22, 2011 7:05 AM
  • User-647007878 posted

    What are your thoughts on about this cause?

    Thursday, November 24, 2011 1:28 AM
  • User-330204900 posted

    Sorry not sure what you are trying to achive?

    Thursday, November 24, 2011 5:01 AM
  • User-647007878 posted

    I try to do redirect to details.apsx page after editing the item. On Edit.aspx i add following code to code behind (Edit.apsx.cs):

    protected void DetailsDataSource_Updated(object sender, EntityDataSourceChangedEventArgs e)
    {
        if (e.Exception == null || e.ExceptionHandled)
        {
            Response.Redirect(table.GetActionPath(PageAction.Details,e.Entity));
        }
    }
    

    When is cleaned one of rows(for example we delete text from textbox) in record of table then after pressing Update button on Edit.aspx page we will see Details.apsx page but the row is not cleared! The other words the table was not updated after press Update! But if  we do not remove the text, but we add the text to existing text then the table will be updated.

    Thursday, November 24, 2011 6:31 AM
  • User-330204900 posted

    Hi konstantin, how are you clearing the text field? can you explain what you are trying to achive rather that how you are trying to achive it please.

    Thursday, November 24, 2011 8:39 AM
  • User-647007878 posted

    I am so sorry. My problem is solved by removing ViewStateMode=Disable from Page directive (on Edit.aspx page). I understood that if we deal with Dynamic Data site we should be careful with view state.

    Thursday, November 24, 2011 9:59 AM
  • User-330204900 posted

    if you turn viewstate off things will break in DD you need to be carefull of which controls you it off on.

    Thursday, November 24, 2011 3:39 PM
  • User-647007878 posted

    ok. thanks.

    Thursday, November 24, 2011 9:07 PM