Problems using a DetailsView without a datasource

Discussion in 'ASP .Net' started by Giulio Petrucci, Jul 28, 2009.

  1. 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" ...>
    <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true"/>
    <asp:BoundField DataField="Label" HeaderText="Label"/>

    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 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 };

    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
    Giulio Petrucci, Jul 28, 2009
    1. Advertisements

  2. 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, Jul 28, 2009
    1. Advertisements

  3. Hi Gregory,

    first of all, thank you for your reply.

    Gregory A. Beamer ha scritto:
    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?

    Giulio Petrucci, Jul 28, 2009
  4. Hi Mark,

    thanks for your reply

    Mark Rae [MVP] ha scritto:
    uhm... but if I use an object data source everithing comes out
    Can I "emulate" this black magic? ;-)

    Giulio Petrucci, Jul 28, 2009
  5. 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, Jul 28, 2009
  6. In windows forms applications and WPF. In web applications, the
    automagic is only one way.

    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, Jul 28, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.