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

Set SelectedItem if no value is selected

5 Answers 111 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Angie
Top achievements
Rank 1
Angie asked on 09 Jun 2010, 08:29 PM
I would like to know at what stage in the ListView lifecycle to set the Selected item so that it takes on the <SelectedItemTemplate> styling.

I tried doing it at the Prerender and I tried at ItemDataBound, but neither changed the itemstyle properly.

Thank you.


This is what I had in the ItemDataBound:

 

if (lvGalleries.SelectedValue == null && item.DisplayIndex==0)  
 
item.Selected = true;  
 

 

 

5 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 11 Jun 2010, 10:48 AM
Hello Angie,

There are several ways to se item as selected. In order to set selected items on initial load, you can use page's load event and modify the SelectedIndexes collection.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        RadListView1.SelectedIndexes.Add(0);
    }
}

If you want to set it after postback, you may use RadListView's PreRender event, however a rebind of the control will be required.

Best wishes,
Rosen
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
yoomee kwon
Top achievements
Rank 1
answered on 15 Apr 2011, 07:30 PM
I set the selected to be true and add into selectedindexes but the background color doesn't change which means selecteditemtemplate is not being picked up.  Any idea?

 

protected override void OnPreRender(EventArgs e)

 

{

lvOffer.Items[0].Selected =

true;

 

 

lvOffer.SelectedIndexes.Add(0);

}

0
Veli
Telerik team
answered on 21 Apr 2011, 07:21 AM
As Rosen stated, if you are using PreRender, you need to rebind RadListView. Using either one of your two statements will suffice.

Veli
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Thorsten
Top achievements
Rank 2
answered on 14 Jul 2011, 10:39 AM


Hi there,

I am evaluating the Telerik components at the moment and I am bit confused that it is that much of a problem of just selecting an item when loading a page (a common scenario I think).

Ok, here is my problem:

I have a RadListView, bound to an EntityDataSource. 
What I do in the Page_Load-Event is:

if (!IsPostBack) {
..
            EntityDataSource1.DataBind();
            listViewBox1.DataBind();             // without this, I had nothing 
//in listViewBox1 during the Page_Load-event
...

listViewBox1.SelectedIndexes.Add(i);
AND/OR
listViewBox1.Items[0].Selected = true;
}

The items gets selected, when I click on the page an a postback occurs.

....

OK, I got it now. After the select I did a listViewBox1.Rebind() - is that the correct way to do this?

Greetings,
Thorsten
0
Veli
Telerik team
answered on 14 Jul 2011, 11:24 AM
Hi Thorsten,

If you add the index of the selected item in RadListView.SelectedIndexes before databinding, you should get the selected item display right after rebind when the page loads. Are you getting this?

Veli
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
ListView
Asked by
Angie
Top achievements
Rank 1
Answers by
Rosen
Telerik team
yoomee kwon
Top achievements
Rank 1
Veli
Telerik team
Thorsten
Top achievements
Rank 2
Share this question
or