Datalist SelectedItemTemplate problem

  • Thread starter Kim Søvsø Pedersen
  • Start date
K

Kim Søvsø Pedersen

Hello

I have a small problem with a DataList control and SelectedItemTemplate. It
works fine except that I have to click twice on an item to select it. Or if
I click an item, this item is only selected when I click another item.

What do I do wrong?

<asp:datalist id="DataList1" runat="server" onitemcommand="folder_ItemCmd">
<itemtemplate>
<asp:linkbutton ID="linkbutton1" Runat="server"
CommandName="select">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</asp:linkbutton>
</itemtemplate>

<SelectedItemTemplate>
<asp:linkbutton ID="Linkbutton2" Runat="server"
CommandName="select">
<b><%# DataBinder.Eval(Container.DataItem, "Name") %></b>
</asp:linkbutton>
</SelectedItemTemplate>
</asp:datalist>

The sub folderItemCmd contains only this:
Datalist1.SelectedIndex = e.Item.ItemIndex

Greetings
 
P

Phillip Williams

The reason this happens is that you have not done a databind on the datalist
after you changed the SelectedIndex.

Try:
1) in the page load to not bind the datalist upon every postback, instead
add a condition such as if (!Page.IsPostBack ) DataList_Databind();

2) Create a separate method where you bind the DataList, e.g.
private void DataList_Databind()
{
DataList1.DataSource = dt; //where dt is your datatable
DataList1.DataBind ();
}

3) whenever you change the datalist.selectedIndex rebind the datalist:
rotected void folder_ItemCmd(object source, DataListCommandEventArgs e)
{
DataList1.SelectedIndex = e.Item.ItemIndex;
DataList_Databind();
}
 
K

Kim Søvsø Pedersen

Phillip Williams said:
The reason this happens is that you have not done a databind on the
datalist
after you changed the SelectedIndex.

Thank you, that solved my problem :)

- Kim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top