dynamic user control textbox is empty when accessing from aspx pag

Discussion in 'ASP .Net Web Controls' started by randman, Jul 22, 2005.

  1. randman

    randman Guest

    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
     
    randman, Jul 22, 2005
    #1
    1. Advertisements

  2. randman

    Harolds Guest

    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
    >
     
    Harolds, Jul 25, 2005
    #2
    1. Advertisements

  3. randman

    randman Guest

    RE: dynamic user control textbox is empty when accessing from aspx

    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
    > >
     
    randman, Jul 26, 2005
    #3
  4. randman

    sam Guest

    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.
     
    sam, Jul 26, 2005
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Thunell
    Replies:
    3
    Views:
    3,628
    Edward
    Jul 21, 2004
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    906
    Mad Scientist Jr
    Mar 22, 2006
  3. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    813
    Jonathan Wood
    Feb 2, 2010
  4. Elroyskimms

    Accessing Textbox Text After Postback in Dynamic Control

    Elroyskimms, Feb 20, 2006, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    673
    Jose Rodriguez
    Feb 22, 2006
  5. Dave
    Replies:
    19
    Views:
    499
    David A. Coursey
    Jan 14, 2005
  6. Jason
    Replies:
    0
    Views:
    196
    Jason
    Oct 6, 2005
  7. Kiran More
    Replies:
    2
    Views:
    527
    Kiran More
    Nov 14, 2006
  8. Savas Ates
    Replies:
    1
    Views:
    464
    Patrice
    Aug 17, 2004
Loading...