Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
How can I access CheckBoxes in javascript functions in ASP.net?

Please help me.

Thanks in advance!

[Edit]OP did in fact mean javascript so I'm re-editing OP's question[/Edit]

OP's code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="tour2.aspx.cs" Inherits="tour2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1"  runat="server">
    <title>www.smartwaytours.com</title>
    <script type="text/javascript">          function pageLoad() {      }
    function imageload()    {
       document.getElementById("img1").src=document.getElementById("File1").value; 
       alert(document.getElementById("img1").src);
         document.getElementById("hidHeight").value = document.getElementById("img1").height;
         document.getElementById("hidWidth").value  = document.getElementById("img1").width;
         //document.getElementById("img1").style.display="none"
         var height=document.getElementById("hidHeight").value;
         var width=document.getElementById("hidWidth").value;
         alert(height);         alert(width);                
         if((height!="61")&&(width!="64"))         { 
            alert("Hi");
            //document.getElementById("img1").style.display="";
            document.getElementById("img1").src = "images/ni(64X61).gif"
            //document.getElementById("File1").value= "fasdf";
            alert("Please upload 64 X 61 size image");            
           document.getElementById("hidHeight").value= "";
            document.getElementById("hidWidth").value= "";
            alert("ashdgjf");                        return false;           
                     }         else{         
         document.getElementById("img1").src=document.getElementById("File1").value; 
         }                    return ;     }    function imageload1()    {
        
        document.getElementById("img2").src=document.getElementById("File2").value;
        document.getElementById("HiddenField3").value = document.getElementById("img2").height;
         document.getElementById("HiddenField4").value  = document.getElementById("img2").width;
    }    function imageload2()    {        
        document.getElementById("img3").src=document.getElementById("File3").value;
        document.getElementById("HiddenField5").value = document.getElementById("img3").height;
         document.getElementById("HiddenField6").value  = document.getElementById("img3").width;
    }        function chkImg()    {       
        document.getElementById("img3").style.display = "";
        document.getElementById("File3").style.display = "";        }        
                    </script>    <style type="text/css">            
         .style3       {       	width:60%;       }         .style4        {
            text-align: right;        }     </style></head>
</head><body>    <form id="form1"  runat="server">
    <div>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
                
    <table width="966" height="629" border="0" align="center" style="background-color:Aqua" cellpadding="5" cellspacing="5" id="Table_01">
      <tr>
		<td colspan="5" background="images/inside_01.jpg"> </td>
  </tr>	<tr>
		<td width="44" background="images/bg1.jpg"> </td>
		<td colspan="3">
			<img src="images/inside_03.jpg" width="876" height="171" alt=""><table 
                class="style2">                <br />
                <br />
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>                <br />
                <br />                <br />
                <tr>                
                    <td class="style4">
                        Tour Title</td>
                                <td class
Tour Title</td>                                <td class="style3" >
                                    <asp:TextBox ID="txttourtitle" runat="server" Width="190"></asp:TextBox>
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txttourtitle" ErrorMessage="*"></asp:RequiredFieldValidator>
                                </td>
                            </tr>                            <tr>
                                <td class="style4">
                                    Tour Description</td>
                                <td class="style3">
                                    <textarea id="txttourdesc" cols="20" name="S1" rows="4" class="style3"></textarea>
                                    
                                    </td>
                                                                </tr>
                            <tr>
                                <td class="style4">
                                    Image(64X61)</td>
                                <td>                
                <img  id="img1" src=""  runat="server" alt="" />
                <asp:HiddenField ID="hidWidth" runat="server" />
                <asp:HiddenField ID="hidHeight" runat="server" />
                <input  type="file" id="File1"  runat="server"   önchange="return imageload();" />
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                   ErrorMessage="Upload Jpegs and Gifs only." 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$" 
ControlToValidate="File1"></asp:RegularExpressionValidator>
            </td>                            </tr>
                            <tr>
                                <td class="style4">
                                    Image(398X225)</td>
                                <td>                
                <img  id="img2" src=""  runat="server" alt="" />
                 <asp:HiddenField ID="HiddenField1" runat="server" />
                <asp:HiddenField ID="HiddenField2" runat="server" />
                <input  type="file" id="File2"  runat="server"  önchange="imageload1();" /><asp:RegularExpressionValidator 
                    ID="RegularExpressionValidator2" runat="server" 
                    rrorMessage="Upload Jpegs and Gifs only." 
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$" 
ControlToValidate="File2"></asp:RegularExpressionValidator>
            </td>                            </tr>
                            <tr>
                                <td class="style4">
                                     </td>
                                <td>
         <asp:UpdatePanel ID="UpdatePanel1" runat="server">            <contenttemplate>
            <input type="checkbox" id="CheckBox1"  runat="server"  önclick="chkImg()" />
            <asp:Label ID="Label1" Text="For Popular Tour" runat="server"></asp:Label>
                                       </td>            
                                                  </tr>
                            <tr>            <td>
                <br />                 </td>
            <td>                
                <img  id="img3"  runat="server" alt="" style="display:none"/>
                 <asp:HiddenField ID="HiddenField5" runat="server" />
                <asp:HiddenField ID="HiddenField6" runat="server" />
                <input  type="file" id="File3"  runat="server"  önchange="imageload2();" style="display:none"/></td>
        </tr>        <tr>        </contenttemplate>
Posted
Updated 3-Mar-11 0:50am
v5
Comments
Baji Jabbar 3-Mar-11 5:15am    
What! Is it about dynamically adding check boxes
leelavathikuna 3-Mar-11 5:39am    
no,
i am added one checkbox in the design feild.
and in below that i added one file upload control which is in the hidden feild.
if i can check that box that file upload will be displayed
but if i uncheck that box then the file upload control don't display in the form.but it was displaying how can i rectify this
Manfred Rudolf Bihy 3-Mar-11 6:52am    
Please do not use comments to post code. Code belongs in question or answers. If your the one who posted the question the code most likely belongs there. I fixed that for you now, but please don't do it again.
Manfred Rudolf Bihy 3-Mar-11 7:13am    
I've added a solution which should do what you needed. If you have issues with my solution please leave your comments there.
Manfred Rudolf Bihy 4-Mar-11 10:13am    
You said that my solution worked for you. Why did you not accept it and also neither voted on it.

In aspx
<asp:CheckBox ID="CheckBox1"; runat="server" />



C#

if (CheckBox1.Checked)
{
 //your actions

}


Hope this might help you.
 
Share this answer
 
v2
use Javascript to hide/show the control.

document.getElementById("").style.visibility="hidden";
 
Share this answer
 
I you're doing this from javascript this should work:

// Edit this function in your javascript section
function chkImg(box)
{
    if(box.checked == true)
    {
        //if you want the image to be shown if the checkbox is checked the use
        // display="block" or display="inline" in the next line
        document.getElementById("img3").style.display = "none";
        document.getElementById("File3").style.visibility="hidden";
    }
}


Make this change to your checkbox in your HTML:
<input type="checkbox" id="CheckBox1" onclick="chkImg(this);" />


You had a funny character in the attribute name "onclick" and the Runat="server" attribute is not needed. Passing "this" to the chkImg function gives you immeadiate access to the checkbox without fetching it via document.getElementById for instance.

Cheers!
 
Share this answer
 
v2
Comments
leelavathikuna 3-Mar-11 7:51am    
thanku it's working.
Manfred Rudolf Bihy 3-Mar-11 8:33am    
If it's working for you, then please accept my answer and vote for it if you liked it.
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900