Get Value of a control from my Own Control

D

David

Hello,
i have this code:

public class MyControl :System.Web.UI.Control, INamingContainer
{
protected override void CreateChildControls()
{
TextBox myTextBox = new TextBox();
myTextBox.ID = "txtMyTextBox";
this.Controls.Add(myTextBox);
}
}

When the page postback how can i do to get the text of myTextBox?

Thanks.
 
B

Brock Allen

Handle your control's own Load event or override your OnLoad method. In there,
simply access myTextBox.Text. Oh, this will also require you to move the
declaration of mytextBox to be a field of your class.
 
D

David

If I handle the Load event first is raised the Load of the Page and then of
the Control. I want to get value of the TextBox and set tu a property from
my control because I want to read the Property of the control when the
Page_Load is raised.

Thanks
 
B

Brock Allen

So make a property on your control:

public string Text
{
get
{
EnsureChildControls();
return _myText.Text;
}
set
{
EnsureChildControls();
_myText.Text = value;
}
}
 
D

David

Thanks, and the last question:
There's no way to get the text of the control when CreateChildControl method
is running, because i create another control that depend from the value of
myTextBox.
 
B

Brock Allen

There's no way to get the text of the control when CreateChildControl
method
is running, because i create another control that depend from the
value of
myTextBox.

I don't quite understand what you're asking. But typically if you need to
dynamically create controls you need to store some extra state to know/remember
how to recreate everything on your page. If you need to dynamically change
what's on your page based upon something a user enters, then handle the event
for the control that tse user is changing and do your dynamic work there.
So if the user changes a DropDownList and that means you add a new control
to the form, you should do this in the DropDownList's SelectedIndexChange
event.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top