473,811 Members | 3,300 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 18165
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
2134
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 set to a, selectable, out-of-focus color.
3
1692
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 occur. I have been attempting to access the public property on the UC, from the page (testCal) shown...
2
3126
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 year field directly on the aspx page.
3
1608
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. Parser Error Message: 'exp.wc' is not a valid base class because it does not extend class...
4
2520
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 syntax, since my page doesnt know the contents of the class (and therefore, the public...
6
1667
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 = HorizontalAlignment.Right endif if Me.type = "C"
1
6875
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 UserControl has one textbox on the control, and one button control. I have added a public property...
5
8732
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 focus to be on the textbox on the usercontrol. This works the first time I display the form. But...
0
983
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
9607
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10652
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10395
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10408
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7673
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6895
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5561
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4346
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
2
3874
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.