ASP.NET ASCX Use of Instance Variable

ascx, asp.net, instance-variables

Solution

You could use the `Page` property of the user control and cast it to the instance of your page. Then, call the method of your page class to get the user name.

To make this work in a dynamically compiled project, you have to do a little more work to have the control recognize the data type of the dynamically compiled page. Here is a short tutorial on how to do.

Or, as Brandon outlines, do the opposite and let your page tell your user control the information.

Problem

Let's say I have an ASCX user control that requires access to the current user's full name. An ASPX page contains this line at the top ``` <%@ Register src="top.ascx" tagprefix="custom" tagname="top" %> ``` and this line in the body: ``` <custom:top runat="server" /> ``` The ASPX file knows the user ID of the current user and could determine his full name. So how can I use the code run by the ASPX file to provide its information to the ASCX file?

Original source