Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > dynamic user control textbox is empty when accessing from aspx pag

Reply
Thread Tools

dynamic user control textbox is empty when accessing from aspx pag

 
 
randman
Guest
Posts: n/a
 
      07-22-2005
I have created an ASPX page that has a placeholder that dynamically gets
assigned various user controls (that I have also created) during runtime. On
one of the user controls contains a textbox. I need to access the ascx page
textbox value from a sub routine in the aspx page.

This is the code that I have that returns the value of the textbox (named
txtName)

Public Property UserName() As String
Get
Return txtName.Text
End Get
Set(ByVal Value As String)
txtName.Text = Value.ToString
End Set
End Property

When I call this from my aspx page nothing is returned even though I have
typed something into the txtName textbox.

If I assign a value to the textbox, as in the following example, then the
value ("Hello") does appear in the aspx page that calls this code.

Public Property UserName() As String
Get
txtName.Text = "Hello"
Return txtName.Text
End Get
Set(ByVal Value As String)
txtName.Text = Value.ToString
End Set
End Property


The user control is called ranman.ascx

This is the code that dynamically assigns the user control to the aspx page

Session("strBody") = ranman
strControlBody = "controls\" & CStr(Session("strBody")) & ".ascx"
ctlBody = LoadControl(strControlBody)
phBody.Controls.Add(ctlBody)


This is the code on the aspx page that calls the user control

Dim myUserControl As ranman = CType(LoadControl("controls\ranman.ascx"),
ranman)

strUserName = myUserControl.UserName


It seems like a timing issue, but I could be wrong
I'm not even sure if I am on the right path with this. Please help !!

Thanks in advance,
Randman

 
Reply With Quote
 
 
 
 
Harolds
Guest
Posts: n/a
 
      07-25-2005
Your code creates a new control, so you are not trying to get the text box
value from the control in which you entered the data, but you are getting the
text box value from a brand new control of the same type.

I am not sure how to remedy your problem.
Try recreating your control in LoadViewState override, then in the postback
method do:
Dim myUserControl As ranman = FindControl(ControlID)
strUserName = myUserControl.UserName

Now what the controlid is I am not sure you will have to figure that out for
yourself.

That fact that you are using a dynamically created user control is what
makes this complicated.



"randman" wrote:

> I have created an ASPX page that has a placeholder that dynamically gets
> assigned various user controls (that I have also created) during runtime. On
> one of the user controls contains a textbox. I need to access the ascx page
> textbox value from a sub routine in the aspx page.
>
> This is the code that I have that returns the value of the textbox (named
> txtName)
>
> Public Property UserName() As String
> Get
> Return txtName.Text
> End Get
> Set(ByVal Value As String)
> txtName.Text = Value.ToString
> End Set
> End Property
>
> When I call this from my aspx page nothing is returned even though I have
> typed something into the txtName textbox.
>
> If I assign a value to the textbox, as in the following example, then the
> value ("Hello") does appear in the aspx page that calls this code.
>
> Public Property UserName() As String
> Get
> txtName.Text = "Hello"
> Return txtName.Text
> End Get
> Set(ByVal Value As String)
> txtName.Text = Value.ToString
> End Set
> End Property
>
>
> The user control is called ranman.ascx
>
> This is the code that dynamically assigns the user control to the aspx page
>
> Session("strBody") = ranman
> strControlBody = "controls\" & CStr(Session("strBody")) & ".ascx"
> ctlBody = LoadControl(strControlBody)
> phBody.Controls.Add(ctlBody)
>
>
> This is the code on the aspx page that calls the user control
>
> Dim myUserControl As ranman = CType(LoadControl("controls\ranman.ascx"),
> ranman)
>
> strUserName = myUserControl.UserName
>
>
> It seems like a timing issue, but I could be wrong
> I'm not even sure if I am on the right path with this. Please help !!
>
> Thanks in advance,
> Randman
>

 
Reply With Quote
 
 
 
 
randman
Guest
Posts: n/a
 
      07-26-2005
Thanks for your reply, your explanation has really helped me understand what
is taking place. I will investigate your suggestion of recreating the
control in then "LoadViewState override".

randman



"Harolds" wrote:

> Your code creates a new control, so you are not trying to get the text box
> value from the control in which you entered the data, but you are getting the
> text box value from a brand new control of the same type.
>
> I am not sure how to remedy your problem.
> Try recreating your control in LoadViewState override, then in the postback
> method do:
> Dim myUserControl As ranman = FindControl(ControlID)
> strUserName = myUserControl.UserName
>
> Now what the controlid is I am not sure you will have to figure that out for
> yourself.
>
> That fact that you are using a dynamically created user control is what
> makes this complicated.
>
>
>
> "randman" wrote:
>
> > I have created an ASPX page that has a placeholder that dynamically gets
> > assigned various user controls (that I have also created) during runtime. On
> > one of the user controls contains a textbox. I need to access the ascx page
> > textbox value from a sub routine in the aspx page.
> >
> > This is the code that I have that returns the value of the textbox (named
> > txtName)
> >
> > Public Property UserName() As String
> > Get
> > Return txtName.Text
> > End Get
> > Set(ByVal Value As String)
> > txtName.Text = Value.ToString
> > End Set
> > End Property
> >
> > When I call this from my aspx page nothing is returned even though I have
> > typed something into the txtName textbox.
> >
> > If I assign a value to the textbox, as in the following example, then the
> > value ("Hello") does appear in the aspx page that calls this code.
> >
> > Public Property UserName() As String
> > Get
> > txtName.Text = "Hello"
> > Return txtName.Text
> > End Get
> > Set(ByVal Value As String)
> > txtName.Text = Value.ToString
> > End Set
> > End Property
> >
> >
> > The user control is called ranman.ascx
> >
> > This is the code that dynamically assigns the user control to the aspx page
> >
> > Session("strBody") = ranman
> > strControlBody = "controls\" & CStr(Session("strBody")) & ".ascx"
> > ctlBody = LoadControl(strControlBody)
> > phBody.Controls.Add(ctlBody)
> >
> >
> > This is the code on the aspx page that calls the user control
> >
> > Dim myUserControl As ranman = CType(LoadControl("controls\ranman.ascx"),
> > ranman)
> >
> > strUserName = myUserControl.UserName
> >
> >
> > It seems like a timing issue, but I could be wrong
> > I'm not even sure if I am on the right path with this. Please help !!
> >
> > Thanks in advance,
> > Randman
> >

 
Reply With Quote
 
sam
Guest
Posts: n/a
 
      07-26-2005
Hi there,

Make sure you are adding the user control in the Init() method of the
aspx page. Then you should be able to get the text value in the Load()
method of the aspx page.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make any request for a noexistent subdirectory go to a default pag codeboy ASP .Net 2 12-09-2009 05:02 PM
Embedded Resource File in Code Library called from ASP.Net web pag Jay Pondy ASP .Net 1 02-16-2009 08:02 PM
Any quick solution to display performance diagnostics on every pag Phil Johnson ASP .Net 1 01-31-2008 10:16 AM
Erro no start pag tarcisio ASP .Net 1 12-28-2005 01:24 PM
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57