I am using MS VS 2010, and working on an ASP.NET C# website. I am stuck on something that I think may be quite simple, maybe not though.
Lets say I have a drop down list.
DropDownList ddl = new DropDownList();
ddl.ID = "d355";
dynamicPanel.Controls.Add(ddl);
ListItem lstItem1 = new ListItem();
lstItem1.Text = "1";
ListItem lstItem2 = new ListItem();
lstItem2.Text = "2";
ddl.Items.Add(lstItem1);
ddl.Items.Add(lstItem2);
ddl.SelectedIndexChanged += new EventHandler(this.ddl_SelectedIndexChanged);
Since we programatically created our drop down list, we need to also create our custom event handler that we tied to it.
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
// add the selected index to a counter
counter +=((DropDownList)sender).SelectedIndex;
// Now this is where I get stuck, if the current selected index is less
// than the previous selected index, I want to subtract from the counter
}
This is where my problem lies. Please read the comments in the event handler. (Sorry if I have some of the syntax off, this is all free hand at the moment)
I have a feeling that I can get the previous selected index (or item it doesn’t matter) from the event args ((DropDownList)e).?
Please help >.< This doesn’t seem too bad!
I don’t think that there is a built in mechanism, but you could use
ViewStateor aHiddenFieldto keep the previous index.Something like the following: