简体   繁体   中英

ASP.NET with DropDownList

I added a DropDownList from the Toolbox to the Login page on a website I'm working on.

Once I choose a ListItem in the DropDownList (in my case lets say Gym for example...), when clicked, I want that three bars we'll be opened below my DropDownList (for example, the bars that we'll be opened are Username, Password and ID), I mean three TextBoxes beneath each other.

http://imageshack.us/photo/my-images/854/w7wn.jpg

我认为您可以尝试SelectedIndexChanged事件或Javascript来显示文本框而无需回发。

<select>
<option value="1">Gym 1</option>
<option value="2">Gym 2</option>
<option value="3">Gym 3</option>
<select>

At firest you put your textboxes in a panle then hide this panel and you should set Autopostback property of you drobdownlist to True and after selecting a item in DropDownList, postback will accur. So you can show that panel include text boxes.

What you might really be needing is dynamic text boxes.

In the html part:

<asp:DropDownList runat="server" ID="DDL1" autopostback = "true" onselectedindexchanged="DDL_SelectChanged" /> <asp:PlaceHolder runat="server" ID="PH1"> </asp:PlaceHolder>

In codebehind:

    void DDL_SelectChanged(object sender, EventArgs e)
    {
        if (DDL1.SelectedIndex == 1)
        {
            for (int i = 0; i < 3; i++)
            {
                TextBox newTB = new TextBox();
                newTB.ID = "TB" + i;
                PH1.Controls.Add(newTB);
            }
        }
    }

You could use MultiveView control. And set active view index in Dropdown's selectedIndexChanged event. I wrote some example code for you:

ASPx side:

<asp:MultiView ID="multiView" ActiveViewIndex="-1" runat="server">
        <asp:View ID="viewGym" runat="server">
            <asp:TextBox ID="txtBxUserName" runat="server" />
            <asp:TextBox ID="txtBxPassword" runat="server" />
            <asp:TextBox ID="txtBxId" runat="server" />
        </asp:View>
    </asp:MultiView>
    <asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true" 
        onselectedindexchanged="Dropdownlist1_SelectedIndexChanged">
        <asp:ListItem Text="Choose one club" Value="0" />
        <asp:ListItem Text="Gym" Value="1" />
        <asp:ListItem Text="Shoppers" Value="2" />
    </asp:DropDownList>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
    {
        if ( IsPostBack ) //don't forget :)
            return;
    }

    protected void Dropdownlist1_SelectedIndexChanged( object sender, EventArgs e )
    {
        if ( Dropdownlist1.SelectedValue == "1" ) //Gym item selected
        {
            multiView.ActiveViewIndex = 0; //Gym view active
        }
    }

If you want to any view not active when first page load then you set the ActiveViewIndex with -1 in aspx code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM