473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.ascx" (On the page it is rendered as
"RegisterInfo1")
My main web page is Register.aspx

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

private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterInfo1.PromoCode.Text = "PROMOCODE";
}

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

private void Page_Load(object sender, System.EventArgs e)
{
RegisterInfo r = new RegisterInfo();
r._promoCode.Text = "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 18130
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.MyUserControlInstance.FindControl("Firstn ame") as TextBox;
if(nestedTextBox == null) throw new Exception ("Couldn't find the nested
control");
nestedTextBox.Text = "PROMO";

Hope this helps.
D
"Robbie" <Ro************@netscape.net> wrote in message
news:17**************************@posting.google.c om...
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.ascx" (On the page it is rendered as
"RegisterInfo1")
My main web page is Register.aspx

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

private void Page_Load(object sender, System.EventArgs e)
{
this.RegisterInfo1.PromoCode.Text = "PROMOCODE";
}

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

private void Page_Load(object sender, System.EventArgs e)
{
RegisterInfo r = new RegisterInfo();
r._promoCode.Text = "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.FindControl("ucRegister1").FindControl("Promo Code") as TextBox;
tbPromoCode.Text = "BLAH";

"ucRegister1" is my User Control on the web page

Thanks again!
"D. Rush" <dl***********@gmail.com> wrote in message news:<Da********************@adelphia.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.MyUserControlInstance.FindControl("Firstn ame") as TextBox;
if(nestedTextBox == null) throw new Exception ("Couldn't find the nested
control");
nestedTextBox.Text = "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
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...
3
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...
2
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...
3
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,...
4
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...
6
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...
1
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...
5
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...
0
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 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.