Solved the problem of default submit button when pressing enter in the text box of ASP. NET

Source: Internet
Author: User

In ASP. NET, a page only has a form. A common problem is that when you press enter in a text box, you do not know which button is used as the submit button for submission. It is very depressing !!! Especially when there are user controls, do not dare to use validator.
Today, it cannot be solved, but it is not troublesome.
Basic Ideas:
Write a control to inherit the textbox, and add a SubmitControl attribute to specify which button is used to respond when you press enter on the text box.
Another converter adds support for design, that is, you can use the drop-down box to select the submit button during design.
Currently, only buttons, linkbuttons, and imagebutton are considered as the submission buttons. Of course, they can be inherited.

If there are few codes, paste them here.

Ufotextbox control:
Public class ufotextbox: textbox
{
[Typeconverter (typeof (submitablecontrolconvertor), defaultvalue (""), category ("behavior")]
Public String SubmitControl
{
Get
{
Object ret = This. viewstate ["SubmitControl"];
If (Ret! = NULL)
{
Return (string) ret;
}
Return string. empty;
}
Set
{
This. viewstate ["SubmitControl"] = value;
}
}

Protected override void addattributestorender (htmltextwriter writer)
{
Base. addattributestorender (writer );
If (this. SubmitControl. length> 0)
{
Control con = findcontrol (SubmitControl );
If (con! = NULL)
{
String script = "If (event. keycode = 13) {document. getelementbyid ('"+ con. clientid + "'). click (); event. returnvalue = false ;}";
Writer. addattridown ("onkeydown", script );
}
}

}
}
Custom converter, supported during design:
Public class submitablecontrolconvertor: stringconverter
{
Private object [] getcontrols (icontainer container)
{
Componentcollection components = container. components;
Arraylist ret = new arraylist ();
Foreach (icomponent control in components)
{
If (! (Control is button | control is linkbutton | control is imagebutton ))
{
Continue;
}
Control Button = (Control) control;
If (button. ID! = NULL) & (button. Id. length! = 0 ))
{
Ret. Add (string. Copy (button. ID ));
}
}
Ret. Sort (comparer. Default );
Return ret. toarray ();
}

Public override system. componentmodel. typeconverter. standardvaluescollection getstandardvalues (itypedescriptorcontext context)
{
If (context! = NULL) & (context. container! = NULL ))
{
Object [] controls = This. getcontrols (context. Container );
If (controls! = NULL)
{
Return new typeconverter. standardvaluescollection (Controls );
}
}
Return NULL;
}

Public override bool getstandardvaluesexclusive (itypedescriptorcontext context)
{
Return false;
}

Public override bool getstandardvaluessupported (itypedescriptorcontext context)
{
Return true;
}
}
A few lines of code.

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.