开发者

Update text box text to DB while the text box has focus

I'm looking for the bes开发者_如何学JAVAt solution to update a text box text value to DB while the text box still contains the focus. I don't want to do it on TextChanged or Leave events taking in consideration that I need to query the actual value from DB even when the text box contains the focus and the user stopped typing.


Every X seconds (Using a Timer)

    if (txtMyTextBox.Text != previousText)
       previousText = txtMyTextBox.Text;
    else //(if the text is the same before X seconds)
         //User has stopped typing! Save it to the database
         //Stop the timer!

TextChange event of txtMyTextBox will check if the Timer is stopped, and if yes, restart it.

X is the number of seconds after, if no input is received, you decide that the user has stopped typing.

Given there are a lot of text boxes, this can get very cumbersome :S So I agree definitely there should be a better way.


I'd go another way:

First define when you assume that the user stopped typing. LostFocus is a very good indication because the user explicitly left the textbox. A time-out is much less trustworthy because the user might be slow at typing or might be distracted. Having the program then change the text might confuse the user.

You could even create a two-value textbox control: one input field accepting the users input the other (readonly) field showing the current value on the database. This control might have a button to allow the user to accept the value from the database.

This all feels like trying to dodge the nasty side of the classical optimistic concurrency problem: what if the value in the database changes during editing?

The solution you are creating here has a lot of overhead which might not be worth it (have a look at the network traffic...) Just allow the user to enter a value. Pass it back to the database, catch conflicts and allow the user to handle the conflict. This will scale much better and is much easier to implement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜