How to get the previous item in DropDownList before OnSelectedIndexChanged fires an event

How do I get the previous item on DropDownListbefore I OnSelectedIndexChangedfire the event?

Example: I had DropDownListone that has names as its elements ("John", "Mark"). The default SelectedIndexis John. After changing the index and selecting Mark, an event will occur OnSelectedIndexChanged. When I use it ddlName.SelectedIndex, it will only return the index for the "Mark" that I want to get, this is the "John" index.

+5
source share
3 answers

, . , SelectedIndexChanged , , ( ). , ( ), , , , ( ).

+4
<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 :

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;

        }
+3

e.OldValues.

<asp:DropDownList ID="esDropDownList" runat="server" DataSourceID="SqlDataSourceddlEnrolmentStatus" DataTextField="EnrolmentStatusDescription" DataValueField="EnrolmentStatusID" SelectedValue='<%# Bind("StudentEnrolmentStatus") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceddlEnrolmentStatus" runat="server"
ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>" SelectCommand="SELECT [EnrolmentStatusID], [EnrolmentStatusDescription] FROM [tblEnrolmentStatuses] ORDER BY [EnrolmentStatusID]">
</asp:SqlDataSource>

And in the code behind (if your dropdown is on the form) ...

    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
..
            String msg = "This is the new value " + e.NewValues["StudentEnrolmentStatus"].ToString()+ " and this is the old value " + e.OldValues["StudentEnrolmentStatus"].ToString();
..
    }
+1
source

All Articles