We help IT Professionals succeed at work.

Passing parameters to dynamically loaded userControl

doctor069
doctor069 asked
on
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

Comment
Watch Question

Kyle AbrahamsSenior .Net Developer

Commented:
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"

Author

Commented:
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

Author

Commented:
On the above the control loads - I get "Hello" but I don't get "henry"
Most Valuable Expert 2012
Top Expert 2014

Commented:
Does it work if you pass the value of UserName to some control such as label within the user control?

Author

Commented:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label1.Text = "Hello" & m_UserName
    End Sub

Nothing...
Most Valuable Expert 2012
Top Expert 2014

Commented:
Nothing or just "Hello"? How do you call the RenderControl function?

Author

Commented:
Just "Hello"

Its called by

 <WebMethod()> _
    Public Shared Function Result(ByVal controlName As String) As String
        Return RenderControl(controlName)
    End Function
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
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

Commented:
I haven't thought alot about using an interface for this situation. I don't think it will work out in the end. It would be fine if you were trying to do some inter-page communication but here I think you're going to run into a dead end.

Anyway I was doing something similar a few years ago and I remember being pretty proud of myself when I figured out a way to use reflection to do this. If there is a better way I'd love to see it but for now you should be able to do this:

See Ex.
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 ucType As Type = ucontrol.GetType
            Dim ucUserName As System.Reflection.PropertyInfo = ucType.GetProperty("UserName")
            ucUserName.SetValue(ucontrol, "Users Name", Nothing)
            Dim form As New HtmlForm()
            form.Controls.Add(ucontrol)
            page.Controls.Add(form)
            Dim textWriter As New System.IO.StringWriter()
            HttpContext.Current.Server.Execute(page, textWriter, False)
            Return textWriter.ToString()
        Else
            Return "NO Control"
        End If
    End Function

Open in new window

Commented:
Worth mentioning that the property on the usercontrol must be "public" as in your first sample code(not private like on your interface).

Author

Commented:
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

????

Commented:
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

Author

Commented:
Got it working!!

Thanks for your help

Commented:
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