Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

DataList (ASP.NET 1.1)

  • 05-07-2006 12:25PM
    #1
    Closed Accounts Posts: 4,943 ✭✭✭


    How can i easily and safely DEselect an item in a DataList? At the moment i have link buttons in both the Item template and SelectedItem template which fire a "select" command. Then using the OnItemCommand event, i set SelectedIndex = -1 if the currently select row is the same as the row that fired the event. However this doesn't work.

    I'd appreciate if anyone could fire up a small project and paste up working code which lets you select/deselect items in a DataList. It's driving me bananas!


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Specifically, this doesn't work:
    		private void dlistTransfer_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
    		{
    			if(e.CommandName == "select")
    			{
    				if(this.dlistTransfer.SelectedIndex == e.Item.ItemIndex)
    					this.dlistTransfer.SelectedIndex = -1;
    				else
    					this.dlistTransfer.SelectedIndex = e.Item.ItemIndex;
    
    				this.BindTransfer();
    			}
    		}
    

    Setting the SelectedIndex = e.Item.ItemIndex works fine. After postbacks, the correct index is stored and everything is highlighted correctly. But if i try setting to -1 (to deselect all items) on postback the previous value for SelectedIndex is there, and things are highlighted incorrectly. Is this not the right way to "unselect" things?


  • Closed Accounts Posts: 14 GiantCranes


    Please post the full page code and I will give it a try here


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    I figured it out a few hours after i posted it up. It came to me in a flash :p The "bug" was that i used the commandname "select" for my custom selecting code. What was happening is that i was picking up the "select" command in my code, but ASP.NET was then picking it up itself on the "selected index changed" event. This resulted in me trying to change the index to (for example) 5, but then as soon as my method was complete, ASP.NET would fire the SelectedIndexChanged event and change the index on me.

    The code works fine if i either change my commandname to "custom select" or just move my code into the "selectedindexchanged" handler. Twas a stupid bug :p


Advertisement