Link to home
Start Free TrialLog in
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.
ASKER CERTIFIED SOLUTION
Avatar of JacobBushong
JacobBushong
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of celler_wellbridge

ASKER

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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmwheeler
jmwheeler

SelectedIndex = 1 will actually select the second item as indices start with 0
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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";    
}

Thank you.