Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a content page , on the content page i am using a user control . I need to add User control from code behind and pass the value in same time. Any help

Following i am adding user control to my aspx page but i don't know how to pass value to user control.

NOTE:: I can do this from HTML but i need to do from code behind


HTML
Dim MainContent As ContentPlaceHolder = TryCast(Master.FindControl("MainContent"), ContentPlaceHolder)
 Dim _usercontrol As UserControl = CType(Page.LoadControl("~\Usercontrols\dashboard_Employer.ascx"), Control)
MainContent.Controls.Add(_usercontrol)


I have property set in User Control Called ValueID. I need to pass this ID from code behind.

VB
Public Property ValueID() As Integer
       Get
           Return _ValueID
       End Get
       Set(ByVal value As Integer)
           _ValueID = value
       End Set
   End Property
Posted
Updated 15-May-12 9:44am
v4

You could always add the value as an attribute

VB
Dim MainContent As ContentPlaceHolder = TryCast(Master.FindControl("MainContent"), ContentPlaceHolder)
 Dim _usercontrol As UserControl = CType(Page.LoadControl("~\Usercontrols\dashboard_Employer.ascx"), Control)
MainContent.Controls.Add(_usercontrol)

_usercontrol.Attributes.Add("Value", "1")


In your user control you could set

VB
Public Property ValueID() As Integer
        Get
            Return If(Me.Attributes("ValueID").Length < 1, 0, CType(a.Attributes("ValueID"), Integer))
        End Get
        Set(ByVal value As Integer)
            Me.Attributes.Add("ValueID", value.ToString())
        End Set
    End Property
 
Share this answer
 
Comments
anjumnavid 29-May-12 10:51am    
Thanks MugiwareUK ,I solved this problem but your solution is way better than mine. Thanks again.
i don't know to pass value to user control.
Expose get-set properties in your user control and use them.
 
Share this answer
 
Comments
anjumnavid 15-May-12 15:27pm    
i have property set in my user control .

I just need help to calling and passing to user control from parent page code behind
Sandeep Mewara 15-May-12 15:36pm    
What about: _usercontrol.ValueID = 25 ?
anjumnavid 15-May-12 15:43pm    
This doesn't work.
Because i am not assigning my user control from asp page. what i am doing is :

Finding and adding my usercontrol to my parent page as follwing:

Dim MainContent As ContentPlaceHolder = TryCast(Master.FindControl("MainContent"), ContentPlaceHolder)

MainContent.Controls.Add(_usercontrol)


Next step is to pass value as you said up. but _usercontrol.valueID= 25 . Gives error
Sandeep Mewara 16-May-12 1:21am    
Pretty strange way to use Usercontrol. Finding from exisiting masterpage and adding to some content holder. Hope you know what you are doing.

For the above, Once you find the control, cast it into Usercontrol type, your public property will be exposed. You can set the value then.
preet88 16-May-12 1:16am    
see you can access your control properties using the control object
if in case you are finding that control so do make a reference of that control and then access properties of control using that reference.
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900