passing variables to a user control

Discussion in 'ASP .Net' started by =?Utf-8?B?RGFuIE5hc2g=?=, Oct 7, 2004.

  1. Hi

    I'm new to C#, moving from ASP, and slightly confused so bear with me!

    Basically I've got 4 pages, each of which runs the same user control (some
    header information). I want to be able to change the data in the user control
    based on the page (everything else would be duplicated, hence the user
    control).

    Anyway, i thought the easiest way would be to use a variable set in the
    Page_load event of the calling page. So I've got a variable defined in my
    page class as..

    public int pageID = 1;

    However, when I try to access that from the user control, it says the
    variable is not defined in the namespace. What am i missing!?!

    Alternatively, I could use a URL object to get the page were on directly in
    the usercontrol, but I couldnt find a URL method/property to give me just the
    file name.

    Hope all that makes sense!

    Cheers


    Dan
     
    =?Utf-8?B?RGFuIE5hc2g=?=, Oct 7, 2004
    #1
    1. Advertising

  2. You should try to put a public property on your user control. In your html
    code set your property.

    <uc1:UserControl id="MyControl" runat="server"
    MyPublicProp="1"></uc1:UserControl>

    If you want to do this from code, first, you should declare your user
    control in your page because visual studion didn't do this by default.

    it should look like this
    protected MyPath.To.Control.ControlName MyControl;
    after this you can acces your control properties normally..

    MyControl.MyPublicProp ="1";

    Cheers
    "Dan Nash" wrote:

    > Hi
    >
    > I'm new to C#, moving from ASP, and slightly confused so bear with me!
    >
    > Basically I've got 4 pages, each of which runs the same user control (some
    > header information). I want to be able to change the data in the user control
    > based on the page (everything else would be duplicated, hence the user
    > control).
    >
    > Anyway, i thought the easiest way would be to use a variable set in the
    > Page_load event of the calling page. So I've got a variable defined in my
    > page class as..
    >
    > public int pageID = 1;
    >
    > However, when I try to access that from the user control, it says the
    > variable is not defined in the namespace. What am i missing!?!
    >
    > Alternatively, I could use a URL object to get the page were on directly in
    > the usercontrol, but I couldnt find a URL method/property to give me just the
    > file name.
    >
    > Hope all that makes sense!
    >
    > Cheers
    >
    >
    > Dan
    >
    >
     
    =?Utf-8?B?UHN5Y2hv?=, Oct 7, 2004
    #2
    1. Advertising

  3. Hi,

    Create an interface. eg:

    public interface ICommonPage
    {
    int PageID{get;}
    }

    then implement it in the classes that contain the usercontrol in question
    like this:

    public class page1 : System.Web.UI.Page, ICommonPage
    {
    //Hardcode the value in a field:
    int _PageID = 1; // or 2 in page2, 3 in page3, etc.

    // The actual implementation:
    public ICommonPage.PageID
    {
    get{return _PageID;}
    }

    // the rest of the class as it is...
    }

    And finally in the usercontrol cast the Page property to the interface and
    get the value:

    int PageID = ((ICommonPage)Page).PageID;

    // work with PageID...


    Hope this helps
    Martin
    "Dan Nash" <> wrote in message
    news:...
    > Hi
    >
    > I'm new to C#, moving from ASP, and slightly confused so bear with me!
    >
    > Basically I've got 4 pages, each of which runs the same user control (some
    > header information). I want to be able to change the data in the user

    control
    > based on the page (everything else would be duplicated, hence the user
    > control).
    >
    > Anyway, i thought the easiest way would be to use a variable set in the
    > Page_load event of the calling page. So I've got a variable defined in my
    > page class as..
    >
    > public int pageID = 1;
    >
    > However, when I try to access that from the user control, it says the
    > variable is not defined in the namespace. What am i missing!?!
    >
    > Alternatively, I could use a URL object to get the page were on directly

    in
    > the usercontrol, but I couldnt find a URL method/property to give me just

    the
    > file name.
    >
    > Hope all that makes sense!
    >
    > Cheers
    >
    >
    > Dan
    >
    >
     
    Martin Dechev, Oct 7, 2004
    #3
  4. Thanks guys!

    Psychos post solved it, I guess you would say, the easy way! However,
    Martin's solution looks interesting.. might try that one when I know a bit
    more about C# (only got it Friday!).

    Would there be any advantage with ICommonPage over the second method that
    Psycho suggested?

    Cheers guys


    Dan

    "Dan Nash" wrote:

    > Hi
    >
    > I'm new to C#, moving from ASP, and slightly confused so bear with me!
    >
    > Basically I've got 4 pages, each of which runs the same user control (some
    > header information). I want to be able to change the data in the user control
    > based on the page (everything else would be duplicated, hence the user
    > control).
    >
    > Anyway, i thought the easiest way would be to use a variable set in the
    > Page_load event of the calling page. So I've got a variable defined in my
    > page class as..
    >
    > public int pageID = 1;
    >
    > However, when I try to access that from the user control, it says the
    > variable is not defined in the namespace. What am i missing!?!
    >
    > Alternatively, I could use a URL object to get the page were on directly in
    > the usercontrol, but I couldnt find a URL method/property to give me just the
    > file name.
    >
    > Hope all that makes sense!
    >
    > Cheers
    >
    >
    > Dan
    >
    >
     
    =?Utf-8?B?RGFuIE5hc2g=?=, Oct 7, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Poole
    Replies:
    3
    Views:
    538
    Jim Corey
    Sep 23, 2004
  2. Mad Scientist Jr
    Replies:
    0
    Views:
    641
    Mad Scientist Jr
    Mar 22, 2006
  3. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Replies:
    4
    Views:
    551
    Jonathan Wood
    Feb 2, 2010
  4. darrel

    passing custom variables TO a user control

    darrel, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    124
    darrel
    Mar 2, 2004
  5. hmikki
    Replies:
    1
    Views:
    144
    Nathan Sokalski
    Oct 19, 2007
Loading...

Share This Page