Bind controls to object

C

Chris Zopers

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
 
M

Masudur

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
 
S

Scott Roberts

Masudur said:
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.
 
S

Scott Roberts

Chris Zopers said:
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.
 
B

bruce barker

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


-- bruce (sqlwork.com)
 
C

Chris Zopers

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top