Bind controls to object

Discussion in 'ASP .Net' started by Chris Zopers, Feb 13, 2008.

  1. Chris Zopers

    Chris Zopers Guest

    Hello,

    I have an aspx page with some controls like textboxes on it and a button
    to save the entered values.

    In the code-behind I declared a public variabele of an object, for
    example:

    public Person _prs = new Person();

    Where Person is a class with some properties, like FirstName and
    LastName. The textboxes display the values in this way: Text = '<%#
    _prs.FirstName #>' and for another Textbox: Text = '<%# _prs.LastName
    #>'

    When the user clicks on the save button, the page_load event happens
    first, so my public Person is always set to a new Person() again and
    therefore I can't use the variable _prs to see the new entered values in
    the textboxes.

    My question is: how can I bind my textboxes to the properties of the
    public variable _prs, so the values that are entered in the textboxes
    immediately are updated in the Person object and how can I preserve
    these values, so I can use the Person object in the button-save click
    event?

    Greetings,
    Chris







    *** Sent via Developersdex http://www.developersdex.com ***
     
    Chris Zopers, Feb 13, 2008
    #1
    1. Advertising

  2. Chris Zopers

    Masudur Guest

    On Feb 13, 1:52 pm, Chris Zopers <> wrote:
    > Hello,
    >
    > I have an aspx page with some controls like textboxes on it and a button
    > to save the entered values.
    >
    > In the code-behind I declared a public variabele of an object, for
    > example:
    >
    > public Person _prs = new Person();
    >
    > Where Person is a class with some properties, like FirstName and
    > LastName. The textboxes display the values in this way: Text = '<%#
    > _prs.FirstName #>' and for another Textbox: Text = '<%# _prs.LastName
    > #>'
    >
    > When the user clicks on the save button, the page_load event happens
    > first, so my public Person is always set to a new Person() again and
    > therefore I can't use the variable _prs to see the new entered values in
    > the textboxes.
    >
    > My question is: how can I bind my textboxes to the properties of the
    > public variable _prs, so the values that are entered in the textboxes
    > immediately are updated in the Person object and how can I preserve
    > these values, so I can use the Person object in the button-save click
    > event?
    >
    > Greetings,
    > Chris
    >
    > *** Sent via Developersdexhttp://www.developersdex.com***


    Do expose a property of your desired type encapsulating it in a
    viewstate or session.
    for example
    public person myperson
    {
    get
    {
    if(viewstate["myperson"]==null;
    viewstate["myperson"] = new Person();
    return (person)viewstate["myperson"]
    }
    set
    {
    viewstate["myperson"] = value;
    }
    }

    Bind you stuff with the property ...
    after postback...
    again update the property values...


    Hope this help

    Masudur
    http://www.shatkotha.com
    http://www.munna.shatkotha.com
     
    Masudur, Feb 13, 2008
    #2
    1. Advertising

  3. "Masudur" <> wrote in message
    news:...
    > On Feb 13, 1:52 pm, Chris Zopers <> wrote:
    >> Hello,
    >>
    >> I have an aspx page with some controls like textboxes on it and a button
    >> to save the entered values.
    >>
    >> In the code-behind I declared a public variabele of an object, for
    >> example:
    >>
    >> public Person _prs = new Person();
    >>
    >> Where Person is a class with some properties, like FirstName and
    >> LastName. The textboxes display the values in this way: Text = '<%#
    >> _prs.FirstName #>' and for another Textbox: Text = '<%# _prs.LastName
    >> #>'
    >>
    >> When the user clicks on the save button, the page_load event happens
    >> first, so my public Person is always set to a new Person() again and
    >> therefore I can't use the variable _prs to see the new entered values in
    >> the textboxes.
    >>
    >> My question is: how can I bind my textboxes to the properties of the
    >> public variable _prs, so the values that are entered in the textboxes
    >> immediately are updated in the Person object and how can I preserve
    >> these values, so I can use the Person object in the button-save click
    >> event?
    >>
    >> Greetings,
    >> Chris
    >>
    >> *** Sent via Developersdexhttp://www.developersdex.com***

    >
    > Do expose a property of your desired type encapsulating it in a
    > viewstate or session.
    > for example
    > public person myperson
    > {
    > get
    > {
    > if(viewstate["myperson"]==null;
    > viewstate["myperson"] = new Person();
    > return (person)viewstate["myperson"]
    > }
    > set
    > {
    > viewstate["myperson"] = value;
    > }
    > }
    >
    > Bind you stuff with the property ...
    > after postback...
    > again update the property values...


    I believe that in order to use the viewstate as you suggest he would need to
    ensure that the Person class is serializable (and may need to implement an
    interface). The OP also needs to realize that by default the Person class
    will be sent to the browser and back, which incurs overhead and is a
    potential security risk.
     
    Scott Roberts, Feb 13, 2008
    #3
  4. "Chris Zopers" <> wrote in message
    news:...
    > Hello,
    >
    > I have an aspx page with some controls like textboxes on it and a button
    > to save the entered values.
    >
    > In the code-behind I declared a public variabele of an object, for
    > example:
    >
    > public Person _prs = new Person();
    >
    > Where Person is a class with some properties, like FirstName and
    > LastName. The textboxes display the values in this way: Text = '<%#
    > _prs.FirstName #>' and for another Textbox: Text = '<%# _prs.LastName
    > #>'
    >
    > When the user clicks on the save button, the page_load event happens
    > first, so my public Person is always set to a new Person() again and
    > therefore I can't use the variable _prs to see the new entered values in
    > the textboxes.
    >
    > My question is: how can I bind my textboxes to the properties of the
    > public variable _prs, so the values that are entered in the textboxes
    > immediately are updated in the Person object and how can I preserve
    > these values, so I can use the Person object in the button-save click
    > event?


    Session is a good candidate:

    if (!IsPostBack)
    {
    Person _prs = Session["prs"] as Person;
    if (_prs == null)
    _prs = new Person(); // or throw an exception
    }
    else
    _prs = new Person();

    Session["prs"] = _prs;


    However, we actually reload the object from the database based on a query
    string parameter:

    Guid id = new Guid(QueryString["id"]);
    Person _prs = new Person(id); // Loads person from DB if "id" exists.
    Otherwise gives a new object.
     
    Scott Roberts, Feb 13, 2008
    #4
  5. Chris Zopers

    bruce barker Guest

    the ObjectDataSource class is used for this. There are several examples in
    the docs that should help you.


    -- bruce (sqlwork.com)


    "Chris Zopers" wrote:

    > Hello,
    >
    > I have an aspx page with some controls like textboxes on it and a button
    > to save the entered values.
    >
    > In the code-behind I declared a public variabele of an object, for
    > example:
    >
    > public Person _prs = new Person();
    >
    > Where Person is a class with some properties, like FirstName and
    > LastName. The textboxes display the values in this way: Text = '<%#
    > _prs.FirstName #>' and for another Textbox: Text = '<%# _prs.LastName
    > #>'
    >
    > When the user clicks on the save button, the page_load event happens
    > first, so my public Person is always set to a new Person() again and
    > therefore I can't use the variable _prs to see the new entered values in
    > the textboxes.
    >
    > My question is: how can I bind my textboxes to the properties of the
    > public variable _prs, so the values that are entered in the textboxes
    > immediately are updated in the Person object and how can I preserve
    > these values, so I can use the Person object in the button-save click
    > event?
    >
    > Greetings,
    > Chris
    >
    >
    >
    >
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    >
     
    bruce barker, Feb 13, 2008
    #5
  6. Chris Zopers

    Chris Zopers Guest

    Hello Bruce,

    I indeed already tried the ObjecDataSource, but I ran into an other
    problem with that. I've posted a question about my new problem in this
    newsgroup, here's the text of this new problem:

    Hello,

    I have some textboxes in a FormView control. The FormView is bound to an
    ObjectDataSource. The ObjectDataSource has a SelectMethod that gets an
    object and the textboxes in the FormView are bound to the Object's
    properties. This all works fine.

    The ObjectDataSource also has an UpdateMethod that points to a function
    that accepts the whole object as a parameter. But when I call the
    ObjectDataSource.Update() method, the specified function is called, but
    the object-parameter is always a completely empty object.

    Does anyone recognize this problem and how can I solve this?

    Greetings,
    Chris




    *** Sent via Developersdex http://www.developersdex.com ***
     
    Chris Zopers, Feb 14, 2008
    #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. Emanuele

    Bind controls to class properties

    Emanuele, Aug 16, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    577
  2. mr2_93
    Replies:
    1
    Views:
    3,604
    mr2_93
    Oct 2, 2005
  3. =?Utf-8?B?RGF2aWQ=?=

    Bind Asp Controls to ObjectDataSource.Select

    =?Utf-8?B?RGF2aWQ=?=, May 22, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,552
    =?Utf-8?B?RGF2aWQ=?=
    May 22, 2006
  4. Replies:
    0
    Views:
    592
  5. dan
    Replies:
    0
    Views:
    601
Loading...

Share This Page