Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. get whether a checkbox is checked or not ASP.net

get whether a checkbox is checked or not ASP.net

Scheduled Pinned Locked Moved C#
questioncsharpasp-netdesign
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DeepToot
    wrote on last edited by
    #1

    Ok, this seems basic I know but for some reason it's giving me troubles. I have a checkbox on an asp.net page that gets checked...however, there is no postback going to happen on a button click. My question is how can I get the value of the checkbox without a postback? I created an event and wired it to my continue button, once fired it does the validation. The client didnt like the fact that the page posted back when the checkbox was clicked (I was saving what was checked on postback). So I created this event. I am able to get the control and everything, but it's telling me the Checked if false, when I know I checked it. Here is the code I use to Iterate through the controls to find the checkbox: Public Function IterateControls(ByVal parent As Control) As String Dim id As String = "" For Each child As Control In parent.Controls If child.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox") Then Dim clientid As Integer = child.ClientID.IndexOf("chk") Dim cid As String = child.ClientID Dim chk As CheckBox = CType(child, CheckBox) '************************************** 'THIS RETURNS FALSE EVEN IF CHECKED '************************************** If chk.Checked = True Then 'get id of this control If id = "" Then id = chk.ID Else 'already populated 'they checked one too many 'now warn them Dim lblwarn As Label = CType(Page.FindControl("lblWarning"), Label) lblwarn.Text = "You can only select ONE (1) Asset at a time!" lblwarn.ForeColor = Color.Red chk.Checked = False Return "-1" End If End If End If If child.HasControls Then IterateControls(child) End If Next Return id End Function Anyone have any clue if I am able to capture a value even though there is no postback happening? VB or C# examples would be appreciated. Thanks Steve Welborn Software Engineer BitWise Solutions

    M 1 Reply Last reply
    0
    • D DeepToot

      Ok, this seems basic I know but for some reason it's giving me troubles. I have a checkbox on an asp.net page that gets checked...however, there is no postback going to happen on a button click. My question is how can I get the value of the checkbox without a postback? I created an event and wired it to my continue button, once fired it does the validation. The client didnt like the fact that the page posted back when the checkbox was clicked (I was saving what was checked on postback). So I created this event. I am able to get the control and everything, but it's telling me the Checked if false, when I know I checked it. Here is the code I use to Iterate through the controls to find the checkbox: Public Function IterateControls(ByVal parent As Control) As String Dim id As String = "" For Each child As Control In parent.Controls If child.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox") Then Dim clientid As Integer = child.ClientID.IndexOf("chk") Dim cid As String = child.ClientID Dim chk As CheckBox = CType(child, CheckBox) '************************************** 'THIS RETURNS FALSE EVEN IF CHECKED '************************************** If chk.Checked = True Then 'get id of this control If id = "" Then id = chk.ID Else 'already populated 'they checked one too many 'now warn them Dim lblwarn As Label = CType(Page.FindControl("lblWarning"), Label) lblwarn.Text = "You can only select ONE (1) Asset at a time!" lblwarn.ForeColor = Color.Red chk.Checked = False Return "-1" End If End If End If If child.HasControls Then IterateControls(child) End If Next Return id End Function Anyone have any clue if I am able to capture a value even though there is no postback happening? VB or C# examples would be appreciated. Thanks Steve Welborn Software Engineer BitWise Solutions

      M Offline
      M Offline
      maryamf
      wrote on last edited by
      #2

      i can't understand your question clearly but i think your anwser is : you must first set Autopostback property of the checkbox to the true and then work with checkedchanged event like this code: protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (CheckBox1.Checked) Response.Write("checked"); else Response.Write("unchecked"); }

      D 1 Reply Last reply
      0
      • M maryamf

        i can't understand your question clearly but i think your anwser is : you must first set Autopostback property of the checkbox to the true and then work with checkedchanged event like this code: protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { if (CheckBox1.Checked) Response.Write("checked"); else Response.Write("unchecked"); }

        D Offline
        D Offline
        DeepToot
        wrote on last edited by
        #3

        Thanks for the time and reply maryamf. The problem with this is that I cant have it postback when the user clicks the checkbox. All validation will happen when the client clicks the 'continue' button. I had it working perfectly with the autopostback, however they thought it was distracting having it refresh on each click. And I agree if there are on dialup or something. So my question was mainly in the validation of the page, via the continue button. I have to Iterate through the controls and find the checked one. However, this needs to be done without postback when user clicks checkboxs. Anyway to get the values of the controls this way? Hope that made sense. Steve Welborn Software Engineer BitWise Solutions

        L 1 Reply Last reply
        0
        • D DeepToot

          Thanks for the time and reply maryamf. The problem with this is that I cant have it postback when the user clicks the checkbox. All validation will happen when the client clicks the 'continue' button. I had it working perfectly with the autopostback, however they thought it was distracting having it refresh on each click. And I agree if there are on dialup or something. So my question was mainly in the validation of the page, via the continue button. I have to Iterate through the controls and find the checked one. However, this needs to be done without postback when user clicks checkboxs. Anyway to get the values of the controls this way? Hope that made sense. Steve Welborn Software Engineer BitWise Solutions

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Javascript, and go ask in the ASP.NET forum :)**

          How xacc.ide transforms text to colored words on the screen
          Intel PentuimM (aka Centrino) undervolting

          **

          D 1 Reply Last reply
          0
          • L leppie

            Javascript, and go ask in the ASP.NET forum :)**

            How xacc.ide transforms text to colored words on the screen
            Intel PentuimM (aka Centrino) undervolting

            **

            D Offline
            D Offline
            DeepToot
            wrote on last edited by
            #5

            thanks for the reply leppie, unfortunatly javascript wont do it either. This needs to be server side because the values I get from the id of the checked checkbox will be used to gather the data on the next page. I guess to ask it a different way: Can I get the correct value of a checkbox without posting back? What needs to change in the code above in order for this to happen? I can't set the AutoPostBack to true because of the refresh. This event I created takes the refresh away but now I cannot grab the value with the current code. Come to think of it, I might just forget about doing it the event way and just do all validation in the Continue buttons click event...not sure what client would say but I know it works. Thanks again leppie for the time. Steve Welborn Software Engineer BitWise Solutions

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups