Thursday, April 7, 2011

Silverlight bound TextBox loses data when browser closes

When I bind a TextBox control to a string property of a object using two way binding, it works fine - as long as the user moves off the control before he closes the browser window/tab.

But what about the user who makes a change to the contents of a text box then closes the window expecting the data to have been saved?

While it is possible to hook into the TextChanged event or Application_Exit() handler and manually update the property, you're essentially re-doing the work of the binder. Nevertheless these seem to be the only solutions so far.

nb. The same xaml/code in a WPF app works fine (App.OnExit shows updated data in object).

From stackoverflow
  • Does Silverlight's Binding class have an UpdateSourceTrigger property? In WPF you can tell a control to update its bound source whenever the property changes (rather than when the control loses focus), like this:

    <TextBox Text="{Binding Path=Foo,UpdateSourceTrigger=PropertyChanged}" />
    
    Phil Bachmann : Nice try, I can't find it documented and when I tried it, the page did not load at all. By the way, I tested my original xaml/code in a WPF app and it works fine.
  • I'm making an educated guess here based on significant web development experience but very limited Silverlight experience.

    You could use a bit of Javascript to hook into onunload in the HTML and then call a function in your Silverlight code to handle it.

    Phil Bachmann : Silverlight has a a page unload type event (app_exit), but this still requires the manual retrieval of the control data. Probably the best work-around yet.
  • It seems that this is problem with Silverlight. It does not update bound property on text box until it loses focus.

    One workaround I had to use (I was implementing dynamic filter) is to implement TextChanged event handler and update backing property manually.

    Phil Bachmann : Thanks for that. I've updated the question accordingly.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.