6

I dynamically populate a dropdownlist of all 50 states from an ArrayList on PageLoad. When the user selects the SUBMIT button (btnSubmit_Click event), the SelectedIndex property of the dropdownlist control is always 0 despite what selection the user selects.


Added more code to help troubleshooting. Getting a -1 both from the session variable (bbb) and from the ddlState.selectedindex (bbb).

HTML code in form:

<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" 
    onselectedindexchanged="ddlState_SelectedIndexChanged" >
</asp:DropDownList>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
    //------------------------------------------------
    // Populates state dropdownlists
    //------------------------------------------------
    if (!IsPostBack)
    {
        GetAllStatesForDdl(ddlDLState);
        GetAllStatesForDdl(ddlOldState);
        GetStatesForDdl(ddlState);
    }
}

private void GetAllStatesForDdl(DropDownList ddlStateList)
{
    AppInputFormProcessor getStates = new AppInputFormProcessor();
    ArrayList States = new ArrayList();
    States = getStates.GetAllStates();
    ddlStateList.DataSource = States;
    ddlStateList.DataBind();
}

private void GetStatesForDdl(DropDownList ddlStateList)
{
    AppInputFormProcessor getStates = new AppInputFormProcessor();
    ArrayList States = new ArrayList();
    States = getStates.GetStates();
    ddlStateList.DataSource = States;
    ddlStateList.DataBind();
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int aaa = ddlState.SelectedIndex;
    int bbb = Convert.ToInt32(Session["ddlState"]);
}

protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["ddlState"] = ddlState.SelectedIndex;
}
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
Susan
  • 1,822
  • 8
  • 47
  • 69
  • 1
    you need to re-work the formatting there - currently you have one method nested inside another - it wouldn't even compile – dice Mar 08 '12 at 14:26
  • @Susan Does the user choose the option you are expecting to be returned or is it done programmatically based on what the user does? Are all of the State values visible in the DropDown when the user clicks the submit button? – Josh Mein Mar 12 '12 at 16:24
  • Does your page or any control in which your DropDown resides have `EnableViewState="false"`? – Mike Guthrie Mar 13 '12 at 14:51
  • @Susan I think we need more information to further help you. – Josh Mein Mar 14 '12 at 13:31
  • @Susan Im Unable to recreate your error. It seems to remember the SelectedIndex on post back. Is there a specific setting you have set? the only way i see is you are repopulating the dropdownlist before retrieving the selectedIndex – Gnani Mar 16 '12 at 09:30

9 Answers9

4

When I had trouble with ViewState (that is what l suspect in your case) l used this to restore data to a dynamically populated dropdown object

    public void Page_Load(object sender, EventArgs e){
            if (!IsPostBack)
            {
                Databind();
            }
            else {
                LoadAllViewStates();
            }
    }
    private void Databind()
        {
            DataTable questionnaireDT = null;
            DataTable questionsDT = null;
            DataTable indicatorDT = null;

            DataView tempView = QuestionnaireDS.Select(DataSourceSelectArguments.Empty) as DataView;
            questionnaireDT = tempView.Table;
            ViewState["QuestionnaireDL"] = questionnaireDT;
            QuestionnaireDL.DataSource = ViewState["QuestionnaireDL"];
            QuestionnaireDL.DataBind();

            tempView = QuestionDS.Select(DataSourceSelectArguments.Empty) as DataView;
            questionsDT = tempView.Table;
            ViewState["QuestionList"] = questionsDT;
            QuestionList.DataSource = ViewState["QuestionList"];
            QuestionList.DataBind();

            tempView = IndicatorDS.Select(DataSourceSelectArguments.Empty) as DataView;
            indicatorDT = tempView.Table;
            ViewState["IndicatorLst"] = indicatorDT;
            IndicatorLst.DataSource = ViewState["IndicatorLst"];
            IndicatorLst.DataBind();
        }

        private void LoadAllViewStates()
        {
            QuestionnaireDL.DataSource = ViewState["QuestionnaireDL"];
            QuestionnaireDL.DataBind();

            QuestionList.DataSource = ViewState["QuestionList"];
            QuestionList.DataBind();

            IndicatorLst.DataSource = ViewState["IndicatorLst"];
            IndicatorLst.DataBind();
        }

To restore the selected index, I passed the selectedIndex into a hidden field.

Hope this helps?

By the way, why pass in the DropDownList object as a parameter? Instead call a parameterless function and populate the DropDownList object within the function.

Also, ensure that ViewState isnt switched off.

Derrick
  • 323
  • 1
  • 8
1

This should work for you. However, I am sort of confused why you are passing the dropdown to the function to get the states. Do you have multiple dropdowns to be filled? I think we need to see your html to be of more help.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
        GetStatesForDdl(ddl);
}

 private void GetStatesForDdl(DropDownList ddlStateList)
 {
     AppInputFormProcessor getStates = new AppInputFormProcessor();
     ArrayList States = new ArrayList();
     States = getStates.GetStates();
     ddlStateList.DataSource = States;
     ddlStateList.DataBind();
 }
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
  • Yes, I have 3 state dropdownlists that need to be populated and there is logic for each predicated on earlier user selections changing which states are presented in the list. – Susan Mar 08 '12 at 14:29
  • @Susan I think we need to see your html then and a more thorough explanation of what you are trying to do. Your question as it stands is deceiving. – Josh Mein Mar 08 '12 at 14:30
  • You are getting ddlStateList as parameter to the method but not using it. Binding to ddlState.... – Kaf Mar 08 '12 at 14:40
  • @ Indikaf I just copied her code for that chunk, but I will update my answer. – Josh Mein Mar 08 '12 at 14:49
1

In your example, you are trying to get the selected value from a session variable, but there's no code shown that actually sets anything in the session.

Even if you have some sort of async call that sets a session variable, this is a very dangerous practice: as soon as someone opens up a 2nd tab, you risk the chance of data corruption.

chris
  • 36,094
  • 53
  • 157
  • 237
0

If you bind your drop down list items in OnInit all of the view state will be handled automatically by the framework. If you bind it in Page_Load you're not adding them until after the viewstate is already applied. Move it to OnInit and you'll not have to worry about it.

dave
  • 115
  • 1
  • 6
0

First you will need to populate and databing the list only in a if( !IsPostBack ) { ... statement.

Next - Session["ddlState"] is never being populated, not sure where you want to populate this but you could do it in the selected index changed as you stated. this should work if you are not calling DataBind() on the postback.

dice
  • 2,820
  • 1
  • 23
  • 34
  • I tried your suggesting of adding a SelectedIndexChanged event and storing the selected index to a session variable, but since the dropdownlist is only loaded on !IsPostBack, the dropdownlists are now empty. – Susan Mar 12 '12 at 13:36
0

How about this:

protected void GetStatesForDdl(DropDownList ddlStateList)
{
    //remove event handler
    ddlStateList.SelectedIndexChanged -= new EventHandler(ddlState_SelectedIndexChanged);

    //business as usual
    AppInputFormProcessor getStates = new AppInputFormProcessor();
    ArrayList States = new ArrayList();
    States = getStates.GetStates();
    ddlStateList.DataSource = States;
    ddlStateList.DataBind();

    // then force it to select desired index
    ddlStateList.SelectedIndex = (int)Session["ddlState"];

   //ok, stick it back to control
   ddlStateList.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
}

I suggest usage of local variables in ViewState instead of Session and SelectedValue instead of SelectedIndex.

Deka
  • 131
  • 1
  • 4
  • 12
0

This could help you out:

While binding the drop down list specify the DataTextField and the DataValueField properties. The reason for getting the selected index as 0 during the Submit is because the drop down does not contain unique values associated to each item in it. And remember that the property specified in DataValueField should be unique. i.e. each item in the drop down should be uniquely identifiyable.

For example, consider that the States class is specified like this:

public class State
{
    public string Name { get; set; }
    public string Id { get; set; }
}

Now the drop down list should be binded as given below. Here for every States Item the 'Id' property is unique.

ddlStateList.DataSource = States;
ddlStateList.DataTextField = "Name";
ddlStateList.DataValueField = "Id";
ddlStateList.DataBind();
Krishna
  • 636
  • 3
  • 8
  • I'm not sure I understand you comment about why I'm getting a selectedindex of 0; it suggests to me that during the buttonSubmit I've lost the selectedindex and it is 0 because that is the default value. – Susan Mar 15 '12 at 12:43
  • The selected index for the drop down list is based on the value of the **user selected item**. So if you have all the items in ddlStateList with the same value, the control returns the first item with the **user selected value**. In your case that would be 0 - the first item. – Krishna Mar 15 '12 at 16:40
  • Now consider a case where the drop down list is defined as ` ` In this case if you select the third item, the selected index would be 0 and if you select the fourth item, the selected index would be 1. Hope this helps you. – Krishna Mar 15 '12 at 16:40
0

If you have ViewState disabled, then you have to save the SelectedIndex into the Session and on the Page_Load, you always reload the data and set the index directly. When you bind the datasource, send the index as a parameter to default the index (from session) and set the SelectedIndex to this number after you call DataBind().

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39
0

When you say, you don't use ViewState, does it mean, you have it disabled for the page or your dropdown list? That could be your problem. I know a lot of people have asked you this, but I don't see any answer from you to suggest whether or not you are using viewstate. When your user makes a selection and your page posts back, you still need viewstate, so your page knows what the user selects even after posting back. Then its upto you to either save it in session or viewstate.

On the other hand, if your viewstate is enabled, add one piece of code on a new page to see when the problem starts to appear. Remember to not touch the viewstate and to go with the default. To start with, add a page with a dropdown and a button and bind it to 2 dummy values and read the user selection on post back. As you keep adding more code and controls, if the problem re-appears, you'll be able to find out what line of code is giving you grief.

Hope this helps.

Divi
  • 7,621
  • 13
  • 47
  • 63