Home All Groups Group Topic Archive Search About

Get Value of a control from my Own Control

Author
6 Apr 2005 7:23 PM
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.

Author
6 Apr 2005 7:34 PM
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quote
> 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.
>
Author
6 Apr 2005 7:57 PM
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


Show quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:388304632483984315595968@msnews.microsoft.com...
> 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.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > 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.
> >
>
>
>
Author
6 Apr 2005 9:41 PM
Brock Allen
So make a property on your control:

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

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quote
> 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
>
> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> news:388304632483984315595968@msnews.microsoft.com...
>
>> 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.
>>
>> -Brock
>> DevelopMentor
>> http://staff.develop.com/ballen
>>> 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.
>>>
Author
7 Apr 2005 12:40 PM
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.

Show quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:389931632484060837028384@msnews.microsoft.com...
> So make a property on your control:
>
> public string Text
> {
> get
> {
>    EnsureChildControls();
>    return _myText.Text;
> }
> set
> {
>    EnsureChildControls();
>    _myText.Text = value;
> }
> }
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > 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
> >
> > "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> > news:388304632483984315595968@msnews.microsoft.com...
> >
> >> 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.
> >>
> >> -Brock
> >> DevelopMentor
> >> http://staff.develop.com/ballen
> >>> 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.
> >>>
>
>
>
Author
7 Apr 2005 4:30 PM
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.


-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quote
> Thanks, and the last question:
> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> news:389931632484060837028384@msnews.microsoft.com...
>
>> So make a property on your control:
>>
>> public string Text
>> {
>> get
>> {
>> EnsureChildControls();
>> return _myText.Text;
>> }
>> set
>> {
>> EnsureChildControls();
>> _myText.Text = value;
>> }
>> }
>> -Brock
>> DevelopMentor
>> http://staff.develop.com/ballen
>>> 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
>>>
>>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
>>> news:388304632483984315595968@msnews.microsoft.com...
>>>
>>>> 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.
>>>>
>>>> -Brock
>>>> DevelopMentor
>>>> http://staff.develop.com/ballen
>>>>> 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.

AddThis Social Bookmark Button