Access parent page's control from a user control

liunx

Guest
I have a .ascx user control that contains a button. I place this user control on a .aspx web page. The web page contains a textbox. When I click on the button in the user control I want the data in the textbox (in the web page) to be saved to the database. I want to write this code in the click event of the user control's button. Is there any way I can access the parent web page's controls from the user control?Yes, there is a way.

Dim obj As Object
Dim obj1 As Object
Dim strText As String

For Each obj In Me.Page.Controls
strText = obj.UniqueId
If strName = "Form1" Then
obj1 = obj.FindControl("txtData")
If Not obj1 Is Nothing Then
strName = obj1.text()
Exit For
End If
End If
Next


Where "Form1" is the name of the form of your .aspx page, and txtData is your textbox on that page.
You write this code in the event handler of the button on your user control.

Good Luck!

Happy New Year!Every control has a parent property that you can use to access the parent.You are right! :cool:Originally posted by mattyblah
Every control has a parent property that you can use to access the parent. And this relates to the fact that asp.net is fully object oriented and inheritance is one property of object oriented programming.Thanks all of you!
 
Back
Top