get whether a checkbox is checked or not ASP.net
-
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 -
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 Solutionsi 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"); }
-
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"); }
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
-
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
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**
-
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**
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