Click here to Skip to main content
15,867,488 members
Articles / Web Development / ASP.NET

Why DropDownList SelectedValue Does Not Work Inside SelectedIndexChanged Event?

22 May 2014CPOL2 min read 80K   5   6
Why DropDownList SelectedValue Does Not Work Inside SelectedIndexChanged Event?

Debugger Inside DropDown SelectedIndexChanged Event

Debugger Inside DropDown SelectedIndexChanged Event
In this blog, we will explore one interesting bug, which ASP.NET developers encounter sometimes.

Bug

DropDownList SelectedValue inside SelectedIndexChanged event does not give the current selected value, rather it gives the default value or first option value.

Walk Through

  1. Let’s declare a DropDownList first.
    XML
    <asp:DropDownList ID="ddlDropDownId" runat="server" AutoPostBack="true" 
                      OnSelectedIndexChanged="ddlDropDownId_SelectedIndexChanged">
    </asp:DropDownList>
  2. Now to bind the DropDownList, we will write one function like below…
    C#
    private void BindDropDownList()
    {
        // Declare a Dictionary to hold all the Options with Value and Text.
        Dictionary<string, string> options = new Dictionary<string, string>();
        options.Add("-1", "Select Option");
        options.Add("1", "Option 1");
        options.Add("2", "Option 2");
        options.Add("3", "Option 3");
        options.Add("4", "Option 4");
        options.Add("5", "Option 5");
    
        // Bind the Dictionary to the DropDownList.
        ddlDropDownId.DataSource = options;
        ddlDropDownId.DataTextField = "value";
        ddlDropDownId.DataValueField = "key";
        ddlDropDownId.DataBind();
    }
  3. So, when you run the Page, it would show you the DropDownList on the Page with the Options defined.

    DropDownList on Browser

    DropDownList on Browser
  4. When you select an Option from the DropDownList, it hits the SelectedIndexChanged event in the code behind. If you need to get the SelectedValue, then you will write…
    C#
    protected void ddlDropDownId_SelectedIndexChanged(object sender, EventArgs e)
    {
        string value = ddlDropDownId.SelectedValue;
    }

Now Here Comes the Main Issue

When we put a debugger inside the Event and checked, irrespective of the selection of any Option, it always gives you the first Option Value that is “-1?. Refer to the debugging screen shot at the top.
And there is no doubt on the first Option value “-1?, as we can clearly see from the Source HTML of the DropDownList. See highlighted option in the image below.

DropDownList Source HTML

DropDownList Source HTML

Any Hint Here?

As we are getting the default value or the first value always, so the guess is that, something is causing the DropDownList to bind again when we select an Option.

Let’s Find Out !!!

In the walk through steps, I mentioned about the function BindDropDownList(), which binds the DropDownList. But we need to check where exactly it is getting called. As the DropDownList shows when the Page is Loaded, it must be present inside the Page Load Event.

C#
protected void Page_Load(object sender, EventArgs e)
{
    BindDropDownList();
}

Now, according to the Event Life Cycle rules, when you select an Option from DropDownList, it posts back and goes to Page Load Event first and then it comes to the SelectedIndexChanged Event.
That means, it comes to the Page Load again on selection of any Option and calls the BindDropDownList(). Thus, the DropDownList is bounded again, before it goes to the SelectedIndexChanged Event. As the DropDownList is bound again, so the SelectedValue is now the first Option Value, which is “-1? (for “Select Option”).

Fix?

You might have got the answer. That is IsPostBack Property.

Gets a value that indicates whether the page is being rendered for the first time or is being loaded in response to a postback.

It tells you whether you are loading the Page for the first time or is it a Post Back due to any Event. Modified code would look something like below:

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindDropDownList();
    }
}

So, Be Careful and Share This to Save Someone’s Day !!!

You might have played with all the codes in your page to find out the issue, but at last you come to know that it was because you forgot to check IsPostBack property. So, like and share this with your friends so that anybody who is unable to find the problem, finally lets out a sigh.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

Comments and Discussions

 
Generalthanx yar mere case me maine jquery postback ko culprit samajh raha tha but i reloaded this two times. Pin
varun1507-Jun-15 21:00
varun1507-Jun-15 21:00 
GeneralRe: thanx yar mere case me maine jquery postback ko culprit samajh raha tha but i reloaded this two times. Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Dec-15 0:19
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)29-Dec-15 0:19 
QuestionOne more solution!!! Pin
Oliver Lundt31-Mar-15 17:46
Oliver Lundt31-Mar-15 17:46 
AnswerRe: One more solution!!! Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)31-Mar-15 22:10
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)31-Mar-15 22:10 
QuestionTHANKS!!! Pin
Member 1080903320-Jan-15 8:33
Member 1080903320-Jan-15 8:33 
AnswerRe: THANKS!!! Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)20-Jan-15 15:40
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)20-Jan-15 15:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.