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.