User Control inside User Control

I have an ASPX with user controls in it called UC1.
There is another user control called UC2 within user control UC1.(UC1 - parent , UC2 - child)

What I need to do is to pass value of the text box that’s located inside user control UC1 to UC2.

This is the code I put into  user control UC1. How to I read it in User control UC2?

 

   Public ReadOnly Property propCountryName() As TextBox
        Get
            Return Me.txtCountrygName
        End Get
    End Property
 

 Thank you


100 years live, 100 years learn
0
Access
6/1/2007 6:29:26 AM
πŸ“ asp.net.getting-started
πŸ“ƒ 91979 articles.
⭐ 3 followers.

πŸ’¬ 7 Replies
πŸ‘οΈβ€πŸ—¨οΈ 676 Views

You can define a property inside the UC1 and UC2. Page will fill in the property inside UC1 and then UC1 will fill in the property inside UC2.

Does this help?
Regards


Bilal Hadiar, MCP, MCTS, MCPD, MCT
Microsoft MVP - Telerik MVP
0
haidar_bilal
6/1/2007 1:24:28 PM

I defined property in UC1(in my original post) , but i don't get how to define it in UC2?

Please help.

 Thank you

 


100 years live, 100 years learn
0
Access
6/1/2007 2:09:03 PM

Hi,

You could use the Parent property of the child control to access the parent User Control: 

MyParentUC parentUc = this.Parent as MyParentUC; //just a type cast, in case you don't know C#
string myValue = parentUC.MyStringProperty;

 But a better way would be to expose a read/write property on the Child User Control (uc2), and set its value when the value is accessed inside the property on UC1. This is suggestion given above I believe.

Rich


Please mark any responses as Answers if they helped you solve your problem
0
rrmarriott
6/1/2007 4:21:52 PM

Thanks for reply.

I'm doing it in ASP.NET

In UC1(parent) i have:

Public ReadOnly Property propDrugName() As TextBox
        Get Return Me.txtDrugName
        End Get
    End Property

 

In UC2(child)  i have:  

1    Private parentUc As UC1 = CType(Me.Parent, UC1)
2    Private myValue As String = parentUc.propCountryName .Text
3    
 
 But but it gives me an error on the second line saying: System.NullReferenceException: Object reference not set to an instance of an object
 Thank you

 


100 years live, 100 years learn
0
Access
6/1/2007 5:56:36 PM

You don't need to do any casting in here!

Define a property on the inside UC, then add an instance of the inside UC inside the main UC, then place main UC inside the page.

Now when you are in the main UC you can access the property inside the inside UC as UC2.PropertName and inside the page you can access UC1.PropertryName.

Does this help?
Regards


Bilal Hadiar, MCP, MCTS, MCPD, MCT
Microsoft MVP - Telerik MVP
0
haidar_bilal
6/3/2007 8:16:11 PM

 


100 years live, 100 years learn
0
Access
6/4/2007 5:04:18 AM
Thanks for reply, I’m sure it will work what you suggested, but I’ve got a help from another forum member with less code to write:  

Dim tb As TextBox = CType(Parent.FindControl("TextBox1"), TextBox)

   it has to be placed in UC1. Thanks again.

 


100 years live, 100 years learn
0
Access
6/4/2007 5:05:25 AM
Reply: