How to get the previous item on DropDownList before OnSelectedIndexChanged fires the event
You can't capture an event prior to the change, but you could easily store the previous value in a variable. Each time SelectedIndexChanged is fired, use the previous value and then set it to the new index (for the next time the event fires). To handle the case when it's a new selection (from the default), you can either set the variable when the page loads, or allow it to be null and have that alert you to the fact it's a new selection (which you can then handle however you like).
<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlName_SelectedIndexChanged">
<asp:ListItem Text="John" Value="1"></asp:ListItem>
<asp:ListItem Text="Mark" Value="2"></asp:ListItem>
<asp:ListItem Text="Jim" Value="3"></asp:ListItem>
</asp:DropDownList>
.cs file code here:
public static int PreviousIndex;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlName.AppendDataBoundItems = true;
ddlName.Items.Add(new ListItem("Other", "4"));
PreviousIndex = ddlName.SelectedIndex;
}
}
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
string GetPreviousValue = ddlName.Items[PreviousIndex].Text;
Response.Write("This is Previously Selected Value"+ GetPreviousValue);
//Do selected change event here.
PreviousIndex = ddlName.SelectedIndex;
}