locked
How to access a page variable from a user control placed on that page from VB? RRS feed

  • Question

  • User1351886979 posted

    Hi,

    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?

    Thanks Marcus

    Saturday, December 26, 2009 8:56 PM

Answers

  • User-158764254 posted

    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.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 3, 2010 5:53 PM

All replies

  • User139366633 posted

    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....

    Session.Add("VariableName", ControlValue)

    On the other page (or same page for that fact):

    NewControlValue.text=Session("VariableName")

    This works pretty well if the variable that you are trying to store (or transfer) doesn't need to last indefinitely.

    Happy Holidays.

     

    Saturday, December 26, 2009 9:50 PM
  • User2069888697 posted

    This is what you need to do.

    Dim variable =TryCast(Parent.FindControl("DropDownList1"), DropDownList).SelectedValue

    Saturday, December 26, 2009 9:54 PM
  • User1351886979 posted

    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.

    Marcus

    Sunday, December 27, 2009 5:43 AM
  • User1351886979 posted

    I tried to add a reference to theparent page in the user control, but it still doesn't work.

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

    Sunday, December 27, 2009 6:58 AM
  • User-158764254 posted

    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.

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 3, 2010 5:53 PM