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

Set value of TextBox in UserControl from hosting web form

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.