By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,682 Members | 1,999 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,682 IT Pros & Developers. It's quick & easy.

Change Label ForeColor When TextBox Value Changes

P: n/a
Hi Everyone,

I have a web form that I would like to enhance. What I'd like to do
is this:
A user adds/edits the text in a textbox. I want the adjoining label to
change color.

Example:
User edits text in txtCaseNumber. The cursor leaves txtCaseNumber.
Code executes and turns lblCaseNumber text from black to red.

Here is what I've come up with so far:

Protected Sub txtCaseNumber_TextChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles txtCaseNumber.TextChanged

ChangeLabelColor(sender)

End Sub

Private Sub ChangeLabelColor(ByVal TextBox)
Dim labelID As String = TextBox.ID.replace("txt", "lbl")
Dim ctrl As New Control
Dim ctrlNameText As Label

For Each ctrl In Page.FindControl("aspnetForm").Controls
If TypeOf (ctrl) Is Label And (ctrl.ID.ToString.Trim =
labelID.Trim) Then
ctrlNameText = ctrl
ctrlNameText.ForeColor = Drawing.Color.Red
End If
Next

End Sub
The code bombs out on the For Each ctrl statement.
In debug mode, I'm receiving the following messages:
NullReferenceException was unhandled by user code.
Object reference not set to an instance of an object.

Can anyone offer some suggestions?

Thanks,

CR Junk
Oct 31 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Page.FindControl("aspnetForm") is returning null, so accessing .Controls
from it thows an error. either there is no Control named "aspnetForm", or it
not an immediate child of the Page.

if label is a sibling of the textbox, then just do a FindControl by name. to
find it, the label must have a runat="server", so you could just access it by
name.

the onchanged will not fire until a postback, so you must set AutoPostBack =
true, will will then cause a postback on th onblur client event.

this easily done in javascript without requiring a postback, and give a much
better experince to you users.


-- bruce (sqlwork.com)
"crjunk" wrote:
Hi Everyone,

I have a web form that I would like to enhance. What I'd like to do
is this:
A user adds/edits the text in a textbox. I want the adjoining label to
change color.

Example:
User edits text in txtCaseNumber. The cursor leaves txtCaseNumber.
Code executes and turns lblCaseNumber text from black to red.

Here is what I've come up with so far:

Protected Sub txtCaseNumber_TextChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles txtCaseNumber.TextChanged

ChangeLabelColor(sender)

End Sub

Private Sub ChangeLabelColor(ByVal TextBox)
Dim labelID As String = TextBox.ID.replace("txt", "lbl")
Dim ctrl As New Control
Dim ctrlNameText As Label

For Each ctrl In Page.FindControl("aspnetForm").Controls
If TypeOf (ctrl) Is Label And (ctrl.ID.ToString.Trim =
labelID.Trim) Then
ctrlNameText = ctrl
ctrlNameText.ForeColor = Drawing.Color.Red
End If
Next

End Sub
The code bombs out on the For Each ctrl statement.
In debug mode, I'm receiving the following messages:
NullReferenceException was unhandled by user code.
Object reference not set to an instance of an object.

Can anyone offer some suggestions?

Thanks,

CR Junk
Oct 31 '08 #2

P: n/a
On Oct 31, 10:36 am, bruce barker wrote:
Page.FindControl("aspnetForm") is returning null, so accessing .Controls
from it thows an error. either there is no Control named "aspnetForm", or it
not an immediate child of the Page.

I have a question about this. Originally, I had
Page.FindControl("form1") instead of aspnetForm. The MasterPage that
I am using has "form1" listed in the html source, but when I run my
default.aspx web form and look at the source code through my browser,
it has "aspnetForm" listed. Which form name should I be using?

if label is a sibling of the textbox, then just do a FindControl by name. to
find it, the label must have a runat="server", so you could just access it by
name.

I commented out my For statement, verified lblCaseNumber had
runat="server" and tried the following:

Dim lbl As Label = CType(Page.FindControl("lblCaseNumber"),
Label)
lbl.Text = "HELLO WORLD"

but I am still receiving: "Object Reference not set to an instance of
an object". Is this the proper way to use the FindControl stament?

the onchanged will not fire until a postback, so you must set AutoPostBack =
true, will will then cause a postback on th onblur client event.

I verified that AutoPostBack = True for txtCaseNumber

this easily done in javascript without requiring a postback, and give a much
better experince to you users.
Normally, I would try doing this ne javascript, but I'm using this
code as somewhat of a test so that I can expand it to do a lot of
other formatting, validating, etc....

Thanks for your help!
CR Junk

Oct 31 '08 #3

P: n/a
On Oct 31, 10:36 am, bruce barker wrote:
Page.FindControl("aspnetForm") is returning null, so accessing .Controls
from it thows an error. either there is no Control named "aspnetForm", or it
not an immediate child of the Page.

I have a question about this. Originally, I had
Page.FindControl("form1") instead of aspnetForm. The MasterPage that
I am using has "form1" listed in the html source, but when I run my
default.aspx web form and look at the source code through my browser,
it has "aspnetForm" listed. Which form name should I be using?

if label is a sibling of the textbox, then just do a FindControl by name. to
find it, the label must have a runat="server", so you could just access it by
name.

I commented out my For statement, verified lblCaseNumber had
runat="server" and tried the following:

Dim lbl As Label = CType(Page.FindControl("lblCaseNumber"),
Label)
lbl.Text = "HELLO WORLD"

but I am still receiving: "Object Reference not set to an instance of
an object". Is this the proper way to use the FindControl stament?

the onchanged will not fire until a postback, so you must set AutoPostBack =
true, will will then cause a postback on th onblur client event.

I verified that AutoPostBack = True for txtCaseNumber

this easily done in javascript without requiring a postback, and give a much
better experince to you users.
Normally, I would try doing this ne javascript, but I'm using this
code as somewhat of a test so that I can expand it to do a lot of
other formatting, validating, etc....

Thanks for your help!
CR Junk

Oct 31 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.