Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7636167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:36:18+00:00 2026-05-31T07:36:18+00:00

I dynamically populate a dropdownlist of all 50 states from an ArrayList on PageLoad.

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:36:19+00:00Added an answer on May 31, 2026 at 7:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I populate a dropdown list (in a JSP page) dynamically with all
I want to dynamically populate a form field from multiple other fields in the
In MVC.NET, how should I dynamically populate the HTML structure from a database into
i m using following code to populate dropdownlist dynamically... i want that value should
I'm trying to dynamically populate a dropdown when a user is trying to add
I have a Menu control that I dynamically populate with categories. When a user
Good evening all. I'm attempting to dynamically populate a select_tag call in my rails
I need to dynamically populate a form from fields in a database using EXT
How to dynamically populate list box from database? Please Provide some examples in zend
I've a MainMenu.xib and a MyDocument.xib. I need to dynamically populate some NSMenuItem's from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.