locked
mobile:ObjectList use like DataList RRS feed

  • Question

  • User-221787929 posted

    I want to know if it is possible to access items when the data gets bound to the ObjectList control like you do with a datalist control. Want to make the link based on certain criteria of the data. Cannot figure out how to get access to the link (lnkTest) in the OnItemDataBind method.

    <mobile:ObjectList ID="objCheckingAccounts" OnItemDataBind="Test_OnDataBind" Runat="server">

    <DeviceSpecific ID="dspChkAcct" Runat="server">

    <Choice>

    <ItemTemplate>

    <mobile:Link ID="lnkTest" Runat="server" />

    <%# ((ObjectListItem)Container)["Description"]%>: <%# ((ObjectListItem)Container)["ProductId"]%>

    </ItemTemplate>

    <SeparatorTemplate><br /><br /></SeparatorTemplate>

    </Choice>

    </DeviceSpecific>

    </mobile:ObjectList>

    Friday, July 20, 2007 10:16 PM

Answers

  • User2032526919 posted

    This was nice learning experience for me too. You can't access the items in ItemDataBind since contents of the template hasn't been instantiated yet. Therefore you must defer accessing till PreRender event. For example my code-behind for your sample ObjectList, where I bind to a generic List

     

     

    <mobile:ObjectList ID="objCheckingAccounts" Runat="server" OnPreRender="objCheckingAccounts_PreRender">
     
    protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                List<int> ints = new List<int>();
                ints.Add(1);
                ints.Add(2);
                ints.Add(3);
                ints.Add(4);
    
                objCheckingAccounts.DataSource = ints;
                objCheckingAccounts.DataBind(); 
            }
        }
    
        
        
    
        protected void objCheckingAccounts_PreRender(object sender, EventArgs e)
        {
            foreach (ObjectListItem item in objCheckingAccounts.Items)
            {
                Link lnk = (Link)item.FindControl("lnkTest");
                if (item.DataItem != null)
                {
                    lnk.Text = ((int)item.DataItem).ToString();
                }
            }
        }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2007 1:39 PM

All replies

  • User2032526919 posted

    Doesn't it provide ObjectListDataBindEventArgs as second argument.

    void Test_OnDataBind(object sender, ObjectListDataBindEventArgs e)
    {
    }

    So shouldn't something like

    Link lnk=(Link)e.ListItem.FindControl("lnkTest");

    work?

    Saturday, July 21, 2007 12:34 PM
  • User-221787929 posted

    I was thinking the same thing you were but it did not work for me. Unless I typed something in wrong I could not get it to work.

    Monday, July 23, 2007 9:12 AM
  • User2032526919 posted

    This was nice learning experience for me too. You can't access the items in ItemDataBind since contents of the template hasn't been instantiated yet. Therefore you must defer accessing till PreRender event. For example my code-behind for your sample ObjectList, where I bind to a generic List

     

     

    <mobile:ObjectList ID="objCheckingAccounts" Runat="server" OnPreRender="objCheckingAccounts_PreRender">
     
    protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                List<int> ints = new List<int>();
                ints.Add(1);
                ints.Add(2);
                ints.Add(3);
                ints.Add(4);
    
                objCheckingAccounts.DataSource = ints;
                objCheckingAccounts.DataBind(); 
            }
        }
    
        
        
    
        protected void objCheckingAccounts_PreRender(object sender, EventArgs e)
        {
            foreach (ObjectListItem item in objCheckingAccounts.Items)
            {
                Link lnk = (Link)item.FindControl("lnkTest");
                if (item.DataItem != null)
                {
                    lnk.Text = ((int)item.DataItem).ToString();
                }
            }
        }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2007 1:39 PM
  • User-538203642 posted

    Hi,

       I am doing this similar kind of work and used the same things as discussed in this forum but I couldnt able to get it working. My emulator is returning "NullReferenceException" Could you please telll me where I am wrong.

     

    Here is my code:

      protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            doxtop.WideDocs widedocs = new doxtop.WideDocs();    

            XmlNode results = null;

            results = widedocs.AdvancedSearch(10, 1, "content", "False", "AnyWord", "Topic", "TitleAndContent", "", "", "", "No");       
           
            rptPublications.DataSource = results.ChildNodes;
            rptPublications.DataBind();        
        }

        protected void rptChecking_PreRender(object sender, EventArgs e)
        {
            foreach (ObjectListItem item in rptPublications.Items)
            {
                XmlNode node = (XmlNode)item.DataItem;

                if (node != null)
                {

                    Link aViewTopic = (Link)item.FindControl("aViewTopic");
                    string topicHeading = node.SelectSingleNode("//TopicHeading").InnerText;
                    string topicID = node.SelectSingleNode("//TopicID").InnerText;
              

                    string topicLink = "/topic.aspx?tid=" + topicID;
                    aViewTopic.NavigateUrl = topicLink;
                    aViewTopic.Text = topicHeading;
                }
            }
        }

     

    my aspx page is like this:

     

     <mobile:ObjectList id="rptPublications" Runat="server" OnPreRender="rptChecking_PreRender">          
                     <mobile:Link ID="aViewTopic" Runat=server NavigateUrl=""/>                
        </mobile:ObjectList> 



     

    Monday, November 5, 2007 8:08 AM
  • User-221787929 posted

    I think your code needs to be like the following.

      

    <mobile:ObjectList id="rptPublications" Runat="server" OnPreRender="rptChecking_PreRender">           
    <DeviceSpecific ID="dspChkAcct" Runat="server">
    
    <Choice>
    
    <ItemTemplate>
    
    <mobile:Link ID="aViewTopic" Runat=server NavigateUrl=""/>  </ItemTemplate>
    
    </Choice>
    
    </DeviceSpecific>                               
     </mobile:ObjectList>  
    
     
    Monday, November 5, 2007 8:53 AM
  • User-538203642 posted

     Hi

      thank you for your reply. Its working fine but I want to the links to generated for each item, Its creating links to one topic several times. Is it problem of retrieving items from data base or why. Do you have any idea.

     

    Thanks

    Aruna.G 


     

    Monday, November 5, 2007 10:00 AM
  • User-221787929 posted

    I would think it is either a problem of retrieving the items from the database or you are selecting the data from the xmlnode in a different way that the data is formatted. The easiest thing to do is put a break point at the line where you set the datasource and look to see what the data actual is comming from the database. This should show you what is going on.

    Monday, November 5, 2007 10:26 AM
  • User-538203642 posted

    Hi,

      I am trying add HeaderTemplate for my form and I addedin ObjectList control and my header templates contains two labels which will get text dynamically which displays total number of records. When I try to create Label controls(which are in header templates) Its giving me Object Null Reference exception. Is it because I am using the "PreRender" event or why. Could you please tell me. Here is my code for Header templates and code behind file.

     my .aspx page


    <mobile:Form Runat=server ID="form" Paginate=True> 
     <mobile:Label ID="error" Runat=server Font-Size=Normal Text="" ForeColor=Red Visible=False></mobile:Label>    
        <mobile:ObjectList ID="rptPublications" Runat="server" OnPreRender="rptChecking_PreRender">        
           <DeviceSpecific ID="dspChkAcct" Runat="server">       
                <Choice>
                  <HeaderTemplate>
                     <mobile:Label ID="hitResults" Runat=server Font-Bold=True Font-Size=Normal Text=""></mobile:Label>                
                   <%--  <mobile:SelectionList ID="page" Runat=server BreakAfter=True>
                          <Item Value="" Text=""/>
                     </mobile:SelectionList>--%>
                     <mobile:Label ID="pageIndex" Runat=server Font-Size=Small Text=""></mobile:Label>                
                  </HeaderTemplate>
                    <ItemTemplate>                    
                         <mobile:Link ID="aViewTopic" Runat="server" Font-Size=Small NavigateUrl="" BreakAfter=True/>
                         <mobile:Label ID="tpcDetails1" Runat=server Font-Size=Small BreakAfter=True></mobile:Label>
                         <mobile:Label ID="tpcDetails2" Runat=server Font-Size=Small BreakAfter=True></mobile:Label>
                    </ItemTemplate>
                    <SeparatorTemplate><br /></SeparatorTemplate>
                </Choice>      
            </DeviceSpecific>    
        </mobile:ObjectList>                    
    </mobile:Form>

     

    my code behind file 

     protected void rptChecking_PreRender(object sender, EventArgs e)
        {       
             ((System.Web.UI.MobileControls.Label)rptPublications.Controls[0].FindControl("hitResults")).Text = "Search results for - " + searchKey + " (" + rptPublications.Items.Count + ")";
         ((System.Web.UI.MobileControls.Label)rptPublications.Controls[1].FindControl("pageIndex")).Text = "Showing Page" + currentPage + " of " + pageCount + ", Total Records: " + rptPublications.Items.Count;

                   
        foreach (ObjectListItem item in rptPublications.Items)
        {
            XmlNode node = (XmlNode)item.DataItem;
            if (node != null)
            {
                Label pubTitle = (Label)item.FindControl("pubTitle");
                Link aViewTopic = (Link)item.FindControl("aViewTopic");
                Label tpcDeatils1 = (Label)item.FindControl("tpcDetails1");
                Label tpcDeatils2 = (Label)item.FindControl("tpcDetails2");

                string pubId = node.Attributes["PubId"].Value;
                string title = node.Attributes["Title"].Value;
                string topicHeading = node.SelectNodes("//TopicHeading").Item(item.Index).InnerText;
                string topicID = node.SelectNodes("//TopicID").Item(item.Index).InnerText;
                string sequence = node.SelectNodes("//Sequence").Item(item.Index).InnerText;
                string topicType = node.SelectNodes("//TopicType").Item(item.Index).InnerText;
                bool isJournalAbout = Convert.ToBoolean(node.SelectNodes("//IsJournalAbout").Item(item.Index).InnerText);
                bool isJournalWelcome = Convert.ToBoolean(node.SelectNodes("//IsJournalWelcome").Item(item.Index).InnerText);
                string journalID = node.SelectNodes("//JournalId").Item(item.Index).InnerText;
                string author = node.SelectNodes("//Author").Item(item.Index).InnerText;
                string views = node.SelectNodes("//NoOfViews").Item(item.Index).InnerText;
                string comment = node.SelectNodes("//NoOfComments").Item(item.Index).InnerText;
                string mDate = node.SelectNodes("//ModifiedDate").Item(item.Index).InnerText;
                   
                string topicLink = "/HNMobileApp/topic.aspx?tid=" + topicID;
                aViewTopic.Text = topicHeading;
                aViewTopic.NavigateUrl = topicLink;
                tpcDeatils1.Text = "Comments: " + comment + " | " + "Views: " + views;
                tpcDeatils2.Text = "By: " + author + " on " + mDate;
            }
        }
      }

     

     

    Thanks,

    Aruna

     

    Tuesday, November 13, 2007 5:24 AM