Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a dropdownlist control and it's populated with a list of peoples names from a database. I want to enable a CheckBox control if the user selects a person in the list and disable the checkbox if they select BLANK (also an option in the list).

Here is a portion of my code...

    <tr>
        <td>&nbsp;<asp:Label ID="lblAssignedTo1" runat="server" Text="Assigned To:"></asp:Label></td>
        <td><asp:DropDownList ID="ddlAssignedTo1" runat="server" AppendDataBoundItems="True" DataSourceID="dsAssignedTo" DataTextField="StaffName" DataValueField="StaffID"><asp:ListItem Text="" /></asp:DropDownList></td>
    </tr>
    <tr>
        <td>&nbsp;<asp:Label ID="LabelEmail1" runat="server" Text="Send Email:"></asp:Label>
        </td>
        <td><asp:CheckBox ID="cbEmail1" runat="server" Checked="true" /></td>
    </tr>

The checkbox is a trigger to send an email to the person selected from the list. I want it to default the checkbox to "enabled" if a person is selected from the list to make sure the program I am using is going to send an email later on.

I had a look at http://api.jquery.com/change/ for an example of this, but it's not using a checkbox control, so not sure if it would work. Sorry I am new to jScript.

Thanks in advance

share|improve this question

1 Answer

up vote 0 down vote accepted

A pure HTML and JavaScript approach would look something like this:

<select id="people">
    <option value="">Select One</option>
    <option value="person1">Person 1</option>
    <option value="person2">Person 2</option>
    <option value="person3">Person 3</option>
</select>
<input type="checkbox" name="sendemail" id="sendemail" disabled="disabled" />

$(document).ready(function() {
    $('#people').change(function() {
        if($(this).val() == '') {
            $('#sendemail').attr('disabled', 'disabled');   
        }
        else {
            $('#sendemail').removeAttr('disabled');   
        }
    });
});

http://jsfiddle.net/AEXpG/

In terms of your code, just grab the select list and checkbox ClientId and then apply the above jQuery code to them.

share|improve this answer
Awesome! Thank you so much. – Fernando68 Mar 19 at 6:25
@Fernando68 Please accept the answer if it did the trick for you. – Nathan Taylor Mar 19 at 22:28
How? Sorry I am new here. I do not see an "ACCEPT" button or link. – Fernando68 Mar 21 at 1:20
@Fernando68 Click the checkmark next to my answer. – Nathan Taylor Mar 21 at 22:15
1  
Done! Thanks mate – Fernando68 Mar 28 at 2:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.