Back button in ItemDetailsTemplate in OjbectList problem

Last post 11-13-2009 1:19 PM by moxol. 2 replies.

Sort Posts:

  • Back button in ItemDetailsTemplate in OjbectList problem

    11-11-2009, 7:33 PM
    • Member
      8 point Member
    • moxol
    • Member since 06-24-2009, 10:45 AM
    • Posts 97

    I have ObjectList that has custom pagination.

    ObjectList data source is ObjectDataSource with LinqToSql.

    When next or previous page is clicked I do data bind in LoadItems event.

    ObjectList has a default Choice with ItemTemplate and ItemDetailsTemplate.

    I have put Details command control in ItemTemplate that switch to details view mode and when it's clicked Details mode is shown correct.
    In ItemDetailsTemplate I have put Back command which switch to list view mode but there is a problem. Back command always return ObjectList's first page instead of page that was before Details command clicked.  And not only that, but is also doing data binding again, which I think it  shouldn't be done. And I have noticed that first page is actually ok, when Back button is clicked it doesn't do data binding, but all other pages except first are not ok, they are doing data bind again which is not needed since when next or previous page is clicked load items did data binding.

    Can someone help me?

     

  • Re: Back button in ItemDetailsTemplate in OjbectList problem

    11-12-2009, 8:40 PM
    • Member
      8 point Member
    • moxol
    • Member since 06-24-2009, 10:45 AM
    • Posts 97

    OK, this is really weird problem, I am not sure if this is a bug, but let me share what I have discovered with debugging and tracing problem.

     In  objectList_ItemCommand event I have this:

    void objectList_ItemCommand(object sender, ObjectListCommandEventArgs e)
    {
      if (e.CommandName == "Details")
            {
                objectList.SelectedIndex = e.ListItem.Index;
                objectList.ViewMode = ObjectListViewMode.Details;
            }
            
            if (e.CommandName == "Back")
            {
                objectList.ViewMode = ObjectListViewMode.List;
            }
    }

    Now, I have discovered this.
    When I press Details to look specific item in object list, and that item is not on first page, then after objectList_ItemCommand event is finished, object list items are cleared and object list LoadItems is going into again, which shouldn't happened because items exist in  objectList_ItemCommand event!
    This doesn't happens when object list is in first page. Then object list items are not cleared after objectList_ItemCommand and isn't going into LoadItems.

    Can anyone help me about this, I am really not sure how to solve this, maybe it's a bug.

  • Re: Back button in ItemDetailsTemplate in OjbectList problem

    11-13-2009, 1:19 PM
    Answer
    • Member
      8 point Member
    • moxol
    • Member since 06-24-2009, 10:45 AM
    • Posts 97

    I solved it!

    I was looking for solution on web but didn't find it, so I will post it because it's a usual problem that is  encountered.

    Problem with ItemTemplate is, when you put Command control in it for details to show, then it will do postback and rebind items that are already binded when page was loaded.

    Solution is to use Link command instead of Command.
    Now, there are two ways that can be used, one using JavaScript, other to use anchor element.
    I will show here anchor element because it's present on (X)HTML and WML so I think it will work on every mobile browser.
    Solution is similar in the way that ASP.NET framework auto-generates.

    Step by step:

    1) Create in App_Code folder .cs file with this code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.MobileControls;
    using System.Web.UI;
    
    namespace MyNamespace
    {
        public class DetailsLink : MobileControl
        {
      
            public DetailsLink()
            {
            }
    
            protected override void Render(HtmlTextWriter writer)
            {
                writer.Write("<a href=\"objectlist_test.aspx?__VIEWSTATE=" + Page.Server.UrlEncode(MobilePage.ClientViewState) +
                    "&__ET=myObjectList&__EA=" +
                    ((ObjectListItem)this.NamingContainer).Index.ToString()
                    + "&__ufps=" +
                    "\">" + "Show Details" + "</a>");
    
            }
        }
    
        public class DetailsBackLink : MobileControl
        {
    
            public DetailsBackLink()
            {
            }
    
            protected override void Render(HtmlTextWriter writer)
            {
                writer.Write("<a href=\"objectlist_test.aspx?__VIEWSTATE=" + Page.Server.UrlEncode(MobilePage.ClientViewState) +
                    "&__ET=myObjectList&__EA=" +
                    "__back"
                    + "&__ufps=" +
                    "\">" + "Back" + "</a>");
    
            }
        }
    }
    
    

    2) Register controls on page (or in web.config) so they can be used.
    3) Put in ObjectList ItemTemplate DetailsLink control, and in ItemDetailsTemplate DetailsBackLink control.
    4) In ObjectList.PreRender event put this code:

     void myObjectList_PreRender(object sender, EventArgs e)
     {
    
            if (Request.Params.Get("__ET") == "myObjectList")
            {
                if (Request.Params.Get("__EA") == "__back")
                {
                    myObjectList.ViewMode = ObjectListViewMode.List;
                }
                else
                {
                    myObjectList.SelectedIndex = Int32.Parse(Request.Params.Get("__EA"));
                    myObjectList.ViewMode = ObjectListViewMode.Details;
                }
            }
    
    }


    That's it.
    Solution is basic one, it can be extended, but basic code will be the same.

Page 1 of 1 (3 items)