Passing values to a custom control

Discussion in 'ASP .Net Building Controls' started by Chris Kennedy, May 9, 2004.

  1. I am building a custom composite control which reads in information from an
    XML file. I want to be able to point my control at different XML files for
    maximum reusablity. How can I pass a parameter to my component. I am a bit
    new to this but ideally would like the rest of my team to be able to use
    this component in their projects and just tell the component which XML file
    to point at.
    Chris Kennedy, May 9, 2004
    #1
    1. Advertising

  2. Through Properties. Basically you have a private variable defined in your
    control, such as

    private string _Location = "";

    Now, you create get/set accessors like so:
    public string Location
    {
    get
    {
    return _Location;
    }
    set
    {
    _Location = value;
    }
    }

    That will set it up so that you now have a publically available property
    called Location that will be abe to have a value passed to it, and retrieved
    from it. Now, the only other thing you'll have to do is make use of this
    value. The problem is, you don't want to use the Page_Load event of the
    control. That happens at the wrong time in the scheme of things. The
    Page_Load of the a sub-control will be loaded before the Page_Load of the
    parent page. That means any value you set the control to in the parent page
    won't be useful to you in the Page_Load event of the sub-control. You can
    override the PreRender instead and place your code that makes use of the
    _Location value there instead.

    Hope this helps,
    Mark Fitzpatrick
    Microsoft MVP - FrontPage


    "Chris Kennedy" <> wrote in message
    news:...
    > I am building a custom composite control which reads in information from

    an
    > XML file. I want to be able to point my control at different XML files for
    > maximum reusablity. How can I pass a parameter to my component. I am a bit
    > new to this but ideally would like the rest of my team to be able to use
    > this component in their projects and just tell the component which XML

    file
    > to point at.
    >
    >
    Mark Fitzpatrick, May 9, 2004
    #2
    1. Advertising

  3. Chris, Mark,

    >>> The Page_Load of the a sub-control will be
    >>> loaded before the Page_Load of the parent page.

    This is actually not correct. The Load event for the Page will fire before
    the Load event of any of its children.

    >>> That happens at the wrong time in the scheme of things

    Hummm... maybe thats your impression, I dont see anything wrong with that
    order :)

    Also chances are that you may want to use viewstate instead of a private
    member field as the storage for your property so it maintains state between
    postbacks.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    "Mark Fitzpatrick" <> wrote in message
    news:%...
    > Through Properties. Basically you have a private variable defined in your
    > control, such as
    >
    > private string _Location = "";
    >
    > Now, you create get/set accessors like so:
    > public string Location
    > {
    > get
    > {
    > return _Location;
    > }
    > set
    > {
    > _Location = value;
    > }
    > }
    >
    > That will set it up so that you now have a publically available property
    > called Location that will be abe to have a value passed to it, and

    retrieved
    > from it. Now, the only other thing you'll have to do is make use of this
    > value. The problem is, you don't want to use the Page_Load event of the
    > control. That happens at the wrong time in the scheme of things. The
    > Page_Load of the a sub-control will be loaded before the Page_Load of the
    > parent page. That means any value you set the control to in the parent

    page
    > won't be useful to you in the Page_Load event of the sub-control. You can
    > override the PreRender instead and place your code that makes use of the
    > _Location value there instead.
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Microsoft MVP - FrontPage
    >
    >
    > "Chris Kennedy" <> wrote in message
    > news:...
    > > I am building a custom composite control which reads in information from

    > an
    > > XML file. I want to be able to point my control at different XML files

    for
    > > maximum reusablity. How can I pass a parameter to my component. I am a

    bit
    > > new to this but ideally would like the rest of my team to be able to use
    > > this component in their projects and just tell the component which XML

    > file
    > > to point at.
    > >
    > >

    >
    >
    Victor Garcia Aprea [MVP], May 9, 2004
    #3
  4. That has pointed me in the right direction but the problem I am having now
    is the control doesn't seem to want to use the string. To clarify, what I am
    doing is using the XML file to tell the control dynamically what controls to
    put on the page so it is called in the CreateChildControls() event. I am
    setting my location property in the onLoad event of the parent page. I am a
    little new to this and find the timing of events a little confusing when
    compared the classic ASP. Regards.


    "Victor Garcia Aprea [MVP]" <> wrote in message
    news:...
    > Chris, Mark,
    >
    > >>> The Page_Load of the a sub-control will be
    > >>> loaded before the Page_Load of the parent page.

    > This is actually not correct. The Load event for the Page will fire before
    > the Load event of any of its children.
    >
    > >>> That happens at the wrong time in the scheme of things

    > Hummm... maybe thats your impression, I dont see anything wrong with that
    > order :)
    >
    > Also chances are that you may want to use viewstate instead of a private
    > member field as the storage for your property so it maintains state

    between
    > postbacks.
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    >
    > "Mark Fitzpatrick" <> wrote in message
    > news:%...
    > > Through Properties. Basically you have a private variable defined in

    your
    > > control, such as
    > >
    > > private string _Location = "";
    > >
    > > Now, you create get/set accessors like so:
    > > public string Location
    > > {
    > > get
    > > {
    > > return _Location;
    > > }
    > > set
    > > {
    > > _Location = value;
    > > }
    > > }
    > >
    > > That will set it up so that you now have a publically available property
    > > called Location that will be abe to have a value passed to it, and

    > retrieved
    > > from it. Now, the only other thing you'll have to do is make use of this
    > > value. The problem is, you don't want to use the Page_Load event of the
    > > control. That happens at the wrong time in the scheme of things. The
    > > Page_Load of the a sub-control will be loaded before the Page_Load of

    the
    > > parent page. That means any value you set the control to in the parent

    > page
    > > won't be useful to you in the Page_Load event of the sub-control. You

    can
    > > override the PreRender instead and place your code that makes use of the
    > > _Location value there instead.
    > >
    > > Hope this helps,
    > > Mark Fitzpatrick
    > > Microsoft MVP - FrontPage
    > >
    > >
    > > "Chris Kennedy" <> wrote in message
    > > news:...
    > > > I am building a custom composite control which reads in information

    from
    > > an
    > > > XML file. I want to be able to point my control at different XML files

    > for
    > > > maximum reusablity. How can I pass a parameter to my component. I am a

    > bit
    > > > new to this but ideally would like the rest of my team to be able to

    use
    > > > this component in their projects and just tell the component which XML

    > > file
    > > > to point at.
    > > >
    > > >

    > >
    > >

    >
    >
    Chris Kennedy, May 10, 2004
    #4
  5. Oh I thought I'd better mention the error I get is CompControl1.Location =
    "C:\Inetpub\wwwroot\AVTest\XMLFile1.xml"
    The path is not of a legal form.


    "Victor Garcia Aprea [MVP]" <> wrote in message
    news:...
    > Chris, Mark,
    >
    > >>> The Page_Load of the a sub-control will be
    > >>> loaded before the Page_Load of the parent page.

    > This is actually not correct. The Load event for the Page will fire before
    > the Load event of any of its children.
    >
    > >>> That happens at the wrong time in the scheme of things

    > Hummm... maybe thats your impression, I dont see anything wrong with that
    > order :)
    >
    > Also chances are that you may want to use viewstate instead of a private
    > member field as the storage for your property so it maintains state

    between
    > postbacks.
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    >
    > "Mark Fitzpatrick" <> wrote in message
    > news:%...
    > > Through Properties. Basically you have a private variable defined in

    your
    > > control, such as
    > >
    > > private string _Location = "";
    > >
    > > Now, you create get/set accessors like so:
    > > public string Location
    > > {
    > > get
    > > {
    > > return _Location;
    > > }
    > > set
    > > {
    > > _Location = value;
    > > }
    > > }
    > >
    > > That will set it up so that you now have a publically available property
    > > called Location that will be abe to have a value passed to it, and

    > retrieved
    > > from it. Now, the only other thing you'll have to do is make use of this
    > > value. The problem is, you don't want to use the Page_Load event of the
    > > control. That happens at the wrong time in the scheme of things. The
    > > Page_Load of the a sub-control will be loaded before the Page_Load of

    the
    > > parent page. That means any value you set the control to in the parent

    > page
    > > won't be useful to you in the Page_Load event of the sub-control. You

    can
    > > override the PreRender instead and place your code that makes use of the
    > > _Location value there instead.
    > >
    > > Hope this helps,
    > > Mark Fitzpatrick
    > > Microsoft MVP - FrontPage
    > >
    > >
    > > "Chris Kennedy" <> wrote in message
    > > news:...
    > > > I am building a custom composite control which reads in information

    from
    > > an
    > > > XML file. I want to be able to point my control at different XML files

    > for
    > > > maximum reusablity. How can I pass a parameter to my component. I am a

    > bit
    > > > new to this but ideally would like the rest of my team to be able to

    use
    > > > this component in their projects and just tell the component which XML

    > > file
    > > > to point at.
    > > >
    > > >

    > >
    > >

    >
    >
    Chris Kennedy, May 10, 2004
    #5
  6. Chris Kennedy

    Sanket Naik Guest

    Hi,
    Maybe u can try this stuff
    "C:\\Inetpub\\wwwroot\\AVTest\\XMLFile1.xml"
    If this does not work
    the other way is
    Server.MapPath(Path of the xml file)

    Cheers,
    Sanket Naik



    C:\Inetpub\wwwroot\AVTest\XMLFile1.xml
    "Chris Kennedy" wrote:

    > Oh I thought I'd better mention the error I get is CompControl1.Location =
    > "C:\Inetpub\wwwroot\AVTest\XMLFile1.xml"
    > The path is not of a legal form.
    >
    >
    > "Victor Garcia Aprea [MVP]" <> wrote in message
    > news:...
    > > Chris, Mark,
    > >
    > > >>> The Page_Load of the a sub-control will be
    > > >>> loaded before the Page_Load of the parent page.

    > > This is actually not correct. The Load event for the Page will fire before
    > > the Load event of any of its children.
    > >
    > > >>> That happens at the wrong time in the scheme of things

    > > Hummm... maybe thats your impression, I dont see anything wrong with that
    > > order :)
    > >
    > > Also chances are that you may want to use viewstate instead of a private
    > > member field as the storage for your property so it maintains state

    > between
    > > postbacks.
    > >
    > > --
    > > Victor Garcia Aprea
    > > Microsoft MVP | ASP.NET
    > > Looking for insights on ASP.NET? Read my blog:
    > > http://obies.com/vga/blog.aspx
    > > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    > >
    > > "Mark Fitzpatrick" <> wrote in message
    > > news:%...
    > > > Through Properties. Basically you have a private variable defined in

    > your
    > > > control, such as
    > > >
    > > > private string _Location = "";
    > > >
    > > > Now, you create get/set accessors like so:
    > > > public string Location
    > > > {
    > > > get
    > > > {
    > > > return _Location;
    > > > }
    > > > set
    > > > {
    > > > _Location = value;
    > > > }
    > > > }
    > > >
    > > > That will set it up so that you now have a publically available property
    > > > called Location that will be abe to have a value passed to it, and

    > > retrieved
    > > > from it. Now, the only other thing you'll have to do is make use of this
    > > > value. The problem is, you don't want to use the Page_Load event of the
    > > > control. That happens at the wrong time in the scheme of things. The
    > > > Page_Load of the a sub-control will be loaded before the Page_Load of

    > the
    > > > parent page. That means any value you set the control to in the parent

    > > page
    > > > won't be useful to you in the Page_Load event of the sub-control. You

    > can
    > > > override the PreRender instead and place your code that makes use of the
    > > > _Location value there instead.
    > > >
    > > > Hope this helps,
    > > > Mark Fitzpatrick
    > > > Microsoft MVP - FrontPage
    > > >
    > > >
    > > > "Chris Kennedy" <> wrote in message
    > > > news:...
    > > > > I am building a custom composite control which reads in information

    > from
    > > > an
    > > > > XML file. I want to be able to point my control at different XML files

    > > for
    > > > > maximum reusablity. How can I pass a parameter to my component. I am a

    > > bit
    > > > > new to this but ideally would like the rest of my team to be able to

    > use
    > > > > this component in their projects and just tell the component which XML
    > > > file
    > > > > to point at.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    >
    Sanket Naik, Jun 26, 2004
    #6
    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. adam
    Replies:
    0
    Views:
    2,561
  2. Matt Sokol
    Replies:
    2
    Views:
    365
    Theo Tillotson
    Aug 7, 2003
  3. Arulraja

    Passing values between Custom controls?

    Arulraja, Oct 21, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    150
    Victor Garcia Aprea [MVP]
    Oct 23, 2003
  4. Suresh Kumar

    Accessing values of one customer Control from another custom control

    Suresh Kumar, Nov 3, 2003, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    177
    Suresh Kumar
    Nov 4, 2003
  5. Suresh Kumar
    Replies:
    4
    Views:
    184
    Suresh Kumar
    Nov 4, 2003
Loading...

Share This Page