How to close a webpage in Asp.net, and how to make the text box focus

Source: Internet
Author: User

Disable web page calls Javascript
<SCRIPT lang = "text/JavaScript">
Window. Close ();
</SCRIPT>

Or: add this. button1.attributes. Add ("onclick", "window. Close ();") in page_load ();");


Obtain the focus. Create a new class in C #.
For example:
Sing system;
Using system. text;
Using system. Web;
Using system. Web. UI;
Namespace Application
{
/// <Summary>
/// Obtain the focus class
/// </Summary>
Public class focus
{
Public focus ()
{
}
// Set it to a static method. In this way, you do not need to create an instance object and call it directly in the form of focus. setfocus (this, "button2.
Public static void setfocus (system. Web. UI. Page, string m_focusedcontrol)
{
// If the control name is null, return
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) "); // if it is not null, set the focus. The method in Javascript called here
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 ("</");
SB. append ("script> ");

If (! Page. isstartupscriptregistered ("setfocusscriptname "))
Page. registerstartupscript ("setfocusscriptname", SB. tostring (); // write this JavaScript code to the page.
}
/// <Summary>
/// Set the control that triggers the event when you press enter on the page
/// </Summary>
/// <Param name = "Ctrl"> control object that triggers the event </param>
Public static void setentercontrol (system. Web. UI. Control CTRL)
{
Page mpage = ctrl. page;
String mscript;
Mscript = @ "<script language =" "JavaScript" ">
Function document. onkeydown ()
{
VaR E = event. srcelement;
VaR K = event. keycode;
If (k = 13 & E. type! = "" Textarea "")
{
Document. All. "+ ctrl. clientid + @". Click ();
Event. cancelbubble = true;
Event. returnvalue = false;
}
}
</SCRIPT> ";
If (! Mpage. isclientscriptblockregistered ("setentercontrol "))
Mpage. registerclientscriptblock ("setentercontrol", mscript );
}
}
}

Add: Start pilot Import
Namespace
Using system. text;
Using system. Web. UI;

In the web page
You can call the methods in the focus class to obtain the focus in page_load (,) or in the button_click event.
I used the button_click time.
The Code is as follows:
// Textbox1 is the ID of the control to obtain the focus
Focus. setentercontrol (this. textbox1 );
Focus. setfocus (textbox1.page, "textbox1 ");

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.