doctor069
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?
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
ASKER
Thanks ged325 -
when you are loading dynamically
(Dim userControl As UserControl = DirectCast(page.LoadContro l(controlN ame), 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....
when you are loading dynamically
(Dim userControl As UserControl = DirectCast(page.LoadContro
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
ASKER
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?
ASKER
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Hello" & m_UserName
End Sub
Nothing...
Label1.Text = "Hello" & m_UserName
End Sub
Nothing...
Nothing or just "Hello"? How do you call the RenderControl function?
ASKER
Just "Hello"
Its called by
<WebMethod()> _
Public Shared Function Result(ByVal controlName As String) As String
Return RenderControl(controlName)
End Function
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.
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Worth mentioning that the property on the usercontrol must be "public" as in your first sample code(not private like on your interface).
ASKER
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
????
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
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
ASKER
Got it working!!
Thanks for your help
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
Public Property UserName() As String Implements ISection.UserName
Can just use this:
Public Property UserName() As String
private string _Text;
Public Property String myText
{
Get { return _Text;}
Set (value as string)
_Text = value;
}
In your code
UserControl.myText = "something"