简体   繁体   中英

Why does the selectedindexchanged event not work when making controls visible

Im trying to have my drop down list make four controls visible on the selectedindexchanged event for the control.

Basically when the user chooses "Server" from the drop down list the event should fire and the user should see two extra options.

Tried a tonne of approaches so far including text_changed event but nothing.

Here is what i got so far

    //adds new fields to the form when the user selects server as the asset type
    protected void AddNewFields(object sender, EventArgs e)
    {
        //If the asset is a server then make the extra controls available
        if (TypeDDL.Text.Equals("Server"))
        {
            DNLabel.Visible.Equals(true);
            //DNLabel.Visible = true;
            DomainNameTB.Visible = true;
            RoleLabel.Visible = true;
            RoleDDL.Visible = true;
        }
    }


            <asp:DropDownList ID="TypeDDL" runat="server" DataSourceID="AssetTypeDS" 
                DataTextField="AssetTypeDescription" DataValueField="AssetTypeID" OnTextChanged="AddNewFields">
            </asp:DropDownList>

Add AutoPostback="True" to your DropDownList and the above code should trigger

As for an explanation: The drop down list doesn't automatically post back to the server. Changing a selection happens on the client side. Once you add the above, it'll repost the page. If you don't want the whole page to flicker each time someone changes a selection, you can either use some client side Javascript or Jquery, or use an asp:UpdatePanel

请设置DropdownList的AutoPostBack =“ True”属性...

add AutoPostBack="true" in dropdown

<asp:DropDownList ID="TypeDDL" runat="server" DataSourceID="AssetTypeDS"  AutoPostBack="true"
                DataTextField="AssetTypeDescription" DataValueField="AssetTypeID" OnTextChanged="AddNewFields">
            </asp:DropDownList>

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