Question JS Function + Checkbox

soni_1985

Member
Joined
Jul 11, 2009
Messages
8
Programming Experience
1-3
I want that when i check or uncheck the checkbox state,accordingly in label text is changed.
If i check the checkbox,then in label text is checked else unchecked.

VB.NET:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        CheckBox1.Attributes.Add("oncheckedchanged", "javascript: return CheckboxState")
    End Sub


<script language ="javascript" type ="text/javascript"> 
 function CheckboxState()
 {
 var checkbox=document.getElementById("CheckBox1");
 var lbl=document.getElementById ("Label1");
 alert(checkbox.checked);
 lbl.innertext=checkbox.checked;
 return false;
 }
 
</script>

But when i check or uncheck,firstly page is postbacked & secondly,in label nothing is coming..
 
Last edited by a moderator:
VB.NET:
<html>
<head>

<script type="text/javascript">

function changeLabel(bool) {
   document.getElementById("myLabel").innerHTML = (bool) ? "Checked" : "Not Checked";
}

</script>
</head>
<body>
<input type="checkbox" id="myCheckbox" onclick="changeLabel(this.checked)">
<label for="myCheckbox" id="myLabel">Not Checked</label>
</body>
</html>
 
I don't think you can update a control without posting back with only server side code.

You can try and use the DynamicPopulate AJAX control.
 
hmm yea it was html control , plus it can be impossible but everything is possible ;)
VB.NET:
<script runat="server">
    Sub Page_Load(sender As Object, e As EventArgs)
        CheckBox1.Attributes("onclick") = "mm ()"
 
        
        
    End Sub
</script>

<script language ="javascript" type ="text/javascript"> 

function mm()
{
alert('af');

var theControl = document.getElementById("<%= CheckBox1.ClientID %>");
var theControl2 = document.getElementById("<%= textbox1.ClientID %>");
theControl2.value = theControl.checked






}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form  id="form1" runat="server">
        <asp:CheckBox ID="CheckBox1" runat="server"/> 
       
        <br />
        <asp:TextBox ID="TextBox1" runat="server">nnn</asp:TextBox>
    </form>
Cheer
 
Back
Top