Link to home
Start Free TrialLog in
Avatar of doctor069
doctor069Flag for Canada

asked on

Passing parameters to dynamically loaded userControl

Hi I am loading user controls onto a page through jquery (ajax). Please see code below.

Every works are required but I now have to pass parameters to the user control.

I have no idea how to do this...

Any Suggestions?


<WebMethod()> _
        Public Shared Function Result(ByVal controlName As String) As String
            Return RenderControl(controlName)
        End Function

        Public Shared Function RenderControl(ByVal controlName As String) As String
            Dim page As New Page()

            Dim userControl As UserControl = DirectCast(page.LoadControl(controlName), UserControl)
            userControl.EnableViewState = False
            Dim form As New HtmlForm()
            form.Controls.Add(userControl)
            page.Controls.Add(form)

            Dim textWriter As New StringWriter()
            HttpContext.Current.Server.Execute(page, textWriter, False)
            Return textWriter.ToString()
        End Function

Open in new window

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Just create a Public property in the usercontrol

private string _Text;

Public Property String myText
{
   Get { return _Text;}
   Set (value as string)
     _Text = value;
}


In your code

UserControl.myText = "something"
Avatar of doctor069

ASKER

Thanks ged325 -

when you are loading dynamically
(Dim userControl As UserControl = DirectCast(page.LoadControl(controlName), UserControl))
 then you could not use UserControl.myText = "something"

I Think....

I have tried to implement a Public Interface but still an having no luck

Please See Code Below....


***************INTERFACE***********************
Namespace Demo.WithInterfaces
    Public Interface ISection
        Property UserName() As String
    End Interface
End Namespace

************FUNCTION ASPX Page*******************
 Public Shared Function RenderControl(ByVal controlName As String) As String

        Dim page As New Page()

        Dim userControl As UserControl = page.LoadControl(controlName)

        Dim ucontrol As ISection = DirectCast(userControl, ISection)
        If ucontrol IsNot Nothing Then
            ucontrol.UserName = "henry"
            Dim form As New HtmlForm()
            form.Controls.Add(ucontrol)
            page.Controls.Add(form)
            Dim textWriter As New StringWriter()
            HttpContext.Current.Server.Execute(page, textWriter, False)
            Return textWriter.ToString()
        Else
            Return "NO Control"
        End If 
    End Function

************USER CONTROL*******************

Partial Public Class uhpmm
    Inherits System.Web.UI.UserControl
    Implements Demo.WithInterfaces.ISection

    Public Property UserName() As String Implements ISection.UserName
        Get
            Return m_UserName
        End Get
        Set(ByVal value As String)
            m_UserName = value
        End Set
    End Property
    Private m_UserName As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        m_UserName = UserName
        Response.Write("Hello " & m_UserName)
    End Sub

End Class

Open in new window

On the above the control loads - I get "Hello" but I don't get "henry"
Does it work if you pass the value of UserName to some control such as label within the user control?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = "Hello" & m_UserName
    End Sub

Nothing...
Nothing or just "Hello"? How do you call the RenderControl function?
Just "Hello"

Its called by

 <WebMethod()> _
    Public Shared Function Result(ByVal controlName As String) As String
        Return RenderControl(controlName)
    End Function
I think page_load of the user control is firing with the Page.LoadControl so by the time you assign it the UserName, its already executed that code. Try placing the code in user_control.render event.
Still Only "Hello" ....
Imports Demo.WithInterfaces


Partial Public Class uhpmm
    Inherits System.Web.UI.UserControl
    Implements Demo.WithInterfaces.ISection

    Public Property UserName() As String Implements ISection.UserName
        Get
            Return m_UserName
        End Get
        Set(ByVal value As String)
            m_UserName = value
        End Set
    End Property
    Private m_UserName As String
    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Label1.Text = "Hello" & m_UserName
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_2_4913559
Member_2_4913559
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worth mentioning that the property on the usercontrol must be "public" as in your first sample code(not private like on your interface).
ddayx10 - looking at that now

Could you tell me how you call it on the control end

 Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        Label1.Text = "Hello" & m_UserName
    End Sub

????
I may not understand the question. You want to call a property of a usercontrol from a webmethod right?

You want to call the method, load a usercontrol on a page(created dynamically), assign a property value of the usercontrol, then render the whole control back to the page via ajax??

If you have a property on the usercontrol and you assign its value via reflection then you should be able to use it on the usercontrol like any other property.

Your sample updated slightly:

    Private m_UserName As String

    Public Property UserName() As String Implements ISection.UserName
        Get
            Return m_UserName
        End Get
        Set(ByVal value As String)
            m_UserName = value
        End Set
    End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        m_UserName = UserName
        Response.Write("Hello " & Me.UserName)
End Sub

Got it working!!

Thanks for your help
I should have said you dont need this:
    Public Property UserName() As String Implements ISection.UserName

Can just use this:
    Public Property UserName() As String