Set focus control

Source: Internet
Author: User

Set focus control

Reproduced in: http://msdn.microsoft.com/zh-cn/library/ms379557 (vs.80). aspx

ASP. NET 2.0 provides an excellent new feature that allows you to specify which input control is set to focus when the page is displayed for the first time. This is a flexible function that reduces the burden of clicking start operations. For example, you can click Start to input data in the text box.

To specify the HTML component as the input focus, you need a short JavaScript code. First, declare that this is not a cutting-edge rocket science. You can easily add this JavaScript code as an embedded code to <Body> MarkedOnloadAttribute. However, in the page classSetfocusMethod to Determine the name of the focus control on the server is indeed a huge step forward. In fact, you can use the following code in ASP. NET 2.0.

void Page_Load(object sender, System.EventArgs e) {
SetFocus("TheFirstName");
}

When the page is displayedThefirstnameThe input control will become the focus. This method is convenient and effective, but how to encode it in ASP. NET 1.x?

Similarly, the skills for implementing this feature are already well known to the industry and can be easily searched from Google. But the question is how to integrate it into the basicsPageClass to reuse.

Let's use the following declaration to extend the msdn. Page basic class.

private string m_focusedControl;
public void SetFocus(string ctlId) {
m_focusedControl = ctlId;
}

SetfocusMethod To collect control IDs and store them in internal members. InPrerenderEvent, call another helper function to build and insert JavaScript code.

Private void addsetfocusscript ()
{
If (m_focusedcontrol = "")
Return;

// Add a script to declare a function
Stringbuilder sb = new stringbuilder ("");
SB. append ("<script language = JavaScript> ");
SB. append ("function ");
SB. append (setfocusfunctionname );
SB. append ("(CTL ){");
SB. append ("If (document. Forms [0] [CTL]! = NULL )");
SB. append ("{document. Forms [0] [CTL]. Focus ();}");
SB. append ("}");

// Add scripts to call Functions
SB. append (setfocusfunctionname );
SB. append ("('");
SB. append (m_focusedcontrol );
SB. append ("'); <");
SB. append ("/"); // disconnect in this way to avoid misunderstanding...
SB. append ("script> ");

// Register the script (the name is case sensitive)
If (! Isstartupscriptregistered (setfocusscriptname ))
Registerstartupscript (setfocusscriptname, SB. tostring ());
}

JavaScript code can be constructed like a dynamic string and accumulated and stored inStringbuilderObject. The next step is to add the string to the page output. In ASP. NET, to add some client script code to the page, you must first register the code in a specific page-level collection. To this end,PageClass provides severalRegisterxxxMethod. EachRegisterxxxMethod to add the JavaScript code block to different sets so that it can be inserted to different locations in the final page tag. For example,RegisterstartupscriptInsert code before the form close mark. WhileRegisterclientscriptblockInsert script code after opening the form. The important thing is that the script must include <Script> Two tags of an element. Each script block is identified by a keyword, so that multiple server controls can use the same script block instead of sending it to the output stream twice or multiple times.

On the page, the following JavaScript code block is inserted before the close mark of the form. In this way, it will start to run immediately after initialization.

<form>
:
<script language=javascript>
function __setFocus(ctl) {
if (document.forms[0][ctl] != null) {
document.forms[0][ctl].focus();
}
}
__setFocus('TheFirstName');
</script>
</form>

By using the msdn. Page classSetfocusPublic method: you can determine the control that is used as the input focus when displaying the page in a browser at any location of the page code. More importantly, you can make this decision based on runtime conditions and/or sending back events.

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.