Labels and usercontrols

Discussion in 'ASP .Net Web Controls' started by Micke Palm, Jun 29, 2004.

  1. Micke Palm

    Micke Palm Guest

    Hi!

    I want to use a parent page and load webusercontrols to this page dynamically. The problem I got is how I can set a label.text in parent page from a usercontrol.

    A scenenario is if I have a button into the usercontrol and want to set a text to a label in the parents page. I've set the click_sub to public and the label to friend so I can se it, but when I try to dedicate a text I got this error.
    Object reference not set to an instance of an object.

    My declaration is ...
    Dim test As New myParentClass
    test.ChangedBy.Text = "Ett test"

    Do anyone have a clue how I can do this. I have also test with a property and I succeed to dedicate the value to the property ,but how can I get it to the label. Maybe I need reload the parent page?

    Maybe sessions are a better way to move the data??

    /regards, Micke
     
    Micke Palm, Jun 29, 2004
    #1
    1. Advertising

  2. Micke Palm

    ranganh Guest

    Dear Micke,

    In the Usercontrol, set a property

    public string lbl_Text
    {
    set
    {
    lbl_Text=value;

    }

    }


    then when you load the usercontrol in the page, you can also provide the value for the property in the page.

    Hope it helps.





    "Micke Palm" wrote:

    > Hi!
    >
    > I want to use a parent page and load webusercontrols to this page dynamically. The problem I got is how I can set a label.text in parent page from a usercontrol.
    >
    > A scenenario is if I have a button into the usercontrol and want to set a text to a label in the parents page. I've set the click_sub to public and the label to friend so I can se it, but when I try to dedicate a text I got this error.
    > Object reference not set to an instance of an object.
    >
    > My declaration is ...
    > Dim test As New myParentClass
    > test.ChangedBy.Text = "Ett test"
    >
    > Do anyone have a clue how I can do this. I have also test with a property and I succeed to dedicate the value to the property ,but how can I get it to the label. Maybe I need reload the parent page?
    >
    > Maybe sessions are a better way to move the data??
    >
    > /regards, Micke
    >
     
    ranganh, Jun 29, 2004
    #2
    1. Advertising

  3. Micke Palm

    Jos Guest

    >"Micke Palm" <> wrote in message
    news:...
    >Hi!
    >
    >I want to use a parent page and load webusercontrols to this page

    dynamically. The problem I got is how I can set a label.text in parent page
    from a usercontrol.
    >
    >A scenenario is if I have a button into the usercontrol and want to set a

    text to a label in the parents page. I've set the click_sub to public and
    the label to friend so I can se it, but when I try to dedicate a >text I got
    this error.
    >Object reference not set to an instance of an object.
    >
    >My declaration is ...
    >Dim test As New myParentClass
    >test.ChangedBy.Text = "Ett test"


    Use:
    Dim test As myParentClass = CType(Page,myParentClass)
    test.ChangedBy.Text = "Ett test"

    By the way, I believe that your problem indicates a flaw in your design.
    According to the object oriented philosophy, a user control is
    supposed to stand on its own. It shouldn't need to access external
    controls, nor should it assume that its parent is of a certain class.

    You could consider making the label a part of the user control,
    except if you want to add multiple user controls and a single label
    to your page. In that case, you could consider adding an event
    to the user control, adding an event handler to the page, and
    setting the label's text there.

    Jos
     
    Jos, Jun 30, 2004
    #3
  4. Micke Palm

    Micke Palm Guest

    I've try a property too and sure I can save data into the property ,but how
    can I pick it up. As you know a aspnet is not the same like vbnet cos the
    page need to reload and build up all controls from scratch everytime it
    loads.
    So its no problem to save it into a property the problem is when I need to
    get it into the label.

    /regards, micke

    "ranganh" <> wrote in message
    news:...
    > Dear Micke,
    >
    > In the Usercontrol, set a property
    >
    > public string lbl_Text
    > {
    > set
    > {
    > lbl_Text=value;
    >
    > }
    >
    > }
    >
    >
    > then when you load the usercontrol in the page, you can also provide the

    value for the property in the page.
    >
    > Hope it helps.
    >
    >
    >
    >
    >
    > "Micke Palm" wrote:
    >
    > > Hi!
    > >
    > > I want to use a parent page and load webusercontrols to this page

    dynamically. The problem I got is how I can set a label.text in parent page
    from a usercontrol.
    > >
    > > A scenenario is if I have a button into the usercontrol and want to set

    a text to a label in the parents page. I've set the click_sub to public and
    the label to friend so I can se it, but when I try to dedicate a text I got
    this error.
    > > Object reference not set to an instance of an object.
    > >
    > > My declaration is ...
    > > Dim test As New myParentClass
    > > test.ChangedBy.Text = "Ett test"
    > >
    > > Do anyone have a clue how I can do this. I have also test with a

    property and I succeed to dedicate the value to the property ,but how can I
    get it to the label. Maybe I need reload the parent page?
    > >
    > > Maybe sessions are a better way to move the data??
    > >
    > > /regards, Micke
    > >
     
    Micke Palm, Jun 30, 2004
    #4
  5. Micke Palm

    Micke Palm Guest

    CType(Page,myParentClass)
    Did the trick...thanks!

    I understand what you mean with "a webuser control should stand alone" and I
    agree ,but in this case I really need it cos I building a Wizard and I don't
    want all code in the same page. That's the reason.

    /Thanks for the help, micke

    "Jos" <> wrote in message
    news:%...
    > >"Micke Palm" <> wrote in message

    > news:...
    > >Hi!
    > >
    > >I want to use a parent page and load webusercontrols to this page

    > dynamically. The problem I got is how I can set a label.text in parent

    page
    > from a usercontrol.
    > >
    > >A scenenario is if I have a button into the usercontrol and want to set a

    > text to a label in the parents page. I've set the click_sub to public and
    > the label to friend so I can se it, but when I try to dedicate a >text I

    got
    > this error.
    > >Object reference not set to an instance of an object.
    > >
    > >My declaration is ...
    > >Dim test As New myParentClass
    > >test.ChangedBy.Text = "Ett test"

    >
    > Use:
    > Dim test As myParentClass = CType(Page,myParentClass)
    > test.ChangedBy.Text = "Ett test"
    >
    > By the way, I believe that your problem indicates a flaw in your design.
    > According to the object oriented philosophy, a user control is
    > supposed to stand on its own. It shouldn't need to access external
    > controls, nor should it assume that its parent is of a certain class.
    >
    > You could consider making the label a part of the user control,
    > except if you want to add multiple user controls and a single label
    > to your page. In that case, you could consider adding an event
    > to the user control, adding an event handler to the page, and
    > setting the label's text there.
    >
    > Jos
    >
    >
    >
    >
     
    Micke Palm, Jun 30, 2004
    #5
    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. Rathtap
    Replies:
    0
    Views:
    410
    Rathtap
    Jun 23, 2004
  2. Jon Paal
    Replies:
    16
    Views:
    525
    Mr Newbie
    Dec 30, 2005
  3. Neo
    Replies:
    0
    Views:
    339
  4. Michael Kellogg
    Replies:
    0
    Views:
    880
    Michael Kellogg
    Apr 6, 2006
  5. Peter Foti
    Replies:
    13
    Views:
    954
    Isofarro
    Oct 18, 2003
Loading...

Share This Page