John
John

Reputation: 3945

disable button until value is selected ---select---doesnt appear

SO far everything working ok, button is disabled until value is selected from the drop down list, However I want ---select--- to be the first option on the drop down list, but this is not happening...any ideas as to why?

<asp:DropDownList ID="DropDownListSubContractors" runat="server" 
    AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id" 
    onchange='selectChanged(this);'>
    <asp:ListItem Text="---Select---" Value="0" />
    <asp:ListItem Text="Option 1" Value="1" />
</asp:DropDownList>

<script type="text/javascript">

   function selectChanged(e) {
    document.getElementById("<%= addNewSubContractor.ClientID %>").disabled 
      = (e.options[e.selectedIndex].value == "0") ? "disabled" : "";
                          }
</script>

<asp:Button ID="addNewSubContractor" Text="Add Sub Contractor" 
    OnClick="UploadSubContractor" runat="server" disabled='disabled' />

Upvotes: 0

Views: 736

Answers (2)

CallumHolden
CallumHolden

Reputation: 83

Have you got append databound item set to true, this checks to see if there are any items in the collection, and adds them first before binding?

Upvotes: 0

Denys Wessels
Denys Wessels

Reputation: 17019

Showing the manually added items first followed by the data bound items is the default behavior of the drop down list, so ---Select--- should in theory be the first item.

I suspect there is some logic elsewhere(can be javascript or code behind) which sets the selected item to something else i.e:

DropDownListSubContractors.SelectedIndex = 1;

Have a good look through your code and make sure the selected item is not being re-set anywhere

Upvotes: 1

Related Questions