Last post Jan 03, 2010 09:53 PM by mbanavige
Dec 27, 2009 12:56 AM|MarcusKo|LINK
I was searching the Internet for quite some hours, but I could not find a solution. I want to access a page variable from a user control placed on that page. All the solutions work in C# via casting to the page class. The problem is that this doesn't work
in VB. The page class is not selectable from the Intellisense dialog. Manual typing results in the error type not defined. It is strange that C# can do this without a problem. What is the proper solution for this in VB? I had other cases where I needed to
access a control on the main page. C# again uses a cast to the page class to access the control. This doesn't work in VB again. So how can I access the page class from a user control in generell?
ASP .net page class user control access variable
Dec 27, 2009 01:50 AM|perryjr84|LINK
Why not use a session variable? I am not familiar with c at all but here is what I know will work (usually in VB):
On the "control" Page:
Dim VariableName AS Type = Control Value '.label text .. textbox.text..etc....
On the other page (or same page for that fact):
This works pretty well if the variable that you are trying to store (or transfer) doesn't need to last indefinitely.
Dec 27, 2009 01:54 AM|MaineOne|LINK
This is what you need to do.
Dim variable =TryCast(Parent.FindControl("DropDownList1"),
Dec 27, 2009 09:43 AM|MarcusKo|LINK
The TryCast example vom MaineOne allows access to a control via the FindControl method, but it doesn't allow access to a variable in the page code. For this I need to do a CType to the page class. The problem is that the page class is not accessable as
a type in VB (but it is in C#!). E.g. in C# I can write in the user control code "=CType(Me.Page.Master, MasterPage).VarName" where MasterPage is the class name of the partial code class. In VB I get an error saying that the Type Masterpage is not defined.
I now used a session variable as a work around, but I still would like to know why the CType to the page class doesn't work in VB. What is missing? There are many posts on the Internet regarding this issue, but I could not find a proper solution.
Dec 27, 2009 10:58 AM|MarcusKo|LINK
I tried to add a reference to theparent page in the user control, but it still doesn't work.
Jan 03, 2010 09:53 PM|mbanavige|LINK
All pages compile into different assemblies. The only classes that are accessible by default from all pages would be those classes that are stored in the special App_Code folder.
Are you trying to get to a property of the Page or of the MasterPage? Your earlier example referred to the masterpage.
You can register the Type of your masterpage in your usercontrol
<%@ Reference Control="~/MasterPage.master" %>
Now that our usercontrol "knows" about the Type of the master, you can cast to it.
DirectCast(Me.Page.Master, MasterPage).SomeCustomProperty = "xyz"
Keep in mind that this kind of setup (where child control are "aware" of their explicit parents) is prone to problems. i.e. If the usercontrol gets dropped onto a page that uses a different masterpage, then the cast will fail at runtime.