CheckBox value on HTML page triggered from Code Behind

  • Thread starter Thread starter Julius Fenata
  • Start date Start date
J

Julius Fenata

Dear all,

I have checkbox on my webform, and I want that when I click my checkbox, it
should change status between true or false...

Here is my code,

// Code Behind
CheckBox1.Attributes["onClick"] = "JSGeneratePackageID()";

How to make my CheckBox has value? Because in the html page my checkbox
doesn't have any value? I assign checkbox value to a label, but it doesn't
show up.

// HTML Code
function JSGeneratePackageID()
{
document.getElementById("Label1").innerText =
document.getElementById("CheckBox1").innerText; // Didn't work.
}

Because I need to use checkbox value to switch the checkbox value, whenever
it has 'click' event.., like this...

//HTML Code
if (document.getElementById("CheckBox1").innerText = = "False")
document.getElementById("CheckBox1").innerText = "True";
document.getElementById("Label1").innerText = "True";
else
document.getElementById("CheckBox1").innerText = "False";
document.getElementById("Label1").innerText = "False";


Thx,
Julius F
 
Hi Mr. Gaurav, I have follow your instructions and thanks for your help, the
checkbox change status when has a first click on it, but.. when I repeatedly
clicking on it, it doesn't change value anymore.., here is my code:

// HTML
function JSGeneratePackageID()
{
if (document.getElementById("chb_vIsDefect").selected)
document.getElementById("chb_vIsDefect").selected = false; // Can't
work, how to change checkbox value?
document.getElementById("lbl_vValue").innerText = "false";
else
document.getElementById("chb_vIsDefect").selected = true; // Can't
work, how to change checkbox value?
document.getElementById("lbl_vValue").innerText = "true";
}

// Code Behind
chb_vIsDefect.Attributes["onClick"] = "JSGeneratePackageID()";


Thx,
Julius F

===================================================
 
The property you are looking for is "checked".

docment.all["chb_vIsDefect"].checked = true;

I am not sure if the click event bubbles to actually turn the checkbox, so
you might want return true from this function, so the click event could
bubble.

bill

Julius Fenata said:
Hi Mr. Gaurav, I have follow your instructions and thanks for your help, the
checkbox change status when has a first click on it, but.. when I repeatedly
clicking on it, it doesn't change value anymore.., here is my code:

// HTML
function JSGeneratePackageID()
{
if (document.getElementById("chb_vIsDefect").selected)
document.getElementById("chb_vIsDefect").selected = false; // Can't
work, how to change checkbox value?
document.getElementById("lbl_vValue").innerText = "false";
else
document.getElementById("chb_vIsDefect").selected = true; // Can't
work, how to change checkbox value?
document.getElementById("lbl_vValue").innerText = "true";
}

// Code Behind
chb_vIsDefect.Attributes["onClick"] = "JSGeneratePackageID()";


Thx,
Julius F

===================================================

MasterGaurav said:
document.getElementById("Label1").value =
(document.getElementById("CheckBox1").selected ? "true" : "false");

InnerText is not applicable to "<input ..." elements. It's mostly to
spans and text areas, where you have an inner-text.


CHeers,
Gaurav Vaish
http://mastergaurav.org
http://mastergaurav.blogspot.com
 
Back
Top