Hi
I'm having trouble to access a Parent's property in User Control. I don't know how to access the parent. I saw the example below and tried it:

CType(Me.Page, Search).Prop = VALUE

and the Parent page is called Search.aspx

Partial Class Search

But then I get en error Type 'Search' is not defined.

I have also tried:

Dim p as Page=Me.Parent.Page
p.Prop 'doesn't work

But I can't access the Property in this way either

Please help
Fia

I presume that the parent of usercontrol is a Panel then your code in usercontrol might be,

CType(Me.Parent,Panel).GroupingText="Test"

Hi
Thanks for the answer, but no, the User Control is not placed in a panel. And how would I be able to access the panel in the Parent when I even can't access the Parent Page.

Fia

Hi
I found the solution I added

<%@ Reference Page="~/Search.aspx" %>

in the User Control's ascx file and then I could access the Parent's
Property with

Dim parentPage As Search = CType(Page, search)
parentPage.Prop=VALUE

Thanks for the help
Fia

:'( Hi again

No, it didn't work when I tried to run the website. Than it gave me the errors
1. The user control is not declared in the parent page
2. The user contol has a circular reference

Please help me

Fia

Firstly add a code-file into your project which must be a subclass of System.Web.UI.Page class.

MyPage.vb (in app_code folder)

Public MustInherit Class MyPage
    Inherits System.Web.UI.Page
    Public MustOverride Property MyText() As String
End Class

Code & markup of Usercontrol.

TestControl.ascx

<%@ Control Language="VB" ClassName="TestControl" %>

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim method As MyPage = CType(Me.Page, MyPage)
        method.MyText = "Hello"
    End Sub
</script>

Webform - Make sure that the .aspx page directive includes Inherits="MyPage" attribute.

TestPage.aspx

<%@ Page Language="VB" Inherits="MyPage" %>

<%@ Register src="TestControl.ascx" tagname="TestControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Public Overrides Property MyText() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal value As String)
            TextBox1.Text = value
        End Set
    End Property
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc1:TestControl ID="TestControl1" runat="server" />
    
    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>

Hi

Thank's for the answer, but how will I be able to use the other code in the Parent Page if I inherit the Parent to MyPage.

I'am a new to ASP.Net. Sorry

Fia

use this funtion to find the control

Private Shared Function FindControlRecursive(ByVal Root As Control, ByVal Id As String) As Control
        If Root.ID = Id Then
            Return Root
        End If


        For Each Ctl As Control In Root.Controls
            Dim FoundCtl As Control = FindControlRecursive(Ctl, Id)
            If FoundCtl IsNot Nothing Then
                Return FoundCtl
            End If
        Next
        Return Nothing
    End Function

Find the control with the next statement

Dim lbl As Label = FindControlRecursive(Page.Master, "yourControlID")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.