473,577 Members | 3,256 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Set value of TextBox in UserControl from hosting web form

I have a Web Form with some tailored logos and artwork. The web form
also has a user control that has typical registration info on it
(Name, Company Name, etc.) One of the fields, a TextBox, is a
PromoCode which is a field required for each company which registers.
I want to programatically set the value of this PromoCode in the host
web-form's Page_Load method, but I can't seem to do it.

My user control is "RegisterInfo.a scx" (On the page it is rendered as
"RegisterInfo1" )
My main web page is Register.aspx

In the Register.aspx.c s code-behind file I would like to do something
like:

private void Page_Load(objec t sender, System.EventArg s e)
{
this.RegisterIn fo1.PromoCode.T ext = "PROMOCODE" ;
}

But, of course, it didn't work. So, I tried...

private void Page_Load(objec t sender, System.EventArg s e)
{
RegisterInfo r = new RegisterInfo();
r._promoCode.Te xt = "PROMOCODE" ;
}

And in the user control Register.ascx, I had this:

public string _promoCode
{
get
{return this.PromoCode. Text; // "PromoCode" is the TextBox object.
}
set
{PromoCode.Text = value;
}
}

This complies fine, but I get an "Object reference not set to an
instance of an object." error.

Specifically, how do I set the value of a TextBox in a user control
from the code-behind file? This is not very clear!

Thanks in advance!
Nov 16 '05 #1
2 18150
Try the FindControl() method to identify the child control from the parent.
You can then test for null to make sure you got something back, and cast it
to use member properties. Unless subcontrols of your usercontrol are linked
to properties in the UserControl, you'll have to reference the subcontrols
by reference, not by object navigation.

TextBox nestedTextBox =
thisPage.MyUser ControlInstance .FindControl("F irstname") as TextBox;
if(nestedTextBo x == null) throw new Exception ("Couldn't find the nested
control");
nestedTextBox.T ext = "PROMO";

Hope this helps.
D
"Robbie" <Ro************ @netscape.net> wrote in message
news:17******** *************** ***@posting.goo gle.com...
I have a Web Form with some tailored logos and artwork. The web form
also has a user control that has typical registration info on it
(Name, Company Name, etc.) One of the fields, a TextBox, is a
PromoCode which is a field required for each company which registers.
I want to programatically set the value of this PromoCode in the host
web-form's Page_Load method, but I can't seem to do it.

My user control is "RegisterInfo.a scx" (On the page it is rendered as
"RegisterInfo1" )
My main web page is Register.aspx

In the Register.aspx.c s code-behind file I would like to do something
like:

private void Page_Load(objec t sender, System.EventArg s e)
{
this.RegisterIn fo1.PromoCode.T ext = "PROMOCODE" ;
}

But, of course, it didn't work. So, I tried...

private void Page_Load(objec t sender, System.EventArg s e)
{
RegisterInfo r = new RegisterInfo();
r._promoCode.Te xt = "PROMOCODE" ;
}

And in the user control Register.ascx, I had this:

public string _promoCode
{
get
{return this.PromoCode. Text; // "PromoCode" is the TextBox object.
}
set
{PromoCode.Text = value;
}
}

This complies fine, but I get an "Object reference not set to an
instance of an object." error.

Specifically, how do I set the value of a TextBox in a user control
from the code-behind file? This is not very clear!

Thanks in advance!

Nov 16 '05 #2
Thanks! That did point me in the right direction.

For the benefit of others, here's what I coded in my Page_Load method:

TextBox tbPromoCode =
Page.FindContro l("ucRegister1" ).FindControl(" PromoCode") as TextBox;
tbPromoCode.Tex t = "BLAH";

"ucRegister 1" is my User Control on the web page

Thanks again!
"D. Rush" <dl***********@ gmail.com> wrote in message news:<Da******* *************@a delphia.com>...
Try the FindControl() method to identify the child control from the parent.
You can then test for null to make sure you got something back, and cast it
to use member properties. Unless subcontrols of your usercontrol are linked
to properties in the UserControl, you'll have to reference the subcontrols
by reference, not by object navigation.

TextBox nestedTextBox =
thisPage.MyUser ControlInstance .FindControl("F irstname") as TextBox;
if(nestedTextBo x == null) throw new Exception ("Couldn't find the nested
control");
nestedTextBox.T ext = "PROMO";

Hope this helps.
D

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2110
by: Jonas L | last post by:
Hi, I need to create a textbox which acts as a normal textbox but with the following extra requirements: 1) In-focus color, when the textbox gets focus the backcolor property of the textbox should be set to a, selectable, in-focus color. 2) Out-of-focus color, when the textbox loses focus the backcolor property of the textbox should be...
3
1681
by: Iain | last post by:
Hi I have page (testCal.aspx) that contains a usercontrol (custCalendar.ascx) - see below signature for code. The UC contains a linkbutton which which when clicked, posts back and displays a calendar. When a value in the calendar is selected it posts back and then updates a public property (CurrentDate) on the UC. Note that 2 postbacks...
2
3104
by: Benedict Teoh | last post by:
I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit is performed. How do I force the usercontrol to display the newly assigned date? I don't have this problem if I place the two dropdownlist and the...
3
1584
by: Lisa Calla | last post by:
Hi, I'm trying to control the foreground and background colors of a textbox. I hate that light gray foreground when the textbox.enabled=false. I've tried to derive a control from the textbox, but I get the following error when trying to run a page with the control on it. wc is the name of my class, and exp is the name of my project. ...
4
2501
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once I have exposed a public property containing the value of a textbox in a user control..how do I grab this from the calling page? I cant think of the...
6
1646
by: Agnes | last post by:
I create my own usercontrol (textbox), I new a property 'Type' I will set this property during design mode, I will set 'I' OR 'C', i stands for integer, c stands for character, Now, In that usercontrol, I want to detect that property , if the propety set to "I", the textalign will set to 'right' iF ME.TYPE ='I" Me.textAlign =...
1
6848
by: Will Gillen | last post by:
I know this has probably been asked before, but I can't seem to find a solid answer in any of the archives. First, before my question, please forgive my limited knowledge of the event lifecycle and page loading/rendering lifecycle.... Ok, now for the question: I have an ASPX page (page.aspx), and I have a UserControl (control.ascx). The...
5
8703
by: Tosch | last post by:
I have a usercontrol with a label, a textbox, a treeview, a grid and a couple of checkboxes. The usercontrol is hosted on a form together with a cancel and a accept button. This form is used to search records in a database. On several forms in my application I show (modal) this form to search records. Everytime I show the form I want the...
0
972
by: Kevin Attard | last post by:
I have this form being generated dynamically. The whole form generator is a usercontrol in itself. Some fields are displayed using regular controls eg: Textbox and others use webusercontrols This is the basic cycle: 1. Create controls 2. load data in controls from db (if exist) When the save button is pressed a post back occurs and :
0
7845
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
7776
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
8121
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8286
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
7869
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
8143
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
6517
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
5340
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2287
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.