Control the TAB key to the next control after refreshing

Source: Internet
Author: User
The control on the page can be TAB continuously, which is a basic requirement of the system. No related methods found on the Internet. I think the general practice is to make the page refreshing, but if the page has to be refreshed, how can we control it?
The idea is very simple. The first is to obtain the control that causes postback,

Control ocontrol = this. FindControl (Request. Form ["_ EVENTTARGET"]); // "_ EVENTTARGET" records the ID of the Control that triggered the last postback event.

Then I will naturally find the next control of the control, then focus (), through Page. controls can be found, but I think this may not avoid hardCode, because it is difficult to judge whether a Control can focus () in the server segment ().
I have found some materials.

Document. onkeydown = new Function ("if (event. keyCode = 13) {event. keyCode = 9 ;}");

Inspired by this, I thought that I had the control that triggered the PostBack get the focus, and then triggered a tab key event. The focus would naturally move to the next control, so I had a hard time, I got a conclusion that this is not feasible. Why not? I don't know. The test code is as follows:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> Untitled Page </title>

<Script>
Function ControlFocus (){
Var Ka = document. createEventObject ();
Ka. ctrlKey = false;
Ka. keyCode = 9;
Document. fireEvent ("onkeydown", Ka );
}
Document. onkeydown = new Function ("if (event. keyCode = 9) {alert ('tab ');}");
// This event is only used for testing.
</Script>

</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input type = "text" id = "test" style = "color: Red; border: 0;" onblur = "ControlFocus ();"/>
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: TextBox ID = "TextBox2" runat = "server"> </asp: TextBox>
<Asp: TextBox ID = "TextBox3" runat = "server"> </asp: TextBox>
<Asp: TextBox ID = "TextBox4" runat = "server"> </asp: TextBox>
<Asp: TextBox ID = "TextBox5" runat = "server"> </asp: TextBox> </div>
</Form>

</Body>
</Html>

In my opinion, when test loses focus, an event is triggered to call the ControlFocus () function, and a Tab key is triggered. As a result, the Tab key is triggered ('tab' is displayed during testing '), but the focus is not transferred to the next control. I can't explain it even if I have checked a lot of information. I think since I can use the enter key to convert it to the tab key control focus, it should have the same effect when I program the tab key, but the result is not, there is no relevant explanation on the Internet. Some experts may want to explain it.

No way, you can only find the next control on the server side to achieve this. Although there is not much technical content, I will send it to you for a look:

// Here is the focus control written in the configuration file, because a third-party control is used.
String FocusAbleControlList = System. Configuration. ConfigurationSettings. deleettings ["AbleFocusControlList"];

Private bool isFocusAble (string ControlType)
{
If (FocusAbleControlList. IndexOf ("," + ControlType + ",") <0)
{
Return false;
}
Else
{
Return true;
}
}

Protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e );
If (Request. Form ["_ EVENTTARGET"]! = Null)
{
Control ocontrol = this. FindControl (Request. Form ["_ EVENTTARGET"]);
If (ocontrol! = Null)
{
Foreach (Control con in this. Controls)
{
// When there are multiple from statements, hardcode is used here.
If (con. ID! = Null & con. ID. ToLower (). IndexOf ("form")>-1 ){
Int index = con. Controls. IndexOf (ocontrol );
If (index! =-1)
{
Bool isableFocous = false;
While (! IsableFocous)
{
Index ++;
String [] temp = con. Controls [index]. ToString (). Split ('.');
String typename = temp [temp. Length-1];
IsableFocous = isFocusAble (typename );
}
Con. Controls [index]. Focus ();
}
}
}

}
}
}

 
I hope you can see your ideas. I think there should be a simpler way to implement this function.


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.