Setting Selected Item on Load

9 posts, 1 answers
  1. Rod
    Rod avatar
    18 posts
    Member since:
    Apr 2008

    Posted 12 Dec 2009 Link to this post

    Hi -

    I have a RadListView bound to a datasource.  How can I have the first item automatically selected on page load?

    Rod

  2. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 12 Dec 2009 Link to this post

    What about if you handle the DataBound event of the control

     
            if (radListView1.Items.Count > 0) 
                radListView1.Items[0].Selected = true


    http://www.telerik.com/help/aspnet-ajax/listview-selecting-items.html

    Steve
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rod
    Rod avatar
    18 posts
    Member since:
    Apr 2008

    Posted 12 Dec 2009 Link to this post

    I tried using the following:

    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
      <script type="text/javascript">  
        var listView = null;  
        function optionSelect() {  
          var listview = $find("<%= lvOptions.ClientID %>");  
          listview.fireCommand("Select", 0);  
        }  
      </script>  
    </telerik:RadCodeBlock> 

    ...along with...

    <telerik:RadListView ID="lvOptions" runat="server"   
          ItemPlaceholderID="pnlOptionsPlaceholder" DataKeyNames="OptionID"   
          DataSourceID="dsOptions">  
          <ClientSettings> 
            <ClientEvents OnListViewCreated="optionSelect" /> 
          </ClientSettings> 
    ... 

    And it threw the page into an endless loop (although it did correctly select the first item in my RadListView each time!).

    Any ideas?  Does the "fireCommand("Select", itemIndex)" method have to force a reload of the listview, or can I prevent that?

    Thanks,

    Rod
  5. Rod
    Rod avatar
    18 posts
    Member since:
    Apr 2008

    Posted 12 Dec 2009 Link to this post

    Steve -

    Tried that; no joy.  Have you been able to make it work?

    Rod
  6. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 12 Dec 2009 Link to this post

    Unless I'm misreading the docs that should be working, but no I havent been able to get it to work.  It appears that the client-side select causes a postback so setting selecting it on a clientside pageload event will cause it to postback everytime :o

  7. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 12 Dec 2009 Link to this post

    * Deleted
  8. Answer
    Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 12 Dec 2009 Link to this post

    Ok, this works

     
        protected void listView_ItemDataBound(object sender, RadListViewItemEventArgs e) { 
            RadListViewDataItem item = e.Item as RadListViewDataItem; 
             
            if(item.DisplayIndex == 0) 
                item.Selected = true
        } 
     
        protected void listView_PreRender(object sender, EventArgs e) { 
            listView.Rebind(); 
        } 

    Must be becasue of some funky lifecycle deal


  9. Rod
    Rod avatar
    18 posts
    Member since:
    Apr 2008

    Posted 13 Dec 2009 Link to this post

    Steve -

    Funky is the word...but it works.  I had to wrap both in "Not IsPostBack" qualifiers to prevent them from firing each time I clicked a new selection:

        Protected Sub lvOptions_ItemDataBound(ByVal sender As ObjectByVal e As Telerik.Web.UI.RadListViewItemEventArgs) Handles lvOptions.ItemDataBound  
     
            If Not IsPostBack Then 
                Dim itmRow As RadListViewDataItem = e.Item  
                If itmRow.DisplayIndex = 0 Then 
                    itmRow.Selected = True 
                End If 
            End If 
     
        End Sub 
     
     
        Protected Sub lvOptions_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles lvOptions.PreRender  
     
            If Not IsPostBack Then 
                lvOptions.Rebind()  
            End If 
     
        End Sub 
     

    I wouldn't have thought of that - thanks for your help.

    Rod
  10. Steve
    Steve avatar
    1851 posts
    Member since:
    Dec 2008

    Posted 14 Dec 2009 Link to this post

    No problem, would you mind clicking Mark as Answer? :)
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017