First of all, thanks to terrylee's recommendation series. This article is available in his recommendation series.
Original article: Seamless inline text editing with ASP. NET Ajax
Before starting, we need a web form to display the text label in the row and a hidden Textbox, which will be replaced during compilation:
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server"
Enablepagemethods = "True" >
< Scripts >
< ASP: scriptreference path = " Behaviors. js " / >
</ Scripts >
</ ASP >
< ASP: Label Runat = "Server" ID = "Label1" />
< ASP: textbox Runat = "Server" ID = "Textbox1" Style = "Display: none ;" />
I didn't use the "visibility" attribute of textbox to hide it, but used CSS to hide it. This is important because ASP. Net does not generate HTML for a hidden Server Control.CodeThis will make it unable to be controlled on the client.
For ease of demonstration, I will use cache to fill our label. You can also easily use databases, files, or other storage data instead.
Protected Void Page_load ( Object Sender, eventargs E)
{
If (Cache [ " Title " ] ! = Null )
Label1.text = Cache [ " Title " ]. Tostring ();
Else
Label1.text = " Default title (click to edit) " ;
}
Use JavaScript to switch display
First, we need an application init handler to establish the client event processor of label and textbox.
> All the following code blocks are included in behaviors. js.
VaR Label1, textbox1; SYS. application. add_init (appinit );
Function Appinit (sender) {
Label1 = $ Get ( ' Label1 ' );
Textbox1 = $ Get ( ' Textbox1 ' );
$ Addhandler (label1, " Click " , Labelease click );
$ Addhandler (textbox1, " Blur " , Textbox1_blur );
$ Addhandler (textbox1, " Keydown " , Textbox1_keydown );
}
In addition to writing the event processor, I also created a global variable to save the reference to the label and textbox elements. This avoids excessive calls to $ get (), which can be improved.ProgramIs easy to use.
Next, let's handle the label click event:
Function Labelease click (){
Textbox1.value = Label1.innerhtml;
Label1.style. Display = ' None ' ;
Textbox1.style. Display = '' ; // Thanks, Ira.
Textbox1.focus ();
}
Some things happen here:
- First, make sure that all items are synchronized, and always assign the current value of the label to it before the textbox is displayed.
- Then, by switching the display attribute of the two elements, we can efficiently switch the label to textbox.
- Finally, use the focus () method of textbox to move the cursor to the newly displayed textbox.
This series of events creates an intuitive user experience, as if you simply click the text and place the mouse in the textbox.
Next, we will handle the textbox blur (out of focus) event:
Function Textbox1_blur (){
Label1.innerhtml = Textbox1.value;
Textbox1.style. Display = ' None ' ;
Label1.style. Display = '' ; // Thanks, Ira.
}
Here, the new value of textbox is used to update the label value, and then the visibility of the two elements is switched. This is only the opposite of the labelease click () method.
Response to the Enter key
A problem occurs when the user presses the Enter key during editing. pressing the Enter key in the textbox will submit the form and generate an unwanted PostBack. in fact, this situation should be the same as when the user finishes editing, leaving the focus of the mouse to the textbox.
Fortunately, the solution is simple:
Function Textbox1_keydown (event ){
If (Event. keycode = 13 ){
Event. preventdefault (); // Thanks, Alessandro.
Textbox1.blur ();
}
}
What we do here is to intercept the press of the Enter key, cancel the submission form, and then make the textbox lose the focus, so that everything is as expected.
Returns it before anyone notices it disappears.
If we do not save the modified content back to the server in some way, this will be of no value. of course, the combination of updatapanel and _ dopostback () is a way to save, through partial PostBack.
However, I think using JSON and page methods is another better solution.
First, we need a static method to save our data. as I mentioned earlier, for the sake of convenience, you can easily update a database field or write it to a file in your code.
[Webmethod]
Public Static Void Settitle ( String Title)
{
Httpcontext. Current. cache [ " Title " ] = Title;
}
Now we have a way to save our data. Now, all we have to do is modify the "Blur" event handler of our textbox to use it:
Function textbox1_blur (){
Label1.innerhtml = Textbox1.value;
Textbox1.style. Display = ' None ' ;
Label1.style. Display = ' Inline ' ;
Pagemethods. settitle (textbox1.value );
}
That's simple.
Note that there is no updatepanel on the page. It is not necessary. Using JSON makes the job fast and efficient.
How to make it better
This is just the beginning. Some potential improvements include:
- When textbox loses focus, check the content of textbox. settitle () is called only when the content of textbox is modified ().
- If you use a slow data storage, it may be advantageous to use the progress bar.
- Use background images of some categories to indicate that some content can be edited in the row.
- The ESC key (27) is processed to cancel the editing and return to the label without changing the label content.
Source codeDownload:
Click here to download the source code