• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

How can I enable or disable a checkbox control based on athe value of a label control?

I am trying to enable or disable a checkbox control based on a label control's value.  I am currently using the page prerender method to work in.  I have also tried in the pageload without complete success.  I am simply trying to enable the checkbox only when there is a value in the label.  If the label is empty (null in the DB) I want the checkbox to be disabled.  Is there an easier way to do this?  Am I approachin this in the right way?

 I have attached my code below....
If CType(fvTest.FindControl("lbl1"), Label) is nothing Then
            CType(fvCertificate.FindControl("ckbox1"), CheckBox).Enabled = False
        Else
            CType(fvTest.FindControl("ckbox1"), CheckBox).Enabled = True
        End If

Open in new window

0
devnewbee
Asked:
devnewbee
  • 5
  • 5
1 Solution
 
CB_ThirumalaiCommented:
Since the Label and Checkbox are server controls, you just have to refer them by ID as below.

If lbl1.Text.Equals(String.Empty) Then
   ckbox1.Enabled = False
Else
   ckbox1.Enabled = True
End If
0
 
devnewbeeAuthor Commented:
Ok, I gave it a host and here is what I got:
"System.NullReferenceException: Object reference not set to an instance of an object."

What am I missing?

0
 
CB_ThirumalaiCommented:
Please post your code.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
devnewbeeAuthor Commented:
Partial Class test_page
    Inherits System.Web.UI.Page
    Dim lbl1 As Label
    Dim ckbox1 As CheckBox
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If lbl1.Text.Equals(String.Empty) Then
            ckbox1.Enabled = False
        Else
            ckbox1.Enabled = True
        End If
0
 
CB_ThirumalaiCommented:
The below statements
  Dim lbl1 As Label
  Dim ckbox1 As CheckBox
has not been instantiated at all.  This has to be done by the New statement.  There is no meaning to check if the label contains a value immediately after it is declared without even binding it to a source.  What is the actual requirement?
0
 
devnewbeeAuthor Commented:
Well, if I do not declare them, the system lists them as not declared.  The code has underlines underneath unless I do it this way.  The actual requirement is merely just to enable or disable a checkbox control based on a label control's value.  

I added the NEW in there, and it load up OK, but is still not functioning.  If I check the box where there is no label, I get a timestamp (value).  Now, when I come back to the page, I would like to see the checkbox disabled because there is a value.  Hope that helps...

0
 
CB_ThirumalaiCommented:
I think this is what you have.  A page with a <asp:Label and a <asp:Checkbox control in the ASPX page.  As soon as you click the checkbox and on post back you need to disable that (not sure of this).  But, how do you say that you have the value in Label without postback?
Sorry, still not clear
0
 
devnewbeeAuthor Commented:
I think we are on the same page.  I use getdate() to get the timestamp when the checkbox is clicked the first time.  Then, we I reenter the record, the checkbox is still enabled.  I would like the checkbox to be disabled because the date now exists.  Thanks for helping here...
0
 
devnewbeeAuthor Commented:
The date does not show until after I renter the record aftre being sent back to the grid view listing of records.
0
 
CB_ThirumalaiCommented:
devnewbee: Sorry for late reply as I am in the other part of the world and it is early morning here.

So, if you the the Label and Checkbox controls on the ASPX page, then can post those two tags here?
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now