We help IT Professionals succeed at work.
Get Started

How do I force drop down list selections on page load?

1,042 Views
Last Modified: 2013-12-16
Hello good people.

I have a .NET project where I need to HIDE two drop down lists (enabled=true, visible=false), and force one selection from each.

This is so that the page loaded will automatically display a few secondary panels THE FIRST TIME the page loads. And so that these preselected choices will be used behind the page for other purposes.

HERE IS THE FIRST DROP DOWN LIST.

It is populated from the CS page.

- - - - - - - - - - - - - - -

<asp:Panel ID="pnlRO" runat="server" Enabled="true">
     <asp:DropDownList ID="ddlRC" AutoPostBack="true" OnSelectedIndexChanged="ddlRC_SelectedIndexChanged" runat="server" Visible="true">
          <asp:ListItem Value="">Please select</asp:ListItem>                    
     </asp:DropDownList>
</asp:Panel>

- - - - - - - - - - - - - - - -

HERE IS THE CS CODE

- - - - - - - - - - - - - - - -

                if(!Request.Url.ToString().Contains("loc"))
                {
                    if (!Page.IsPostBack)
                    {
                        pnlRO.Visible = true;
                        ddlRC.Items.Clear();
                        ddlRC.Items.Add(new ListItem("Select One", ""));
                        ddlRC.Items.Add(new ListItem("D", "D"));
                        ddlRC.Items.Add(new ListItem("M", "M"));
                        ddlRC.Items.Add(new ListItem("I", "I"));
                        ddlRC.Items.Add(new ListItem("D2", "D2"));
                        ddlRC.Items.Add(new ListItem("T", "T"));
                    }
                }
                else
                {
                    if(SiteMap.CurrentNode.Url.Contains("/loc/"))
                    {
                        pnlRO.Visible = false;
                        pnlddlreason.Visible = true;
                        CC = SiteMap.CurrentNode.Url.ToString();
                        CC = CC.Replace("~/locations", "");
                        CC = CC.Remove(0, 1);
                        CC = CC.Replace(".aspx", "");
                        CC = CC.Substring(0, CC.IndexOf("/"));
                    }
                }

- - - - - - - - - - - - - - -

HERE IS THE CODE, IN THE ASPX PAGE, FOR THE SECOND DROP DOWN LIST.

I have removed all the other options and leave only the one I wish SELECTED:

- - - - - - - - - - - - - - -

<asp:Panel ID="pnlddlreason" Visible="false" runat="server">
     <asp:DropDownList ID="ddlReason" runat="server" OnSelectedIndexChanged="ddlReason_SelectedIndexChanged" AutoPostBack="True">
          <asp:ListItem Value="">Please select a reason</asp:ListItem>
          <asp:ListItem Value="CorInq" Selected="true">CorInq</asp:ListItem>
     </asp:DropDownList>
</asp:Panel>

- - - - - - - - - - - - - - -

That is all there is and there is no more*.

Thank you for your help.

* There is no more unless I forgot something.... it happens.
Comment
Watch Question
This problem has been solved!
Unlock 5 Answers and 9 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE