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" ...>
    <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
    --
    http://www.giuliopetrucci.it
    http://www.myspace.com/fujikomonamourtheband
    Giulio Petrucci, Jul 28, 2009
    #1
    1. Advertising

  2. Giulio Petrucci <> wrote in
    news:#:

    > 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! |
    *******************************************
    Gregory A. Beamer, Jul 28, 2009
    #2
    1. Advertising

  3. 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
    --
    http://www.giuliopetrucci.it
    http://www.myspace.com/fujikomonamourtheband
    Giulio Petrucci, Jul 28, 2009
    #3
  4. Giulio Petrucci, Jul 28, 2009
    #4
  5. Giulio Petrucci <> wrote in news:OX2zAl5DKHA.4184
    @TK2MSFTNGP02.phx.gbl:

    > 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! |
    *******************************************
    Gregory A. Beamer, Jul 28, 2009
    #5
  6. Giulio Petrucci <> wrote in news:u#DXQr5DKHA.4376
    @TK2MSFTNGP04.phx.gbl:

    > 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".


    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! |
    *******************************************
    Gregory A. Beamer, Jul 28, 2009
    #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. Hartmut Schroth
    Replies:
    3
    Views:
    3,498
    Hartmut Schroth
    Dec 1, 2003
  2. Not Me
    Replies:
    3
    Views:
    9,054
    diegowarrior
    Oct 8, 2006
  3. H5N1
    Replies:
    2
    Views:
    479
  4. =?Utf-8?B?Y2lzY28=?=

    ItemUpdating using DataSource(FormView or DetailsView)

    =?Utf-8?B?Y2lzY28=?=, May 22, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    4,777
    Walter Wang [MSFT]
    May 23, 2006
  5. Feege
    Replies:
    0
    Views:
    444
    Feege
    Dec 20, 2005
Loading...

Share This Page