How to Check if Checkboxes are Checked? ^_~

Hullo,

What I'm basically trying to do is see if a checkbox is checked, and if it is, then change the value of a hiddenfield to update a database.  Here's my code:
 
    protected void Rankbox_CheckedChanged(object sender, EventArgs e)
{
if (Rankbox = checked)
{
Rank.Value = 1;
}
else if (Rankbox = unchecked)
{
Rank.Value = 0;
}
}
 My checkbox is named "Rankbox", and the hidden field with information that needs changing is "Rank".  The error Visual Studio is giving me basically says that it hates the if statements and what they're checking.  I've tried switching them out with "Rankbox.checked == true", "rankbox is checked", and a whole ton of other things.  I'm not exactly sure what the problem is.

I'm using asp.net 2.0 with C#, and any help would be very appreciated.

Thanks.
0
WayTooAwesome
7/7/2006 8:10:18 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

12 Replies
600 Views

Similar Articles

[PageSpeed] 39

You may try this:

Set your checkbox like this:

<asp:CheckBox id="Rankbox" OnCheckedChanged="Rankbox_CheckedChanged" AutoPostBack=True Runat=server></asp:CheckBox>

Use this code in server side:

protected void Rankbox_CheckedChanged(object sender, EventArgs e)

{

if (Rankbox.Checked == true)

{

//Do something;

}

else if (Rankbox.Checked == false)

{

//Do something;

}

}


This posting is provided "AS IS" with no warranties, and confers no rights.
0
JimmyM
7/7/2006 9:30:44 PM
When I do that, the error changes (Seemingly in a progressive way).  It says that "Rank" and "Rankbox" "do not exist in the current context"...  I'm using them in a FormView's EditItemTemplate tags, think that might have something to do with it, or is it something entirely different?

Thanks.
0
WayTooAwesome
7/7/2006 9:34:41 PM
Please show your code of how do you set up the chckbox control.
This posting is provided "AS IS" with no warranties, and confers no rights.
0
JimmyM
7/7/2006 9:39:18 PM
Alright, here's the entirety of my UI code:
 
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
            SelectCommand="SELECT * FROM [Employee] WHERE (([Username] = @Username) AND ([Password] = @Password))" DeleteCommand="DELETE FROM [Employee] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Employee] ([ID], [ManagerID], [MManagerID], [Rank], [FName], [LName], [Title], [Phone], [URL], [Responsibilities], [MonthlyGoals], [Misc], [Picture], [Username], [Password]) VALUES (@ID, @ManagerID, @MManagerID, @Rank, @FName, @LName, @Title, @Phone, @URL, @Responsibilities, @MonthlyGoals, @Misc, @Picture, @Username, @Password)" UpdateCommand="UPDATE [Employee] SET [ManagerID] = @ManagerID, [MManagerID] = @MManagerID, [Rank] = @Rank, [FName] = @FName, [LName] = @LName, [Title] = @Title, [Phone] = @Phone, [URL] = @URL, [Responsibilities] = @Responsibilities, [MonthlyGoals] = @MonthlyGoals, [Misc] = @Misc, [Picture] = @Picture WHERE [ID] = @ID">
            <SelectParameters>
                <asp:SessionParameter Name="Username" SessionField="Username" Type="String" />
                <asp:SessionParameter Name="Password" SessionField="Password" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <!-- Here are just a bunch of repetative asp:parameters -->
            </UpdateParameters>
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Decimal" />
            </DeleteParameters>
            <InsertParameters>
                <!-- Here are some more of those parameters -->
            </InsertParameters>
        </asp:SqlDataSource>

<table cellpadding="5" cellspacing="5" border="0"> <asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1"> <EditItemTemplate> <tr> <td style="text-align:left; vertical-align:top;" width="175">First Name:<asp:Textbox ID="FNameLabel" runat="server" Text='<%# Bind("FName") %>'></asp:Textbox> Last Name:<asp:Textbox ID="LNameLabel" runat="server" Text='<%# Bind("LName") %>'></asp:Textbox><br /> Title:<i><asp:Textbox ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>'></asp:Textbox></i><br /> Phone Number:<asp:Textbox ID="PhoneLabel" runat="server" Text='<%# Bind("Phone") %>'></asp:Textbox><br /> </td> <td style="text-align:left; vertical-align:top; width:200px"> Homepage:<asp:Textbox ID="URLLabel" runat="server" Text='<%# Bind("URL") %>'></asp:Textbox><br /> Picture:<asp:TextBox ID="PictureLabel" runat="server" Text='<%# Bind("Picture") %>'></asp:TextBox><br /><!--<img alt="Picture" height="125" width="125" src="Pictures/<%# Eval("Picture") %>"/>--> <asp:CheckBox ID="Rankbox" runat="server" Text="Manager?" Checked="false" AutoPostBack="true" OnCheckedChanged="Rankbox_CheckedChanged" /><asp:Hiddenfield ID="Rank" runat="server" Value='<%# Bind("Rank") %>'></asp:Hiddenfield> </td> </tr> <tr> <td style="text-align:left; vertical-align:top">Responsibilities:</td> <td style="text-align:left; vertical-align:top"><asp:Textbox ID="ResponsibilitiesLabel" runat="server" Text='<%# Bind("Responsibilities") %>' TextMode="MultiLine"> </asp:Textbox></td> </tr> <tr> <td style="text-align:left; vertical-align:top">Monthly Goals:</td> <td style="text-align:left; vertical-align:top"><asp:Textbox ID="MonthlyGoalsLabel" runat="server" Text='<%# Bind("MonthlyGoals") %>' TextMode="MultiLine"> </asp:Textbox></td> </tr> <tr> <td style="text-align:left; vertical-align:top">Miscellaneous Info:</td> <td style="text-align:left; vertical-align:top"><asp:Textbox ID="MiscLabel" runat="server" Text='<%# Bind("Misc") %>' TextMode="MultiLine"></asp:Textbox></td> </tr> <tr> <td><asp:Linkbutton ID="Update" runat="server" Text="Update" CommandName="Update"></asp:Linkbutton></td> <td><asp:Linkbutton ID="Cancel" runat="server" Text="Cancel" CommandName="Cancel"></asp:Linkbutton></td> </tr> </EditItemTemplate> <ItemTemplate> <tr> <td style="text-align:left; vertical-align:top;" width="175"><asp:Label ID="FNameLabel" runat="server" Text='<%# Bind("FName") %>'></asp:Label> <asp:Label ID="LNameLabel" runat="server" Text='<%# Bind("LName") %>'></asp:Label><br /> <i><asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>'></asp:Label></i><br /> <asp:Label ID="PhoneLabel" runat="server" Text='<%# Bind("Phone") %>'></asp:Label><br /> <asp:Label ID="URLLabel" runat="server" Text='<%# Bind("URL") %>'></asp:Label><br /> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> </td> <td style="text-align:left; vertical-align:top; width:200px"><img alt="Picture" height="125" width="125" src="Pictures/<%# Eval("Picture") %>"/></td> </tr> <tr> <td style="text-align:left; vertical-align:top">Responsibilities:</td> <td style="text-align:left; vertical-align:top"><asp:Label ID="ResponsibilitiesLabel" runat="server" Text='<%# Bind("Responsibilities") %>'> </asp:Label></td> </tr> <tr> <td style="text-align:left; vertical-align:top">Monthly Goals:</td> <td style="text-align:left; vertical-align:top"><asp:Label ID="MonthlyGoalsLabel" runat="server" Text='<%# Bind("MonthlyGoals") %>'> </asp:Label></td> </tr> <tr> <td style="text-align:left; vertical-align:top">Miscellaneous Info:</td> <td style="text-align:left; vertical-align:top"><asp:Label ID="MiscLabel" runat="server" Text='<%# Bind("Misc") %>'></asp:Label></td> </tr> </ItemTemplate> </asp:FormView> </table> </div> </form>
 
0
WayTooAwesome
7/7/2006 9:45:29 PM
I just realized that it'd be very hard to find that part of the code, so here's just the checkbox portion:
 
<asp:CheckBox ID="Rankbox" runat="server" Text="Manager?" Checked="false" AutoPostBack="true" OnCheckedChanged="Rankbox_CheckedChanged" /><asp:Hiddenfield ID="Rank" runat="server" Value='<%# Bind("Rank") %>'></asp:Hiddenfield>
 And that's in the EditItemTemplate portion.
0
WayTooAwesome
7/7/2006 9:47:16 PM

Rank.Value = (Rankbox.Checked) ?

"1" : "0";

should do it for you. Do you have intellisense switched off or something?

0
MartinSmithh
7/7/2006 9:54:31 PM

Ah we really need delete functionality here. Went to answer another question and see the story progressed quite a lot whilst my browser was still open at the original question! Ignore me!

 

 

0
MartinSmithh
7/7/2006 9:56:33 PM
MartinSmithh:

Rank.Value = (Rankbox.Checked) ? "1" : "0";

should do it for you. Do you have intellisense switched off or something?



It gives me the same error that "Rank" and "Rankbox" "do not exist in the current context"...  When I begin typing "Rank", the closest matach intellisense brings up is "Rankbox_CheckedChanged".

Not sure why they're not being identified in my backend.  I tried putting this same code in script tags on the UI page, but it gave me the same error.

@_@
0
WayTooAwesome
7/7/2006 9:59:24 PM

Im not too sure why you need the hidden field but in any case I don't know C# too well but in VB it would be something like the following I think:

So if you can convert this I think it should do what you need

Dim chk As CheckBox

chk = CType(FormView1.FindControl("Rankbox"), CheckBox)

If chk.Checked = True

'Do Whatever

Else

'Do something else

End If

0
justin0501
7/7/2006 10:01:06 PM
Aha!  After several tries I have come across a new error, and it's farther down the line!
 
        FormView1.FindControl("Rank").Value = (FormView1.FindControl("Rankbox").Checked) ?
"1" : "0";
 This time it's that "system.web.ui.control does not contain a definition for 'Value'" and the same thing with 'Checked'.

We're making progress here.  >>
0
WayTooAwesome
7/7/2006 10:08:09 PM
As they say in South America...

GOOOOOOOOOOOOOAAAAAAAAAAAALLLLLLLLLLLLLLLLLLLL
 
        ((HiddenField)(FormView1.FindControl("Rank"))).Value = (((CheckBox)(FormView1.FindControl("Rankbox"))).Checked) ?
"1" : "0";
 That code finally worked...  Tricky stuff right there.  Thanks everybody, thanks a ton!
0
WayTooAwesome
7/7/2006 10:11:50 PM

Try to find your checkbox control like this:

CheckBox Mycheckbox = (CheckBox)this.FormView1.FindControl("Rankbox");


This posting is provided "AS IS" with no warranties, and confers no rights.
0
JimmyM
7/7/2006 10:31:53 PM
Reply:

Similar Artilces:

checking checkbox is checked or not using checkbox.checked
Hi,my checkbox is<asp:CheckBox ID="Chkcity" runat="server" Text="My own city"  OnCheckedChanged="EventHandler"/>code behind: Private Sub Chkcity_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chkcity.CheckedChangedif (Chkcity ...............)end if End SubERROR    'EventHandler' is not a member of 'ASP.bpoaddjob_aspx'.    Please Mark the post as "ANSWER" if it helps you I think it should be like this:<asp:CheckBox ID="Chkcity"...

Check All Check Box While Check Header CheckBox
 hello...I have two grid on same page...both grid contains header checkbox and item checkbox i want that if use check header checkbox then all item check box checked,i have javascript for this functinality but problem is that when i click on header check box of first grid then checkbox of second grid also checked that i dnt want i want to perform this operation on one grid at one time.i have attache my script also...plz help me....thanks<script type="text/javascript">        function SelectAll(ID)      &nbs...

Check One CheckBox To Check All CheckBoxes
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked. I set the AutoPostBack property of the CheckBox in the Header to True & am invoking a sub named CheckAllRows on the CheckedChanged event of this CheckBox. The CheckBox in the Header exists within the HeaderTemplate of a TemplateColumn in the DataGrid & the rest of the CheckBoxes reside within the ItemTemplate of the same TemplateColumn. This is the code (the ID of the CheckBox in the Header...

Check out/check in
Hello How can i use the process check out/check in in PowerBuilder 8.0.3 ? We work in network. The Source Control is PB Native. I don't understand what is the parameters of Source Control ? Thank you Better to ask this in the source control group. Lots of posts on PBNative -- Evita Chapa Senior Systems Analyst II Command Technologies Inc. <Herv� Bareth> wrote in message news:3f69678e.7b3a.846930886@sybase.com... > Hello > > How can i use the process check out/check in in PowerBuilder > 8.0.3 ? We work in network. The Source Control is PB Native. ...

Check out/check in
Hello How can i use the process check out/check in in PowerBuilder 8.0.3 ? We work in network. The Source Control is PB Native. I don't understand what is the parameters of Source Control ? Thank you Have you looked the PB8 User's guide? Chapter 3 talks about source control. -- Evita Chapa Senior Systems Analyst II Command Technologies Inc. <Herv� Bareth> wrote in message news:3f69ba0a.592b.846930886@sybase.com... > Hello > > How can i use the process check out/check in in PowerBuilder > 8.0.3 ? We work in network. The Source Control is PB Na...

Check In Check Out
I have some source code that I need to modify and it was sent to me in a Registered status. Therfore, when I open any object it says " 'object name' is registered but not checked out and therefore can not be overwritten. Open anyway?" Then when I try to check it out, I get "PBNative checkout not allowed for registered objects." How can I get around this? When I look under the "Source" menu, all the items concerning registered are grayed out. TIA Option1:Copy the object(Only) to someother pbl and try . Option2:If You have any SCC Expo...

Check-in/Check-out
I'm having some difficulty with powerbuilder's check-in/check-out operation. When I check an object back in, the .pbl the object is being moved from is destroyed. Has anyone else experienced this and is there a fix? Cheers, Tom Thomas F. (Tom) Massey wrote: > > I'm having some difficulty with powerbuilder's check-in/check-out > operation. When I check an object back in, the .pbl the object is > being moved from is destroyed. > > Has anyone else experienced this and is there a fix? > > Cheers, > > Tom The prob...

Check In/Check out
I am using PowerBuilder 9. The undo check out is right next to the check in. This is a big problem! I so easy to select Undo check out and lose all of your changes. I lost 8 hours of work yesterday because of it. I know I should be more careful. However, there should be a "Are you sure" some where also. PLEASE FIX!!!!!!!!!!!!! -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ This isn't the tech support line, it's an end-user forum. If you want to report a bug, use CaseXpress and either open a case or report a bug ...

Checked=Checked
 I am dynamically writing out checkboxes on a page.  I have 2 link buttons on the page that sets a boolean variable to True or False. This boolen is set in Page_PreInit and is used to Select or Deselect all the Checkboxes as they are written out. A Select ALL, Deselect ALL type of thing. At least that is the plan. As the page is being written out I do this for each checkbox and then add it to a table cell control that it added to a table row and on. tc is the table cell.  Dim chkItem As CheckBox = New CheckBox chkItem.ID = "chkRecommend" + drMember("ID")...

Check in
Hi all. I am migrating from PB 7.0 to PB 10.5, After migrating my first application i am trying to work on my first window but i'am not able to find out the way to CHECK it OUT. Wher do i can find "check out/check in" functions ?? TIA Mario Parietti What source control are you using? Have you configured it? Right click on your Workspace and check the source control tab to see if you've got it set up. "Mario Parietti" <mparietti@intercad.ch> wrote in message news:451283e0$1@forums-1-dub... > Hi all. > I am migrating from PB...

Javascript for validation if checkboxes are checked in web form
I have 26 checkboxes on my form. I want to use javascript to loop thru the controls and check to see if at leat 5 are checked. Any suggestions?? I don't know much about javascript. Thanks in advance!!! Hi Read on all.tag collection of the document. You can get all the "INPUT" objects of your document and check if it is of type "checkbox". To see if the checkbox is checked use the "checked" property of that object. e.g. function countCheckItems() { var items = document.body.all.tags("INPUT"); var i; var iSelec...

Check to see if Checkbox is checked
  Hello All, Currently my application allows user to select the days which they worked and the days are presented in the form of checkboxes which means there are 7 checkboxes for Monday to Sunday. The problem is: I want my users to at least check one box: How do I write the code in VB.NET to ensure that at least one box is checked? I have no totally no idea at all.  Any experts care to shed some light? Thanks!Sincerely Yours, Abupapa hi,Pls go through the link below: This is a very good article.. i think this will meet your requirement.http://codeclimber.net.nz/archive/2007/0...

if checkbox is checked , then all the checkboxlist should be checked
i have a checkbox list and a checkbox , i want f the checkbox is checked then all the checkboxes in the checkboxlist should be checked as well.   Please suggest , if there is any way out   Thanksneelu gupta Do u want this to be done on the client side or server side? And are these check boxes server controls or html controls??Shravan Addaypally MCP maybe this thread can help.> penny for a thought...

How to check checkbox is checked or not dynamically....
Hai friends.... I am creating checkbox dynamically............ dim check(100) as checkbox dim cell as new tablecell check(i)=new checkbox() is there any short method to check whether non of the checkbox is checked........... Ambrose... Help me......Winners Never QuitQuitters Never Win You could do this to check if check boxes are checked If CheckBox1.Checked Then    Message.Text &= "CheckBox 1 was checked<br/>"  End If  If CheckBox2.Checked Then    Message.Text &= "CheckBox 2 was checked<br/>"  End ...

Web resources about - How to Check if Checkboxes are Checked? ^_~ - asp.net.web-forms

Quick Tip Friday: Create Checklists Faster with Auto-Checkboxes
/ We know a lot of Evernote users love checkboxes for to-dos, grocery lists, and more. Creating checkboxes in Evernote is easy: you can add them ...

Group sues Michigan secretary of state over citizenship checkboxes for voters
... rights coalition filed a federal lawsuit today in Detroit challenging Secretary of State Ruth Johnson’s decision to have U.S. citizenship checkboxes ...

Evernote for BlackBerry 10 updated with checkboxes, image and audio attachments
Evernote has updated its recently launched app for the BlackBerry 10 platform with new features, alongside speed improvements and two-step verification. ...

Add checkboxes to an Excel spreadsheet
Tgood37 asked the Answer Line forum how to add checkboxes to Excel spreadsheets, and how to make sure that checking a box will have an effect. ...

Solving Twitter and LinkedIn’s Ad Problem: It’s Time to Ditch the Checkboxes
Last week was a big one for social media giants Twitter and LinkedIn. They admitted to their investors and to the world that their display advertising ...

Simple trick for testing forms full of checkboxes with django
... testing style. What matters is that you have tests. Image via Wikipedia But what do you do when you want to test a form with a bunch of checkboxes? ...

Checkboxes that kill your product
Alexander Limi / Alex Limi : Checkboxes that kill your product — (To read this article with its formatting and typography intact, click here) ...

Evernote for BlackBerry 10 updated with checkboxes, image and audio attachments
... 10 platform with new features, alongside speed improvements and two-step verification. Among the new features is the ability to insert checkboxes ...

Bento for iPad for iPad on the iTunes App Store
Read reviews, get customer ratings, see screenshots, and learn more about Bento for iPad on the App Store. Download Bento for iPad and enjoy ...

Facebook Users in Brazil and India Can Syndicate Their Profiles to Orkut
Facebook users in Brazil and India can now connect their profiles with their account on Orkut , Google’s existing social network. Those who link ...

Resources last updated: 12/30/2015 6:51:12 PM