Problems using a DetailsView without a datasource

  • Thread starter Thread starter Giulio Petrucci
  • Start date Start date
G

Giulio Petrucci

Hi there,

I'm trying to use a DetailsView control without any datasource and I've
been spending the whole morning dealing with the following issue. Here
is my DetailsView markup code:

<asp:DetailsView Visible = "true" ID="fooGrid"
AutoGenerateEditButton="true" ...>
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true"/>
<asp:BoundField DataField="Label" HeaderText="Label"/>
</Fields>
</asp:DetailsView>

I'm using this control to show an instance of a Foo class:
class Foo
{
public string Name { get; set; }
public string Label { get; set; }
}

in my asp.net page code behind, I have the following code to bind my Foo
instance to the control:

//instance menber;
Foo foo = ... ;
//and the following code is within the Page_Load code
fooGrid.DataSource = new Foo[] { foo };
fooGrid.DataBind();

And here comes the problem.
I click on the "Edit" button, the edit the "Label" field and change it.
If i click on the "Update" confirmation button, the 'foo' instance
'Label' property value is not changed: it's still the old one.
Where am I wrong? Any suggestion? Links?

Thanks in advance,
Giulio - Italia
 
Hi there,

I'm trying to use a DetailsView control without any datasource and
I've been spending the whole morning dealing with the following issue.
Here is my DetailsView markup code:

<asp:DetailsView Visible = "true" ID="fooGrid"
AutoGenerateEditButton="true" ...>
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true"/>
<asp:BoundField DataField="Label" HeaderText="Label"/>
</Fields>
</asp:DetailsView>

I'm using this control to show an instance of a Foo class:
class Foo
{
public string Name { get; set; }
public string Label { get; set; }
}

in my asp.net page code behind, I have the following code to bind my
Foo instance to the control:

//instance menber;
Foo foo = ... ;
//and the following code is within the Page_Load code
fooGrid.DataSource = new Foo[] { foo };
fooGrid.DataBind();

And here comes the problem.
I click on the "Edit" button, the edit the "Label" field and change
it. If i click on the "Update" confirmation button, the 'foo' instance
'Label' property value is not changed: it's still the old one.
Where am I wrong? Any suggestion? Links?


Unlike Windows forms, which automagically bind two-way, you have to
actually grab the value and set the object in ASP.NET.

While this might seem wrong, it is necessary as web applications are
stateless. Every time a user clicks, types in an URL, is the first time
the web server has seen the request. Any type of state in an application
(ViewState - stored as an encoded field, Session state (half cookie,
half server side), etc) is unwound with every request.

What this means is your object is bound so the user can see the
information. The binding back to the object is not automagically done
when he clicks the button.

You will have to grab the values on the click event and then set them in
the object to make this work.

Hope this helps!

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Hi Gregory,

first of all, thank you for your reply.

Gregory A. Beamer ha scritto:
[cut]
You will have to grab the values on the click event and then set them in
the object to make this work.

Uhm... I'm feeling like there's something missing. ;-)
1. I bind the objects data to the control's field.
2. I change the control mode into the "edit" mode.
3. I change some values.
I expect the control itself to *write* the changes onto the bound object
(as it *reads* the start values from the same object), right?

Thanks,
Giulio
 
Hi Mark,

thanks for your reply

Mark Rae [MVP] ha scritto:
In WebForms, as Gregory has explained, no. You need to code this
yourself...

uhm... but if I use an object data source everithing comes out
"automagically".
Can I "emulate" this black magic? ;-)

Thanks,
Giulio
 
3. I change some values.
I expect the control itself to *write* the changes onto the bound object
(as it *reads* the start values from the same object), right?

This is the part you are missing. The control does not automatically bind
back in a web application. It has to do with limitations of the web model,
as the application is disconnected.

You will have to grab the value and set it.

MyObject.Foo = FooTextBox.Text

It would be nice if you had roundtripping available in a web application,
but you don't due to the way web applications work.

In a windows application, you can two-way bind.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Hi Mark,

thanks for your reply

Mark Rae [MVP] ha scritto:

uhm... but if I use an object data source everithing comes out
"automagically".

In windows forms applications and WPF. In web applications, the
automagic is only one way.

Can I "emulate" this black magic? ;-)


Not in the automagic sense. You can bind back to the object.

if you truly want automagic and are willing to go to Silverlight, you
will be able to get more automagic than web applications. It will have
to be Silverlight 2 or 3 to have the "mini .NET framework" in the app,
as Silverlight 1 is spliced to the backend using JavaScript and very
similar to a standard web app.

XBAP is another option, but I would never go that route personally. If
someone can convince me of a scenario for XBAP, I might change my mind,
but with Silverlight getting more "full featured" I doubt I will hit
that scenario.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top