开发者

disable or hide listbox by dropdown selection in C#

HELLO

i have one dropdown box and one listbox ,

my dropdown box values is

1-ALL
2-CUSTOM

my listbox values is retrieved from sql database

  • email addresses

what i want to do is if i select ALL from the dropdown box , it will disable or hide the list box in the web page. , if i choose "CUSTOM" it will enable it again.

i tried this code, but it doesn't work

if (DropDownList1.Text == "CUSTOM")
    {
开发者_如何学Python        ListBox1.Visible = true;
    }

note : i put visible = false in the properties of the listbox1

where is the problem exactly ? and where should i put this condition on the .cs page ?


You need to add an event to the dropdown box, so that your code is executed when the event fires. If you are using the designer, select the dropdown then above where the properties are there should be a little lightning symbol. Click on that and you will see all the events that the dropdown can fire. In there look for SelectedIndexChanged. Double click in there and it will create some code for you, which will look something like:

protected void mycombobox_SelectedIndexChanged(object o, EventArgs e)
{

}

Put your code in that section.


UPDATED: If the text in the dropdown is CUSTOM then use this

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "CUSTOM")
        {
            ListBox1.Visible = true;
        }
    }

You will also need to set the AutoPostBack="true" on the DropDownList1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜