471,088 Members | 1,244 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,088 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 17900
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Jonas L | last post: by
2 posts views Thread by Benedict Teoh | last post: by
3 posts views Thread by Lisa Calla | last post: by
4 posts views Thread by louise raisbeck | last post: by
6 posts views Thread by Agnes | last post: by
5 posts views Thread by Tosch | last post: by
reply views Thread by Kevin Attard | last post: by

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.