Newbie: Custom Server Controls and Codebehind

Discussion in 'ASP .Net Web Controls' started by Wardeaux, Jan 15, 2004.

  1. Wardeaux

    Wardeaux Guest

    Help! I'm developing a custom server control and want to get/set properties
    in my codebehind. I have the control set within the HTML page correctly and
    it loads/displays correctly, but when I try to compile my aspx.vb code, it
    complains that my variable name is undeclared. Is there a good article or
    sample I can use to master this? I don't need Postback, but I do want the
    properties avail during other controls postbacks. ANY assist is most
    appreciated!
    wardeaux
    Wardeaux, Jan 15, 2004
    #1
    1. Advertising

  2. The web is stateless, so if you wanted properties to persist your going to
    have to save it into viewstate, or a session variable. Viewstate is your
    best bet coz it will save the values into the page and then retrieve them
    after postback. This is how webserver controls maintain state.
    In a get/set accessors scenario the following is correct. If you are unable
    to compile and it says a variable is not declared this has nothing to do
    with your values not being persisted but that you are using an undeclared
    variable. So declare the variable before using it ;P In the following
    example I have text declared as string --this is how you need to do it ;P
    get/set accessors are useful if you wanted to pass values to another
    page --plus there are some extra steps you need to take in the receiving
    page etc
    Look in the docs, its explained in detail.

    Public Property Text() As String
    Get
    Return CType(ViewState("Text"), String)
    End Get
    Set
    ViewState("Text") = value
    End Set
    End Property
    "Wardeaux" <> wrote in message
    news:...
    > Help! I'm developing a custom server control and want to get/set

    properties
    > in my codebehind. I have the control set within the HTML page correctly

    and
    > it loads/displays correctly, but when I try to compile my aspx.vb code, it
    > complains that my variable name is undeclared. Is there a good article or
    > sample I can use to master this? I don't need Postback, but I do want the
    > properties avail during other controls postbacks. ANY assist is most
    > appreciated!
    > wardeaux
    >
    >
    Alessandro Zifiglio, Jan 15, 2004
    #2
    1. Advertising

  3. oops, custom server control. My mistake. The example i just showed you is
    good. I though you were using the get/set accessors in your page and that
    didnt make much sense to me ;P
    Please ignore where i said "get/set accessors are useful if you wanted to
    pass values to another page --plus there are some extra steps you need to
    take in the receiving page etc Look in the docs, its explained in detail"

    Look at the link for more details on how to maintain state in your web
    custom control ;)
    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp

    "Wardeaux" <> wrote in message
    news:...
    > Help! I'm developing a custom server control and want to get/set

    properties
    > in my codebehind. I have the control set within the HTML page correctly

    and
    > it loads/displays correctly, but when I try to compile my aspx.vb code, it
    > complains that my variable name is undeclared. Is there a good article or
    > sample I can use to master this? I don't need Postback, but I do want the
    > properties avail during other controls postbacks. ANY assist is most
    > appreciated!
    > wardeaux
    >
    >
    Alessandro Zifiglio, Jan 15, 2004
    #3
  4. Re-reading my post, ignore this too : "If you are unable to compile and it
    says a variable is not declared this has nothing to do with your values not
    being persisted but that you are using an undeclared variable. So declare
    the variable before using it ;P"


    "Alessandro Zifiglio" <> wrote in
    message news:W_BNb.3605$...
    > oops, custom server control. My mistake. The example i just showed you is
    > good. I though you were using the get/set accessors in your page and that
    > didnt make much sense to me ;P
    > Please ignore where i said "get/set accessors are useful if you wanted to
    > pass values to another page --plus there are some extra steps you need to
    > take in the receiving page etc Look in the docs, its explained in detail"
    >
    > Look at the link for more details on how to maintain state in your web
    > custom control ;)
    >

    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
    >
    > "Wardeaux" <> wrote in message
    > news:...
    > > Help! I'm developing a custom server control and want to get/set

    > properties
    > > in my codebehind. I have the control set within the HTML page correctly

    > and
    > > it loads/displays correctly, but when I try to compile my aspx.vb code,

    it
    > > complains that my variable name is undeclared. Is there a good article

    or
    > > sample I can use to master this? I don't need Postback, but I do want

    the
    > > properties avail during other controls postbacks. ANY assist is most
    > > appreciated!
    > > wardeaux
    > >
    > >

    >
    >
    Alessandro Zifiglio, Jan 15, 2004
    #4
  5. aaaZZzz .. . .Ignore this too : "or a session variable"
    "Alessandro Zifiglio" <> wrote in
    message news:50CNb.3606$...
    > Re-reading my post, ignore this too : "If you are unable to compile and it
    > says a variable is not declared this has nothing to do with your values

    not
    > being persisted but that you are using an undeclared variable. So declare
    > the variable before using it ;P"
    >
    >
    > "Alessandro Zifiglio" <> wrote in
    > message news:W_BNb.3605$...
    > > oops, custom server control. My mistake. The example i just showed you

    is
    > > good. I though you were using the get/set accessors in your page and

    that
    > > didnt make much sense to me ;P
    > > Please ignore where i said "get/set accessors are useful if you wanted

    to
    > > pass values to another page --plus there are some extra steps you need

    to
    > > take in the receiving page etc Look in the docs, its explained in

    detail"
    > >
    > > Look at the link for more details on how to maintain state in your web
    > > custom control ;)
    > >

    >

    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
    > >
    > > "Wardeaux" <> wrote in message
    > > news:...
    > > > Help! I'm developing a custom server control and want to get/set

    > > properties
    > > > in my codebehind. I have the control set within the HTML page

    correctly
    > > and
    > > > it loads/displays correctly, but when I try to compile my aspx.vb

    code,
    > it
    > > > complains that my variable name is undeclared. Is there a good

    article
    > or
    > > > sample I can use to master this? I don't need Postback, but I do want

    > the
    > > > properties avail during other controls postbacks. ANY assist is most
    > > > appreciated!
    > > > wardeaux
    > > >
    > > >

    > >
    > >

    >
    >
    Alessandro Zifiglio, Jan 15, 2004
    #5
  6. Wardeaux

    Wardeaux Guest

    I appreciate your replies, but I think you missed my question...
    I have a custom control. I want to set properties and get values from my
    custom control in my Codebehind. I place the control on my HTML page
    (MyPage.aspx). I name the control MyControl1. Now I go the codebehind
    MyPage.aspx.vb and in the PageLoad sub I want to load MyControl1 with data,
    but when I type:
    "...
    MyControl1.MyProperty = "Hello world!"
    ...."
    I get a compile-time error stating that MyControl1 is an undeclared
    variable...
    How can I get a handle to the custom control object so that I can use it in
    MyPage.aspx.vb?
    thanks in advance!
    wardeaux

    "Alessandro Zifiglio" <> wrote in
    message news:W_BNb.3605$...
    > oops, custom server control. My mistake. The example i just showed you is
    > good. I though you were using the get/set accessors in your page and that
    > didnt make much sense to me ;P
    > Please ignore where i said "get/set accessors are useful if you wanted to
    > pass values to another page --plus there are some extra steps you need to
    > take in the receiving page etc Look in the docs, its explained in detail"
    >
    > Look at the link for more details on how to maintain state in your web
    > custom control ;)
    >

    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
    >
    > "Wardeaux" <> wrote in message
    > news:...
    > > Help! I'm developing a custom server control and want to get/set

    > properties
    > > in my codebehind. I have the control set within the HTML page correctly

    > and
    > > it loads/displays correctly, but when I try to compile my aspx.vb code,

    it
    > > complains that my variable name is undeclared. Is there a good article

    or
    > > sample I can use to master this? I don't need Postback, but I do want

    the
    > > properties avail during other controls postbacks. ANY assist is most
    > > appreciated!
    > > wardeaux
    > >
    > >

    >
    >
    Wardeaux, Jan 19, 2004
    #6
  7. Wardeaux,
    Just like any webControl. If you had coded it correctly as you drag it onto
    your webform, you should get the declaration for your webcontrol in your
    code behind just like with any webcontrol, use that to access its
    properties.

    Another way is to load it dynamically, you do this like any webcontrol
    again. You declare the type first

    dim mycontrol1 as mycontrol = new mycontrol
    'then add the control to the page, or placeholder control
    placeholder1.controls.add(mycontrol1)
    'then set any properties --now it will maintain state
    mycontrol1.myproperty = "Hello world"

    You probably know this, however make sure you have added a reference to your
    customcontrol and you see it in your references node in solution
    explorer --if you added your custom control your toolbox and dragged and
    dropped it onto your page then the reference is already added --otherwise
    add the reference

    "Wardeaux" <> wrote in message
    news:%...
    > I appreciate your replies, but I think you missed my question...
    > I have a custom control. I want to set properties and get values from my
    > custom control in my Codebehind. I place the control on my HTML page
    > (MyPage.aspx). I name the control MyControl1. Now I go the codebehind
    > MyPage.aspx.vb and in the PageLoad sub I want to load MyControl1 with

    data,
    > but when I type:
    > "...
    > MyControl1.MyProperty = "Hello world!"
    > ..."
    > I get a compile-time error stating that MyControl1 is an undeclared
    > variable...
    > How can I get a handle to the custom control object so that I can use it

    in
    > MyPage.aspx.vb?
    > thanks in advance!
    > wardeaux
    >
    > "Alessandro Zifiglio" <> wrote in
    > message news:W_BNb.3605$...
    > > oops, custom server control. My mistake. The example i just showed you

    is
    > > good. I though you were using the get/set accessors in your page and

    that
    > > didnt make much sense to me ;P
    > > Please ignore where i said "get/set accessors are useful if you wanted

    to
    > > pass values to another page --plus there are some extra steps you need

    to
    > > take in the receiving page etc Look in the docs, its explained in

    detail"
    > >
    > > Look at the link for more details on how to maintain state in your web
    > > custom control ;)
    > >

    >

    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
    > >
    > > "Wardeaux" <> wrote in message
    > > news:...
    > > > Help! I'm developing a custom server control and want to get/set

    > > properties
    > > > in my codebehind. I have the control set within the HTML page

    correctly
    > > and
    > > > it loads/displays correctly, but when I try to compile my aspx.vb

    code,
    > it
    > > > complains that my variable name is undeclared. Is there a good

    article
    > or
    > > > sample I can use to master this? I don't need Postback, but I do want

    > the
    > > > properties avail during other controls postbacks. ANY assist is most
    > > > appreciated!
    > > > wardeaux
    > > >
    > > >

    > >
    > >

    >
    >
    Alessandro Zifiglio, Jan 19, 2004
    #7
  8. Wardeaux

    Wardeaux Guest

    Thanks for the reply! I added the control MANUALLY to the aspx page (coded
    by hand the @reference and the <Prefix:Tagname> tags... that's probably why
    the correct references weren't added to the aspx.vb file... thanks! :)

    "Alessandro Zifiglio" <> wrote in
    message news:Y7YOb.5324$...
    > Wardeaux,
    > Just like any webControl. If you had coded it correctly as you drag it

    onto
    > your webform, you should get the declaration for your webcontrol in your
    > code behind just like with any webcontrol, use that to access its
    > properties.
    >
    > Another way is to load it dynamically, you do this like any webcontrol
    > again. You declare the type first
    >
    > dim mycontrol1 as mycontrol = new mycontrol
    > 'then add the control to the page, or placeholder control
    > placeholder1.controls.add(mycontrol1)
    > 'then set any properties --now it will maintain state
    > mycontrol1.myproperty = "Hello world"
    >
    > You probably know this, however make sure you have added a reference to

    your
    > customcontrol and you see it in your references node in solution
    > explorer --if you added your custom control your toolbox and dragged and
    > dropped it onto your page then the reference is already added --otherwise
    > add the reference
    >
    > "Wardeaux" <> wrote in message
    > news:%...
    > > I appreciate your replies, but I think you missed my question...
    > > I have a custom control. I want to set properties and get values from

    my
    > > custom control in my Codebehind. I place the control on my HTML page
    > > (MyPage.aspx). I name the control MyControl1. Now I go the codebehind
    > > MyPage.aspx.vb and in the PageLoad sub I want to load MyControl1 with

    > data,
    > > but when I type:
    > > "...
    > > MyControl1.MyProperty = "Hello world!"
    > > ..."
    > > I get a compile-time error stating that MyControl1 is an undeclared
    > > variable...
    > > How can I get a handle to the custom control object so that I can use it

    > in
    > > MyPage.aspx.vb?
    > > thanks in advance!
    > > wardeaux
    > >
    > > "Alessandro Zifiglio" <> wrote in
    > > message news:W_BNb.3605$...
    > > > oops, custom server control. My mistake. The example i just showed you

    > is
    > > > good. I though you were using the get/set accessors in your page and

    > that
    > > > didnt make much sense to me ;P
    > > > Please ignore where i said "get/set accessors are useful if you

    wanted
    > to
    > > > pass values to another page --plus there are some extra steps you need

    > to
    > > > take in the receiving page etc Look in the docs, its explained in

    > detail"
    > > >
    > > > Look at the link for more details on how to maintain state in your web
    > > > custom control ;)
    > > >

    > >

    >

    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
    > > >
    > > > "Wardeaux" <> wrote in message
    > > > news:...
    > > > > Help! I'm developing a custom server control and want to get/set
    > > > properties
    > > > > in my codebehind. I have the control set within the HTML page

    > correctly
    > > > and
    > > > > it loads/displays correctly, but when I try to compile my aspx.vb

    > code,
    > > it
    > > > > complains that my variable name is undeclared. Is there a good

    > article
    > > or
    > > > > sample I can use to master this? I don't need Postback, but I do

    want
    > > the
    > > > > properties avail during other controls postbacks. ANY assist is

    most
    > > > > appreciated!
    > > > > wardeaux
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Wardeaux, Jan 19, 2004
    #8
  9. Your welcome. Glad i could help ;)
    "Wardeaux" <> wrote in message
    news:...
    > Thanks for the reply! I added the control MANUALLY to the aspx page

    (coded
    > by hand the @reference and the <Prefix:Tagname> tags... that's probably

    why
    > the correct references weren't added to the aspx.vb file... thanks! :)
    >
    > "Alessandro Zifiglio" <> wrote in
    > message news:Y7YOb.5324$...
    > > Wardeaux,
    > > Just like any webControl. If you had coded it correctly as you drag it

    > onto
    > > your webform, you should get the declaration for your webcontrol in your
    > > code behind just like with any webcontrol, use that to access its
    > > properties.
    > >
    > > Another way is to load it dynamically, you do this like any webcontrol
    > > again. You declare the type first
    > >
    > > dim mycontrol1 as mycontrol = new mycontrol
    > > 'then add the control to the page, or placeholder control
    > > placeholder1.controls.add(mycontrol1)
    > > 'then set any properties --now it will maintain state
    > > mycontrol1.myproperty = "Hello world"
    > >
    > > You probably know this, however make sure you have added a reference to

    > your
    > > customcontrol and you see it in your references node in solution
    > > explorer --if you added your custom control your toolbox and dragged and
    > > dropped it onto your page then the reference is already

    added --otherwise
    > > add the reference
    > >
    > > "Wardeaux" <> wrote in message
    > > news:%...
    > > > I appreciate your replies, but I think you missed my question...
    > > > I have a custom control. I want to set properties and get values from

    > my
    > > > custom control in my Codebehind. I place the control on my HTML page
    > > > (MyPage.aspx). I name the control MyControl1. Now I go the codebehind
    > > > MyPage.aspx.vb and in the PageLoad sub I want to load MyControl1 with

    > > data,
    > > > but when I type:
    > > > "...
    > > > MyControl1.MyProperty = "Hello world!"
    > > > ..."
    > > > I get a compile-time error stating that MyControl1 is an undeclared
    > > > variable...
    > > > How can I get a handle to the custom control object so that I can use

    it
    > > in
    > > > MyPage.aspx.vb?
    > > > thanks in advance!
    > > > wardeaux
    > > >
    > > > "Alessandro Zifiglio" <> wrote

    in
    > > > message news:W_BNb.3605$...
    > > > > oops, custom server control. My mistake. The example i just showed

    you
    > > is
    > > > > good. I though you were using the get/set accessors in your page and

    > > that
    > > > > didnt make much sense to me ;P
    > > > > Please ignore where i said "get/set accessors are useful if you

    > wanted
    > > to
    > > > > pass values to another page --plus there are some extra steps you

    need
    > > to
    > > > > take in the receiving page etc Look in the docs, its explained in

    > > detail"
    > > > >
    > > > > Look at the link for more details on how to maintain state in your

    web
    > > > > custom control ;)
    > > > >
    > > >

    > >

    >

    http://msdn.microsoft.com/library/d...pguide/html/cpconmantainingstateincontrol.asp
    > > > >
    > > > > "Wardeaux" <> wrote in message
    > > > > news:...
    > > > > > Help! I'm developing a custom server control and want to get/set
    > > > > properties
    > > > > > in my codebehind. I have the control set within the HTML page

    > > correctly
    > > > > and
    > > > > > it loads/displays correctly, but when I try to compile my aspx.vb

    > > code,
    > > > it
    > > > > > complains that my variable name is undeclared. Is there a good

    > > article
    > > > or
    > > > > > sample I can use to master this? I don't need Postback, but I do

    > want
    > > > the
    > > > > > properties avail during other controls postbacks. ANY assist is

    > most
    > > > > > appreciated!
    > > > > > wardeaux
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Alessandro Zifiglio, Jan 19, 2004
    #9
    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. Marc Hoeppner

    Re: User Controls and CodeBehind

    Marc Hoeppner, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    370
  2. Karl Hungus

    codebehind and custom objects

    Karl Hungus, Feb 9, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    339
    Karl Hungus
    Feb 9, 2004
  3. Replies:
    0
    Views:
    324
  4. Learner
    Replies:
    5
    Views:
    770
    Learner
    May 24, 2006
  5. rodchar
    Replies:
    0
    Views:
    393
    rodchar
    Nov 26, 2007
Loading...

Share This Page