2

I try to checked specific item in CheckBoxList with JQuery client side in user control?

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:AppCon %>"     
    SelectCommand="SELECT DesF, val, DesGrpId FROM dbo.tblDes WHERE (DesGrpId = @DesGrpId)">    
</asp:SqlDataSource>

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="DesF" DataValueField="val"
    DataSourceID="SqlDataSource1">

3
  • what is the condition for specific item? Commented Sep 7, 2013 at 10:06
  • 1
    a quick recommandation: Move away from this style of writing T-SQL code inside your UI markup. Refer to: asp.net/web-forms/overview/… Commented Sep 7, 2013 at 10:10
  • this is an text separate whit ',' example : '1,2,3' Commented Sep 7, 2013 at 10:13

2 Answers 2

7

Server side Control:

<asp:CheckBoxList ID="ChkList" runat="server">
    <asp:ListItem Text ="a" Value="1"></asp:ListItem>
    <asp:ListItem Text ="b" Value="2"></asp:ListItem>
    <asp:ListItem Text ="c" Value="3"></asp:ListItem>
</asp:CheckBoxList>

Client Side Code:

<script type="text/javascript">
    $(function () {
        var str = "1,2";
        var list = $('#<%= ChkList.ClientID%> input');
        list.each(function (index) {
            item = $(this);
            if (str.indexOf(item.val()) != -1) {
                item.attr('checked', true);
            }
        });
    });
    </script>
Sign up to request clarification or add additional context in comments.

Comments

2

Are you trying to get all the checked items using jQuery? If so just use

var checkedItems = $('#parentDivOfCheckBox').find('input:checked');

1 Comment

In that case you have to do something like this $('#chkListParent').find('input:checkbox').each(function(){ if(this.val == 'something') { $(this).attr('checked','checked');}; });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.