This is a migrated thread and some comments may be shown as answers.

Setting Selected Item on Load

9 Answers 374 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Rod
Top achievements
Rank 1
Rod asked on 12 Dec 2009, 07:10 AM
Hi -

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

Rod

9 Answers, 1 is accepted

Sort by
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 12 Dec 2009, 08:53 PM
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
0
Rod
Top achievements
Rank 1
answered on 13 Dec 2009, 12:53 AM
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
0
Rod
Top achievements
Rank 1
answered on 13 Dec 2009, 12:57 AM
Steve -

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

Rod
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 13 Dec 2009, 01:33 AM
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

0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 13 Dec 2009, 02:02 AM
* Deleted
0
Accepted
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 13 Dec 2009, 02:25 AM
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


0
Rod
Top achievements
Rank 1
answered on 13 Dec 2009, 06:02 AM
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
0
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
answered on 15 Dec 2009, 04:13 AM
No problem, would you mind clicking Mark as Answer? :)
0
hart
Top achievements
Rank 1
Veteran
answered on 31 Dec 2016, 11:31 PM

7 years later and this answer still works!!

Happy New Year!

Tags
ListView
Asked by
Rod
Top achievements
Rank 1
Answers by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Rod
Top achievements
Rank 1
hart
Top achievements
Rank 1
Veteran
Share this question
or