This is a migrated thread and some comments may be shown as answers.

checkbox not changing value when changed via JS

1 Answer 126 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Jason Potter
Top achievements
Rank 1
Jason Potter asked on 30 Oct 2013, 11:56 PM
Hi there,

I have created a onclick event for the checkbox's label (since it does not toggle when you click on it), The Javascript is working and is successfully toggling the checkbox when the label clicked, but when I do a postback to save the values it saves the checkboxes as if I have not touched them. If toggle the checkboxes byclicking on the boxes themselves it works but not when not when I toggle then with JS (even if visually they have toggled). 

Anyone know why?

Thanks
Jason

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Nov 2013, 11:02 AM
Hi Jason Potter,

Please have a look into the sample code I tried which works fine as expected.

ASPX:
<telerik:RadListBox ID="RadListBox1" runat="server" CheckBoxes="true" AutoPostBack="true"
    OnPreRender="RadListBox1_PreRender" OnClientItemChecked="OnClientItemChecked1">
    <Items>
        <telerik:RadListBoxItem Text="Item1" runat="server" Value="1" />
        <telerik:RadListBoxItem Text="Item2" runat="server" Value="2" />
        <telerik:RadListBoxItem Text="Item3" runat="server" Value="3" />
    </Items>
</telerik:RadListBox>
<telerik:RadButton ID="RadButton1" runat="server" Text="Save" OnClick="RadButton1_Click">
</telerik:RadButton>

JavaScript:
<script type="text/javascript">
    function CellClick(text, value) {
        var list = $find("<%=RadListBox1.ClientID %>");
        list.findItemByValue(value).set_checked(!list.findItemByValue(value).get_checked());
    }
    function OnClientItemChecked1(sender, args) {
        args.get_item().set_checked(!args.get_item().get_checked());
    }
</script>

C#:
protected void RadListBox1_PreRender(object sender, EventArgs e)
{
    foreach (RadListBoxItem item in RadListBox1.Items)
    {
        item.Attributes.Add("onclick", "CellClick('" + item.Text + "','" + item.Value + "');");
    }
}
protected void RadButton1_Click(object sender, EventArgs e)
{
    string[] data = new string[10];
    for (int i = 0, j = 0; i < RadListBox1.Items.Count; i++)
    {
        if (RadListBox1.Items[i].Checked == true)
        {
            data[i] = RadListBox1.Items[i].Text;
            j++;
        }
    }
    for (int i = 0; i < data.Length; i++)
    {
        Response.Write(data[i]);
    }
}

Thanks,
Princy.



 
Tags
ListBox
Asked by
Jason Potter
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or