1

I am using a [webmethod] to retrieve a list of strings from sql-server. the code is :

[WebMethod]
public void SelectActivityCheckboxItems(int SystemID)
    {
    //Configure a connection to SystemRegister database        
        var cs = ConfigurationManager.ConnectionStrings["SYSTEMREGISTERConnectionString"].ConnectionString;
        List<string> SelectedActivity = new List<string>();
        //Get the related data for update activty checkbox 
        using (var con = new SqlConnection(cs))
        {                
            con.Open();
            var cmd = new SqlCommand("setModalSystemUpdate", con) { CommandType = CommandType.StoredProcedure };
            cmd.Parameters.AddWithValue("@SYSTEMID", SystemID);
            var dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                SelectedActivity.Add(dr[0].ToString());
            }
            con.Close();
        }
        var js = new JavaScriptSerializer();
        Context.Response.Write(js.Serialize(SelectedActivity));   
}

Here is the checkboxlist:

 <div class="scroll">
                        <fieldset class="form-group">
                            <label class="col-sm-3">Aktivitet:</label>
                            <div>
                                <asp:CheckBoxList CssClass="paddingright" ID="CheckBoxListActivityUpdate" runat="server"
                                    DataSourceID="SqlDataSource6"
                                    DataValueField="ACTIVITYNAME"
                                    RepeatColumns="2">                                       
                                </asp:CheckBoxList>
                            </div>
                        </fieldset>
                    </div>

In my aspx I have this Jquery to "check" the items that are in the list and exist in the checkboxlist.

 $.ajax({
                    type: "POST",
                    url: "SystemService.asmx/SelectActivityCheckboxItems",
                    dataType: "json",
                    data: '{Value: "' + $("#<%=rowID.ClientID%>").value + '" }',
                    contentType: "application/json",
                    success: function (data) {
                        $.each(function () {
                            $("#CheckBoxListActivityUpdate").prop('checked', true);
                        })
                    }
                });  

unfortunatly this does not work, is there any other solutions?

Sebastian
  • 45
  • 1
  • 2
  • 12
  • 1
    What part doesn't work? – hardkoded Feb 21 '18 at 13:55
  • No checkbox item is selected/checked (there is data in the db) – Sebastian Feb 21 '18 at 13:59
  • Did you debug it? What are you getting in `res`? Does `$("#CheckBoxListActivityUpdate")` return an element? – hardkoded Feb 21 '18 at 14:33
  • I modified and remove res and any other parameters. my webservice returns: [{"Attributes":{"Keys":[],"Count":0,"CssStyle":{"Keys":[],"Count":0,"Value":null}},"Enabled":true,"Selected":false,"Text":"Football","Value":"Football"}] – Sebastian Feb 21 '18 at 14:49
  • I have exactly the same issue as (I am just using asp c#): https://stackoverflow.com/questions/15044340/jquery-set-checkbox-checked – Sebastian Feb 21 '18 at 15:03

1 Answers1

0

$.each(function () { with no input makes no sense. What's the point of that? It won't do anything because there's nothing for it to loop over, so the code inside it will never execute. You can just remove it.

Also you may need to use $("#<%=CheckBoxListActivityUpdate.ClientID%>") if the checkbox is an <asp:Checkbox server control.

Of course if you need to actually use the response data for something, and use it to control whether the box is checked or not, then you'll have to either write that yourself, or clarify the question to say exactly what should happen based on the response data (specify the properties in the data which are relevant and also show the target HTML).


As an aside, don't build your data object by hand, instead create a proper object and get the browser to stringify it for you, then you'll avoid any potential syntax errors:

data: JSON.stringify({ "Value": $("#<%=rowID.ClientID%>").value })
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • yes I am using '' . so should write selected instead of checked? `success: function (data) { $("#<%=CheckBoxListActivityUpdate.ClientID%>").prop('Selected', true); }` – Sebastian Feb 21 '18 at 15:33
  • when I pass the row.ID to the webmethod it will return a list of string. for example football and basketball. Then I want to check the available items in the checkboxlist and if there exist football and basketball then check/select them – Sebastian Feb 21 '18 at 15:36
  • so this is a _list_ of checkboxes, not a single checkbox? You can't set the whole list to "true" like that, and it sounds like you don't want to. You need to show us the HTML in that case. And your server doesn't return a string it returns some JSON. Which property from the JSON should we be looking at to match with the checkbox? Also, if the checkbox value is not in the returned data, should be un-check it? Your requirements are a bit incomplete. – ADyson Feb 21 '18 at 15:40
  • Yes you are right it is a checkboxlist which its items are populated from sql-server. And my webmethod returns this now : ["Football"] and yes if the value is not in the returned data is should be unchecked – Sebastian Feb 21 '18 at 15:56