Link to home
Start Free TrialLog in
Avatar of Codeaddict7423
Codeaddict7423Flag for United States of America

asked on

passing values through checkbox

how do i  pass a valuethrough a checkbox
Avatar of Codeaddict7423
Codeaddict7423
Flag of United States of America image

ASKER

passing values through checkboxes
Where ho you want to pass values through checkbox?
On click of checkbox, do you want to enable javascript or do some processing  on server?
Please explain better your question
thank you. i'm getting a better definition of the goal for the checkboxes now and i'll ask again later.
hello,
i have a better understanding now.  how do i pass a value from a checkbox when the user checks a checkbox?
<input type="checkbox" name="mycb" value="myvalue"

if checked variable "mycb" will have value "myvalue"

is it u want?
<input type="checkbox" name="mycb" value="myvalue"> **
i tried the input idea and it did not pass the value. i have this
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="false" Text="April 23, 2010 Test"  />
how do i pass the value "April 23, 2010" directly from this check box so that the checkbox is identified wome  way other than "checkbox1" and has a value of a text string?
The best way is to put code in the checked changed event.
Then you can verify the checked property, for example:

private void myCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if(myCheckBox.Checked)
                  //do something
        }
great, i'm using vb.net, can you please provide sample code to make the asp:checkbox control have an id other than checkbox1 and pass a value of a text string?
To get Checkbox's text in code behind file use
string strChkBoxText = CheckBox1.Text;

To get Checkbox's text in javascript refer
http://blog-dotnetsp.blogspot.com/2010/01/get-checkbox-text-and-checked-property.html
rajapandian_81:
Thank you. can you please help with the javascript code to do this?
Codeaddict7423,
Whenever you click the checkbox, javascript will alert the checkbox's text. Is that you are looking for?
function checkboxClick(obj) 
    { 
        var lbl = obj.nextSibling; 
        while(lbl.nodeName.toLowerCase()!='label')  
            lbl=lbl.nextSibling; 
      
        alert(lbl.innerHTML);        
    } 


<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="false" Text="April 23, 2010 Test" onclick="checkboxClick(this);"  />

Open in new window

rajapandian_81:
Thank you, i tried your suggestion and the return result is "CheckBox1" = True.  How can assign an ID to CheckBox1 or rename it to something like "Date of Workshop" and have a text string attached to it so that it returns a value like "April 23" ? i'm really pressured to fix this... please help.
I can't understand your question. Please clarify your needs.
rajapandian_81:
Thanks for replying.  I'm really under pressure here. Im using VB.NET in a user control. in that user control there is a checkbox next to the description of an event scheduled for April 23, 2010.
i have a asp: checkbox "<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="false" Text="April 23, 2010 Test" onclick="checkboxClick(this);"  /> "

I'm trying to get this checkbox , when checked, to return a value something other thann "checkbox1" and a text string of "April 23, 2010"
i'm open to using javascript or vb.net code...any sample code that works is great....
Please, pleae help
So you need some string like 'Date of Workshop'  and the text of checkbox when it is checked.
Try below javascript. I will post vb.net code also.

function checkboxClick(obj) 
    {                 
        if (obj.checked)
        {
            var lbl = obj.nextSibling; 
            while(lbl.nodeName.toLowerCase()!='label')  
                lbl=lbl.nextSibling;   
            alert('Date of Workshop ' + lbl.innerHTML);      
        }        
    } 

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="false" Text="April 23, 2010" onclick="checkboxClick(this);"  />

Open in new window

rajapandian_81:
Thank yoiu. will this javascript code pass the value of the checkbox? I'm trying to get the checkbox name (something like "Date of Workshop") to pass a value or a string like "April 23, 2010" directly maby this van be passed on to a hidden field?

Please help...
Avatar of nomoose
nomoose

Hey Jason,
Did you come across this in your research? http://forums.asp.net/t/769696.aspx  Seems like this guy figured it out.

Hope this helps,
 Elliot
nomoose:
i'm sorry i did not see a solution to my question.
this is what i'm trying to do:
 I'm using VB.NET in a user control. in that user control there is a checkbox next to the description of an event scheduled for April 23, 2010.
i have a asp: checkbox "<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="false" Text="April 23, 2010 Test" onclick="checkboxClick(this);"  /> "

I'm trying to get this checkbox , when checked, to return a value something other thann "checkbox1" and a text string of "April 23, 2010"
i'm open to using javascript or vb.net code...any sample code that works is great....
Please, help
You can store the checkbox's id and text in hidden field and use it in server side.

function checkboxClick(obj) 
    {                 
        if (obj.checked)
        {
            var lbl = obj.nextSibling; 
            while(lbl.nodeName.toLowerCase()!='label')  
                lbl=lbl.nextSibling;               
            document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;            
        }        
    } 


<asp:CheckBox ID="Date_of_Workshop" runat="server" AutoPostBack="false" Text="April 23, 2010" onclick="checkboxClick(this);"  /> 
<input type="hidden" runat="server" id="hdn" />

Open in new window

rajapandian_81:
thank you for the reply
when i implemented your solution, i got this:
-----------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'hdn' is not declared.

Source Error:

Line 89:             while(lbl.nodeName.toLowerCase()!='label')  
Line 90:                 lbl=lbl.nextSibling;                
Line 91:             document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;            
Line 92:         }        
Line 93:     }
 --------------

how do i declare "hdn"?
thanks,
In Comment #29217725 I added one hidden control like

<input type="hidden" runat="server" id="hdn" />
rajapandian_81:
Thanks for the quick reply. Yes, i saw that and included it in my code. i have the Javascript as:
<script type="text/javascript" language="javascript">

    function checkboxClick(obj)  
    {                  
        if (obj.checked)
        {
            var lbl = obj.nextSibling;  
            while(lbl.nodeName.toLowerCase()!='label')  
                lbl=lbl.nextSibling;                
            document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;            
        }        
    }  
</script>

and the checkbox code as follows:

<asp:CheckBox ID="Date_of_Workshop" runat="server" AutoPostBack="false" Text="April 23, 2010" onclick="checkboxClick(this);"  />  
<input type="hidden" runat="server" id="hdn" />

what did i do wrong?
Which version of visual studio you are using?

Are you getting the same error?
rajapandian_81:
I'm using Visual Studio 2005
I'm trying to declare "hdn" in the Javascript so the script would run but am not havingany luck.

I get this error at runtime:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'hdn' is not declared.

Source Error:

 

Line 51:             while(lbl.nodeName.toLowerCase()!='label')  
Line 52:                 lbl=lbl.nextSibling;                
Line 53:             document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;            
Line 54:         }        
Line 55:     }  
 
Delete the line
<input type="hidden" runat="server" id="hdn" />

and add HiddenField from ToolBox. Then change the id to hdn
rajapandian_81:
i'm still getting the same error;

this is what i put in:

<asp:CheckBox ID="Date_of_Workshop" runat="server" AutoPostBack="false" Text="April 23, 2010" onclick="checkboxClick(this);"  />  

    <input id="hdn" type="hidden" runat="server" />
-----------

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'hdn' is not declared.

Source Error:

 

Line 53:             while(lbl.nodeName.toLowerCase()!='label')  
Line 54:                 lbl=lbl.nextSibling;                
Line 55:             document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;            
Line 56:         }        
Line 57:     }  
 
rajapandian_81:

The error seems to be coming from the Javascript area and i was trying to declare "hdn" as var hdn=" ";
rajapandian_81:
The error is specific to this line:
Line 55:             document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;

please please help!
Codeaddict7423:

As mentioned in Comment #29221471, remove the line
<input type="hidden" runat="server" id="hdn" />

Then try to add HiddenField control from ToolBox. Then change the id to hdn
rajapandian_81:
Thanks for the quck reply, i did as you suggested, deleted the <input type hidden field and then readded a new field as follows:
<asp:CheckBox ID="Date_of_Workshop" runat="server" AutoPostBack="false" Text="April 23, 2010" onclick="checkboxClick(this);"  />  

    <input id="hdn" type="hidden" runat="server" />

however, i 'm still getting this:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30451: Name 'hdn' is not declared.

Source Error:

 

Line 53:             while(lbl.nodeName.toLowerCase()!='label')  
Line 54:                 lbl=lbl.nextSibling;                
Line 55:             document.getElementById('<%= hdn.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;            
Line 56:         }        
Line 57:    
 
i'm desperate for a solution...please help!
I think you misunderstood.

I told to add the control from toolbox. You can drag and drop the control from toolbox. Refer below screenshot.

ToolBox.PNG
So that new control will be added like
<asp:HiddenField ID="HiddenField1" runat="server" />
So final code will be

function checkboxClick(obj)  
    {                  
        if (obj.checked) 
        { 
            var lbl = obj.nextSibling;  
            while(lbl.nodeName.toLowerCase()!='label')   
                lbl=lbl.nextSibling;                
            document.getElementById('<%= HiddenField1.ClientID %>').value = obj.id + ' ' + lbl.innerHTML;             
        }         
    }  
 
 
<asp:CheckBox ID="Date_of_Workshop" runat="server" AutoPostBack="false" Text="April 23, 2010" onclick="checkboxClick(this);"  />  
<asp:HiddenField ID="HiddenField1" runat="server" />

Open in new window

vb.net code:
Set the AutoPostBack as true for checkbox. Then CheckedChanged code will be

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If (CheckBox1.Checked) Then
            SomeFunction(CheckBox1.ID + " " + CheckBox1.Text)
        End If
    End Sub

    Sub SomeFunction(ByVal strText As String)
    //Here write steps you needed
    End Sub

Open in new window

rajapandian_81:
Thank you for your s uggestion.
Attached, please see a combined screenshot of what i see on my screen.
The "Handles CheckBox1.CheckedChanged" is giving me an error indication in Visual Studio in VB.NET

Please help
checkboxes-01.jpg
What error it shows?
Its working fine for me.
rajapandian_81:

thanks for the quick reply.  in visual Studio, the sub called:

Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)  Handles CheckBox1.CheckedChanged

the line that starts wtih : Handles CheckBox1.CheckedChanged
is displayed with a squigly blue line under the word "CheckBox1" and throws an error on run

Is your both screenshot taken from user control page?
rajapandian_81:
yes, absolutely from the user control screen
In top can you see CheckBox1 and CheckedChanged as shown in image below
CheckBox.PNG
I just changed the dropdown controls to "checkbox1" and "checkedchanged"  appears on the other control. i still get the wavy line, how do i change  that?
Its working fine for me.
I am not able to figure out the problem in your application.
Can you create a sample application to reproduce error and attach here as a zip file.
rajapandian_81:
thank yoiu for your help and time. maybe if i step back and explain what i'm being asked to do you can see.
I have a user control with an asp:checkbox next to a description of a workshop. i'm being asked to program the checkbox so that when a user checks the checkbox, a field (perahps a hidden field) gets populated wtih a string like "April 23, 2010" and returns that string value along with a checkbox name of something like "Workshop One"  I'm being told this is a simple program and i'm banging my head against the wall.

If you and your colleagues can discuss this and help me with a solution, you'd save my day and more...
Thanks,
Codeaddict7423:
I am not able to attach application, so I give the code here.

Code for Default.aspx
----------------------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register src="WebUserControl.ascx" tagname="UserControl2" tagprefix="uc1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
    <uc1:UserControl2 ID="UserControl21" runat="server" />
    </div>
    </form>
</body>
</html>


Code for usercontrol ( WebUserControl.ascx)
---------------------------------------------------------------
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
<asp:CheckBox ID="Date_of_Workshop" runat="server" AutoPostBack="true" Text="April 23, 2010" OnCheckedChanged="Date_of_Workshop_CheckedChanged" />


Code for WebUserControl.ascx.vb
------------------------------------------------
Partial Class WebUserControl
    Inherits System.Web.UI.UserControl
    Protected Sub Date_of_Workshop_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Date_of_Workshop.CheckedChanged
        If (Date_of_Workshop.Checked) Then
            SomeFunction(Date_of_Workshop.ID + " " + Date_of_Workshop.Text)
        End If
    End Sub

    Sub SomeFunction(ByVal strText As String)

    End Sub
End Class
rajapandian_81:
Sir,
Below, please find a code that seems to function but i'd like your opinion.
vbcode:
<script runat="server">
 
    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       
        Dim Workshop_One_Date As CheckBox = CType(RSVPForm1.FindControl("Workshop_One_Date"), CheckBox)
        Dim Date_of_First_Workshop As TextBox = CType(RSVPForm1.FindControl("Date_of_First_Workshop"), TextBox)
     
       
       
        Date_of_First_Workshop.Visible = "false"
       
       
        If Workshop_One_Date.Checked = True Then
            Date_of_First_Workshop.Text = "April 23, 2010"
            Date_of_First_Workshop.Visible = "false"
         
        Else
            Date_of_First_Workshop.Text = ""
        End If
    End Sub
</script>

------------
code that calls the checkbox
<table cellpadding="0" cellspacing="0" style="border-color:gray" border="0">
<tr>
<td>
<asp:CheckBox ID="Workshop_One_Date" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_CheckedChanged" />
</td>
<td>
<asp:TextBox ID="Date_of_First_Workshop" runat="server" Visible="false"></asp:TextBox>
</td>
</tr>
</table>
-------------
return from form:
First Name: luis
Last Name: hernandez
Title: test
Organization: hgac
Address: 3555 timmons
City: houston
State: tx
Zip Code: 77027
Phone #: 7134445555
Cell Phone #: 713445555
Email: luis.hernandez@h-gac.com
Workshop_One_Date: True
Date_of_First_Workshop: April 23, 2010
Workshop_Two_Date: True
Date_of_Second_Workshop: May 11, 2010
Your Comments:  : fema test 012
-------------
My question is: How can i keep the checkbox id from displaying and how can i make the textbox id display a name without the underscores?

Please help. thank you again for your time.
rajapandian_81:
i've placed a hidden field next to a text box

<input id="Hidden1" type="hidden" runat="server" />

now i have programmed into the sub the following:      
Dim Hidden1 As HiddenField = RSVPForm1.FindControl("Hidden1")
Date_of_First_Workshop.Text = Hidden1.Value
i'm getting this error at runtime:
Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputHidden' to type 'System.Web.UI.WebControls.HiddenField'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputHidden' to type 'System.Web.UI.WebControls.HiddenField'.

any thoughts as to how to get the value of a hidden field?


   
---------->  How can i keep the checkbox id displaying
You can use ID property to get id of checkbox like below.

        Dim strCheckBoxId As String
        strCheckBoxId = CheckBox1.ID()

OR

        Dim strCheckBoxId As String
        strCheckBoxId = Workshop_One_Date.ID()

------> how can i make the textbox id display a name without the underscores?
You can use Replace command to replace the underscores to blank space like below.

strCheckBoxId = strCheckBoxId.Replace("_", " ")
rajapandian_81:
Thank you for your suggestion.
Below, please find my sub code. It's not working and still returns checkboxID with underscores. Can you please review and let me know where i went wrong?
----------
Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       
        Dim Workshop_One_Date As CheckBox = CType(RSVPForm1.FindControl("Workshop_One_Date"), CheckBox)
        Dim Date_of_First_Workshop As TextBox = CType(RSVPForm1.FindControl("Date_of_First_Workshop"), TextBox)
     
        'Dim Hidden1 As HiddenField = RSVPForm1.FindControl("Hidden1")
       
        Date_of_First_Workshop.Visible = "false"
       
        Dim strWorkshop_One_Date As String
        strWorkshop_One_Date = Workshop_One_Date.ID()
       
        Dim strDate_of_First_Workshop As String
        strDate_of_First_Workshop = Date_of_First_Workshop.ID()
                 
        If Workshop_One_Date.Checked = True Then

            'Workshop_One_Date.Attributes.Add("April 23, 2010", ToString())
           
            strWorkshop_One_Date = strWorkshop_One_Date.Replace("_", " ")
           
            strDate_of_First_Workshop = strDate_of_First_Workshop.Replace("_", " ")
           
            Date_of_First_Workshop.Text = "April 23, 2010"
            Date_of_First_Workshop.Visible = "true"
            '  Date_of_First_Workshop.Text = Hidden1.Value
         
        Else
            Workshop_One_Date.Text = " "
            Date_of_First_Workshop.Text = ""
        End If
    End Sub
------------
ASKER CERTIFIED SOLUTION
Avatar of rajapandian_81
rajapandian_81
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
rajapandian 81,
Thank you. when i implemented your solution, it seems like placing the code breaks the "if -- End If" sub. My code is as follows:
-------
<script runat="server">
 
    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
       
        Dim Workshop_One_Date As CheckBox = CType(RSVPForm1.FindControl("Workshop_One_Date"), CheckBox)
        Dim Date_of_First_Workshop As TextBox = CType(RSVPForm1.FindControl("Date_of_First_Workshop"), TextBox)
     
        'Dim Hidden1 As HiddenField = RSVPForm1.FindControl("Hidden1")
       
        Date_of_First_Workshop.Visible = "false"
       
        Dim strWorkshop_One_Date As String
        strWorkshop_One_Date = Workshop_One_Date.ID()
       
        Dim strDate_of_First_Workshop As String
        strDate_of_First_Workshop = Date_of_First_Workshop.ID()
                 
        If Workshop_One_Date.Checked = True Then

            Workshop_One_Date.LabelAttributes.Add("Workshop One Date", "April 23, 2010")
     
            strWorkshop_One_Date = strWorkshop_One_Date.Replace("_", " ")
           
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "TxtBoxSelect", "<script language=JavaScript>alert('" + strWorkshop_One_Date + "")</script>")
     
            strDate_of_First_Workshop = strDate_of_First_Workshop.Replace("_", " ")
           
            Date_of_First_Workshop.Text = "April 23, 2010"
            Date_of_First_Workshop.Visible = "true"
            '  Date_of_First_Workshop.Text = Hidden1.Value
         
        Else
            Workshop_One_Date.Text = " "
            Date_of_First_Workshop.Text = ""
        End If

       
    End Sub
</script>
---------

Also, I have been looking at the checkbox attribute:
                Dim Workshop_One_Date As New CheckBox
            Workshop_One_Date.LabelAttributes.Add("Date of Workshop", "April 23, 2010")

and placing that in the FormLoad sub. i got this from the following:
http://www.15seconds.com/issue/040804.htm 

maybe the checkbox has a lable attribute that can be accessed and display a different name, etc.
Thanks,
Hello,
I have a user control with an asp:checkbox next to a description of a workshop. i'm being asked to program the checkbox so that when a user checks the checkbox, the checkbox control returns the name of the checkbox (in a string format with other than  the name of the checkbox) and a string value (other than just "true" or "false" .  For example, if checkbox ID="chk_workshop_01", i'm trying to replace the name of this checkbox control to "Workshop One Date:" and if "chk_workshop_01.checked = true" Then replace the value "true" with a string like "April 23, 2010"

I'm trying to work with the  labelattributes like this:
chk_workshop_01.LabelAttributes.Add("April 23, 2010", "lblWorkshop_01") but I'm not having any success.  

All i need is a return from the form is a checkbox control with a string name and a value other than "true" or "false" instead of "true" or "false"

Any help with this is greatly appreciated.
Avatar of Nasir Razzaq
As I have been trying to tell you in your other question, you are on the wrong track. You either need to modify the code which is reading the value of the checkbox or you need to use a property in the usercontrol which you set according to the value of the checkbox. As I have asked in your other question, how are the values received from you usercontrol?
Thank You very much!