S
Stimp
I have a checkboxlist (chkMyList) which is created from a (name, value)
pair from a database table.
I have a read-only textbox which will be used to hold a total of
all the numerical values of each checkbox that is checked, and this
value will change dynamically via javascript when a box is ticked or unticked.
e.g.
<script language="javascript">
function GetCost() {
var iTotalCost;
iTotalCost = 0;
if (document.form.chkMyList_0.checked){
iTotalCost = iTotalCost + 15;}
if (document.form.chkMyList_1.checked){
iTotalCost = iTotalCost + 18;}
....etc...
document.form.txtTotalCost.value = iTotalCost;
}
</script>
<asp:checkboxlist id="chkMyList" runat="server" />
<asp:textbox id="txtTotalCost" runat="server" ReadOnly="True" />
...but when I try to add the 'onchange=GetCost();' to chkMyList in the
Page_Load():
chkMyList.Attributes.Add("onChange", "GetCost();")
...it doesn't add it to the individual checkboxes, but rather to the
<table> generated for the checkboxes by the checkboxlist function.
Hence it doesn't work.
Is there any way to add the javascript to each checkbox in the
checkboxlist as they are generated?
Also a better suggestion for the javascript function would be most
appreciated (Ideally I'd like a function that takes in the checkbox
value, rather than having to hardcode a load of 'if' statements etc)
Thanks in advance!
Peter
pair from a database table.
I have a read-only textbox which will be used to hold a total of
all the numerical values of each checkbox that is checked, and this
value will change dynamically via javascript when a box is ticked or unticked.
e.g.
<script language="javascript">
function GetCost() {
var iTotalCost;
iTotalCost = 0;
if (document.form.chkMyList_0.checked){
iTotalCost = iTotalCost + 15;}
if (document.form.chkMyList_1.checked){
iTotalCost = iTotalCost + 18;}
....etc...
document.form.txtTotalCost.value = iTotalCost;
}
</script>
<asp:checkboxlist id="chkMyList" runat="server" />
<asp:textbox id="txtTotalCost" runat="server" ReadOnly="True" />
...but when I try to add the 'onchange=GetCost();' to chkMyList in the
Page_Load():
chkMyList.Attributes.Add("onChange", "GetCost();")
...it doesn't add it to the individual checkboxes, but rather to the
<table> generated for the checkboxes by the checkboxlist function.
Hence it doesn't work.
Is there any way to add the javascript to each checkbox in the
checkboxlist as they are generated?
Also a better suggestion for the javascript function would be most
appreciated (Ideally I'd like a function that takes in the checkbox
value, rather than having to hardcode a load of 'if' statements etc)
Thanks in advance!
Peter