Avatar of celler_wellbridge
celler_wellbridgeFlag for United States of America asked on

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

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.
.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
celler_wellbridge

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
JacobBushong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
burakiewicz

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
celler_wellbridge

Thank you JacobBushong, burakiewicz and jmwheeler.

Are these items to add to the CS page? And where do I place them?

I will try adding them to sections beginning at the top, until it runs.

After the using lines, the CS page has:

public partial class Controls_PageName: System.Web.UI.UserControl
{
    public string TraceCat = "PageName.ascx.cs";
    public string CC;

    protected void Page_Load(object sender, EventArgs e)
    {


----------

Also, if I use

ddlReason.selectedindex = 1

Should I eliminate the:

          <asp:ListItem Value="">Please select a reason</asp:ListItem>

...and only keep the:

          <asp:ListItem Value="CorInq" Selected="true">CorInq</asp:ListItem>

So that it will be in the first index position?


And if the panel is not visible, even though the control is enabled, will it matter? Or could I just hard code the value into the CS page and forget about the dropdown?

...as in

          ddlReason.SelectedValue="CorInq";

Thank you for the help.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jmwheeler

SelectedIndex = 1 will actually select the second item as indices start with 0
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
celler_wellbridge

Sorry about delay in response here... too much production demand.

The resolution is a combo and thanks to you guys for the direction!

In the ascx file:

1. At the drop down list (ddl), in this form it is "ddlReason",  choose the item to be selected when the form loads (rather than selected and reloaded):

<asp:DropDownList ID="ddlReason" runat="server" ...>
<asp:ListItem Selected="True" Value="ThisReason">This Reason</asp:ListItem>
</asp:DropDownList>

2. in .ascx.cs file:

protected void Page_Load(object sender, EventArgs e)
    {string OPTION;

     OPTION = "ThisReason";

... skipping, skipping, skipping... and at the bottom (after double click on element in design view):

    protected void ddlFitnessInquiry_DataBound(object sender, EventArgs e)

{
        ddlReason.SelectedValue = "CompOrientation";    
}

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
celler_wellbridge

Thank you.