Master Yoda
Master Yoda

Reputation: 4412

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>

Upvotes: 0

Views: 616

Answers (3)

sangram parmar
sangram parmar

Reputation: 8736

add AutoPostBack="true" in dropdown

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

Upvotes: 0

Mikey Mouse
Mikey Mouse

Reputation: 3098

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

Upvotes: 2

Rakesh_HBK
Rakesh_HBK

Reputation: 181

Please set AutoPostBack="True" property of a DropdownList...

Upvotes: 1

Related Questions