It was so easy to control textbox to enter only numbers

Source: Internet
Author: User

When developing Web applications, it is inevitable that only numbers can be entered in textbox. What should I do? Create a verification control, add regular expressions, or use JavaScript scripts to control it,
In the previous development, I used the above two methods to determine whether this method is actually not acceptable. However, recently I was studying component development. It turns out that textbox can be rewritten.
Encapsulate the script for controlling input numbers in the control, so that it can be used only after being dragged out. You don't have to write and judge it every time. Isn't that beautiful!
In fact, I am also a newbie. I wrote what I have done here only to consolidate my knowledge. Where can I write poorly and how can I solve it? I also give more advice to experts!

The following is the source code of the custom control.

Using system;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Collections. Generic;
Using system. text;

Namespace smbcomponent. editorsuite
{
Public class numbereditor: textbox {
Private const string smb_number_script_id = "{9698eb3d-cb91-47cc-8ad8-aa3e7a4b4acd }";

Private const string smb_number_script_only_hook = "Return numbereditor_keypress_handle (this )";
Private const string smb_number_script_only_script = "<script language = \" javascript \ "> \ n" +
"Function numbereditor_keypress_handle (CTRL) \ n {" +
"If (event. keycode = 13) \ n return true; \ n" +
"If (event. keycode <48 | event. keycode> 57) \ n" +
"Return false; \ n else \ n return true; \ n}" +
"</SCRIPT> ";

Private void renderjavscript ()
{
If (! Page. isclientscriptblockregistered (smb_number_script_id ))
{
Page. registerclientscriptblock (smb_number_script_id, String. Format (smb_number_script_only_script, base. ID ));
}
}

Protected override void addattributestorender (htmltextwriter writer)
{
Base. addattributestorender (writer );
Writer. addattripress ("onkeypress", smb_number_script_only_hook );
}

Protected override void onprerender (eventargs E)
{
Base. onprerender (E );
Renderjavscript ();
}

Public numbereditor ()
: Base ()
{}
}
}

Verify whether the number is verified by the Javascript script. The script is encapsulated in the control.

There are two key points:
1. Be sure to register the JS script before drawing the status. Where is the location? It is done in onprerender (eventargs E, if the script is registered after the drawing state is complete, it will be useless.
2. To add a property to the control, you must add it before drawing the HTML Tag. If it is too late after the HTML Tag is drawn.
Before the HTML Tag is drawn, it is in addattributestorender, because addattributestorender is called by renderbegintag.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.