473,545 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change Label ForeColor When TextBox Value Changes

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_T extChanged(ByVa l sender As Object, ByVal
e As System.EventArg s) Handles txtCaseNumber.T extChanged

ChangeLabelColo r(sender)

End Sub

Private Sub ChangeLabelColo r(ByVal TextBox)
Dim labelID As String = TextBox.ID.repl ace("txt", "lbl")
Dim ctrl As New Control
Dim ctrlNameText As Label

For Each ctrl In Page.FindContro l("aspnetForm") .Controls
If TypeOf (ctrl) Is Label And (ctrl.ID.ToStri ng.Trim =
labelID.Trim) Then
ctrlNameText = ctrl
ctrlNameText.Fo reColor = Drawing.Color.R ed
End If
Next

End Sub
The code bombs out on the For Each ctrl statement.
In debug mode, I'm receiving the following messages:
NullReferenceEx ception 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
3 4846
Page.FindContro l("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_T extChanged(ByVa l sender As Object, ByVal
e As System.EventArg s) Handles txtCaseNumber.T extChanged

ChangeLabelColo r(sender)

End Sub

Private Sub ChangeLabelColo r(ByVal TextBox)
Dim labelID As String = TextBox.ID.repl ace("txt", "lbl")
Dim ctrl As New Control
Dim ctrlNameText As Label

For Each ctrl In Page.FindContro l("aspnetForm") .Controls
If TypeOf (ctrl) Is Label And (ctrl.ID.ToStri ng.Trim =
labelID.Trim) Then
ctrlNameText = ctrl
ctrlNameText.Fo reColor = Drawing.Color.R ed
End If
Next

End Sub
The code bombs out on the For Each ctrl statement.
In debug mode, I'm receiving the following messages:
NullReferenceEx ception 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
On Oct 31, 10:36 am, bruce barker wrote:
Page.FindContro l("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.FindContro l("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.Find Control("lblCas eNumber"),
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
On Oct 31, 10:36 am, bruce barker wrote:
Page.FindContro l("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.FindContro l("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.Find Control("lblCas eNumber"),
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1824
by: beanweed | last post by:
BACKGROUND ---------- I have an ASP.NET application having two panels. In one panel, an XML document, transformed using xsl, is displayed. In the other panel are some controls that allow a user to change the xml. For example, each "l_item" element appears as a row in a table labelled with a "label"; so if I have <l_item id="1">...
6
4475
by: Mantorok | last post by:
Hi all Is it possible to disable a Control in .Net but have the text colour always in black? I would rather use Enabled property rather than Readonly as it does everything I need it to in one swoop (no tabstop, no focus etc.). Any ideas?
0
1604
by: Jeff User | last post by:
I have my datagrid running pretty much with a EditCommandColumn and some TemplateColumn columns. There are two types of appearance changes I would like to make programatically from my C# code depending on various conditions. Column items are typical Labels in <ItemTemplate> and TextBoxes in <EditItemTemplate> forms. When the user clicks...
9
33759
by: iwdu15 | last post by:
hi, is there any way (vb.net 2003 or 2005) to change the progress bar color from blue to any other system color? im trying to change between 3 colors depending on a variable in my app. unfortunatly i didnt see any property or method to do this so i was wondering if i was missing something or if il have to write my own. Thanks -- -iwdu15
3
3279
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when the field should only be showing one or the other (date or time). Even on the back end of the database where the column properties are, I have...
1
12210
by: cyningeston | last post by:
OS: WinXP Pro, VB/ASP/ADO.NET I'm building a web-based supplier management application. For each supplier we are required by the FDA to track certain documents. I've managed to pull them from Access into a Gridview (Gridview1), in which I have AutoGenerateEditButton set to True. What I want is this: when a user clicks "Edit" on a line and it...
7
11775
by: garyusenet | last post by:
I'm using krypton toolkit which has allowed me to make a cool looking form. However, when I set my textbox to disabled it is 'greyed' out. The grey colour isn't in keeping with the office 2007 style look of my form. How can i change the colour that control assumed when it's disabled? Thanks! Gary-
3
5175
by: rn5a | last post by:
How do I change the BorderColor of the rows under the 2nd column in a DetailsView? I could change the BorderColor of the 1st column using the FieldHeaderStyle-BorderColor property but using RowStyle-BorderColor, the BorderColor of the 2nd column just refuses to change. I even tried using AlternatingRowStyle-BorderColor but that doesn't...
1
2592
by: dhaneshrs | last post by:
I have a small code that shows inactive and active users from the ms access DB. <%@ Page Language="VB" MasterPageFile="~/MasterPageAdmin.master" Title="Welcome" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <%@ Import Namespace="System.Drawing" %> <script language="VB" runat="server"> Public...
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.