Declaration, these 2nd articles are not self-written, is from the msdn http://www.microsoft.com/china/msdn/library/webservices/asp.net/JAVAwASP2.mspx? MFR = true
Add JavaScript to the server control
Adding JavaScript to a specific server control on the ASP. NET page is very simple. Take the button server control as an example. If you use any Microsoft Visual Studio 2005 (HtmlinputbuttonClass) drag to a page and run it as a server control, the following code structure should be available:
<input id="Button1" type="button" value="button" runat="server" />
This is a common button that can be controlled programmatically by code separation on ASP. NET pages or server scripts. For example, to specify the button text when a page is generated, you only need to change the element to an HTML Server Control (right-click the control and selectRun as Server Control(Run as a server control ).ValueAttribute.
Visual Basic
Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Button1.Value = DateTime.Now.ToString()End Sub
C #
protected void Page_Load(object sender, EventArgs e){ Button1.Value = DateTime.Now.ToString();}
This Code only provides a button on the page. The text of this button is date and time.
Figure 1. display the date and time on the button
Note that the ASP. NET page here gets the time from the server that generates the page. Therefore, if the web server is located at a location in the US Central Time Zone (CST-6 GMT), the web server will get the same time no matter where the person requesting the page is located.
What if you want this button to display the time zone of the person on the page? The easiest way to complete this task is to use JavaScript on the client.
For example, we want to place the computer time of an end user (web page viewer) on a button Web Server Control. The following code shows how to complete the task:
Visual Basic
<% @ Page Language = "VB" %> <SCRIPT runat = "server"> protected sub button#click (byval sender as object, _ byval e as system. eventargs) response. write ("send back! ") End sub </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml">
C #
<% @ Page Language = "C #" %> <SCRIPT runat = "server"> protected void button#click (Object sender, eventargs e) {response. Write ("resend! ");} </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml">
In this Code segment, note how the buttons are specified to the server before being sent to the client browser. In this example, the text font on the button is changed to verdana in bold with a specific size. After the client receives the HTML code of the button, the client JavaScript changes the text of the button to the current time on the terminal user's computer. The HTML code generated for the entire page is as follows:
<HTML xmlns = "http://www.w3.org/1999/xhtml">
You can click this button to send a message again.Response. WriteCommand to view). When this page is re-displayed, the button control displays the new time. Result 2 is displayed.
Figure 2. Click date
In this example, we useOnloadProperty directly places some JavaScript code on the page<Body>Element. ForOnloadAttribute value, we specifically point to the first<Form>Section (because there may be multiple forms in HTML) namedButton1.
Although this method is used to add some JavaScript to work with ASP. net web server controls are easy to use, but we can also easily add a javascript command to the button itself, as shown in the following code example:
Visual Basic
<% @ Page Language = "VB" %> <SCRIPT runat = "server"> protected sub page_load (byval sender as object, _ byval e as system. eventargs) button1.attributes. add ("onclick", _ "javascript: Alert ('pay more attention !!! ')") End sub </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml">
C #
<% @ Page Language = "C #" %> <SCRIPT runat = "server"> protected void page_load (Object sender, eventargs e) {button1.attributes. add ("onclick", "javascript: Alert ('pay more attention !!! ')");} </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml">
UseAttributeProperty to add the additional JavaScript code to the control-specific controls. In this exampleAttribute. AddAttribute and script keyword and script itself (both representStringAdded JavaScript.
Back to Top
Execute simple button flip
When talking about buttons on a web page, the more common function that web developers want to assign to buttons is the flip effect. The flip effect is that when the end user places the mouse on a button on the web page (without clicking the button), the color and shape of the button will change. This function is particularly useful for web pages with multiple buttons, the end user is notified to click the button before clicking it.
This operation was easy to implement before the server control appeared. Now, even with the server control, it is not that difficult. The code for executing a similar operation is as follows:
Visual Basic
<% @ Page Language = "VB" %> <SCRIPT runat = "server"> protected sub imagebutton#click (byval sender as object, _ byval e as system. web. UI. imageclickeventargs) label1.text = "send back! "End sub </SCRIPT> <HTML xmlns =" http://www.w3.org/1999/xhtml ">
C #
<% @ Page Language = "C #" %> <SCRIPT runat = "server"> protected void imagebutton#click (Object sender, imageclickeventargs e) {label1.text = "resend! ";} </SCRIPT> <HTML xmlns = "http://www.w3.org/1999/xhtml">
This time we failed<Body>The element specifies JavaScript to the server control, but uses the control'sOnmouseoverAndOnmouseoutEvent. For each event, we specify a javascript value.OnmouseoverThe event indicates that the end user places the mouse over the control.OnmouseoutThe operation that the end user moves from the top of the control. In this example, we want an image to be displayed when the mouse is placed above the button, and the original image will be displayed after the page is loaded and when the mouse is removed from the button.
If you are directly using this type of control<Body>When Javascript is used in the element, you can specify the control in form.ThisKeyword, followed by the attribute you are trying to change.
Back to Top
Set control focus
ASP. NET 2.0 now includes the ability to set (the cursor's) Focus for an HTML form element. Before ASP. NET 2.0, you must use JavaScript to complete the same task. For example, if your ASP. NET 1.XIf there are multiple text boxes on the page, you can<Body>Use the following code to set the focus of the page to the first one after loading.TextboxControl.
<body onload="document.forms[0]['TextBox1'].focus();">
By using this constructor Code, when the page is loaded, it contains the IDTextbox1So that the end user can start to directly input text, without the need to use the mouse to locate the focus.
ASP. NET 2.0 by addingFocus ()This method makes the task very simple. Now, you can use the following code to completeTextboxControl focus settings:
Visual Basic
Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) TextBox1.Focus()End Sub
C #
protected void Page_Load(object sender, EventArgs e){ TextBox1.Focus(); }
After the browser loads the page using this method, the cursor is placed inside the text box, waiting for you to start typing the text. Therefore, you do not have to move the cursor to the corresponding position to start entering information in the form.Focus ()The method allows you to dynamically place the end user's cursor in the specified form element (not justTextboxControl, but can be fromWebcontrolAny Server Control derived from the class ).