asp.netbuttonpageload

Disable button if dropdownlist is null


I am trying to disable a button on page load. my current code is the following, but when I open the page the button is active.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(DropDownList5.SelectedValue))
            Button1.Enabled = true;
        else
            Button1.Enabled = false;
    }

Solution

  • Albert D. Kallal's comment resolved my issue.

    You need to use DropDownList5.SelectedItem.Text, or .Value. You could also probably better use DropDownlist5.SelectedIndex which is 0 for the first value which as noted tends to be selected by default.