ASPX page several trick of JavaScript

Source: Internet
Author: User
Tags visual studio
Page 1, in general, if you want to add some JavaScript features to the Web Form control on an ASPX page, you can do it with Attributes.Add.
For example, for TextBox txt, you can:
Txt. Attributes.Add ("onclick", "fcn0 ();");
Then, when you click it on a Web page, you call Fcn0 this JavaScript function.

1.1. The exception is the parsing of properties that the IDE cannot recognize.

For example, for a RadioButton rbt,ide cannot recognize the onclick attribute, then, like the above statement,
Rbt. Attributes.Add ("onclick", "fcn1 (this);");
In the. NET Framework 1.1, you will resolve to
<input type=radio id=rbt onclick= "fcn1 (this);" "..."
And in the. NET Framework 1.0, it will be parsed into
<span onclick= "fcn1 (this);" ><input Type=radio id=rbt>...</span>
Note that in FCN1, the parameter this corresponds to a different object. This is a subtle difference.

2, and for HTML control, need to do a little more things.

When you design an ASPX page, drag a Web Form control from the toolbar, such as a TextBox to a page, two things happen:
One, aspx page one more sentence
<asp:textbox id= "TextBox1" style= "runat=" Server "width=" 102px "height=" 25px "></asp:TextBox>
Second, the code behind one more sentence
protected System.Web.UI.WebControls.TextBox TextBox1;
If HTML control is the first sentence, runat= "server" does not appear, and the second inning is not automatically added.
Therefore, if you want to access HTML control, you need
The runat= "Server" attribute is added to the statement in the ASPX page to become
<input style= "..." type= "text" size= "9" id= "Htxt" runat= "Server" >
Ii. statements shown in code behind
protected System.Web.UI.HtmlControls.HtmlInputText htxt;
Notice that the ID of the first sentence and the variable name of the second sentence are the same.

2.1. Note that the preceding System.Web.UI.WebControls.TextBox corresponds to the HTML Control is System.Web.UI.HtmlControls.HtmlInputText, the corresponding HTML tag is <input type= "text";

Accordingly, the HTML tag <body> corresponding HTML control is
Public System.Web.UI.HtmlControls.HtmlGenericControl Mybody;

2.2, with one exception is the HTML <form> tag corresponding to the onsubmit event. Look at an ASPX page like this

<%@ Page language= "C #" codebehind= "WebForm2.aspx.cs" autoeventwireup= "false" inherits= "Testcs.webform2"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name= "generator" content= "Microsoft Visual Studio 7.0" >
<meta name= "Code_language" content= "C #" >
<meta name= "vs_defaultClientScript" content= "JavaScript" >
<meta name= "vs_targetschema" content= "http://schemas.microsoft.com/intellisense/ie5" >
<script language= "JavaScript" >
function Fcn1 ()
{
Prompt ("Hi", "fcn1");
}
</script>
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "WebForm2" method= "POST" runat= "server" onsubmit= "fcn1 ();" >
<asp:button id= "Button1" style= "z-index:103; left:423px; Position:absolute; TOP:83PX "runat=" Server "width=" 86px "height=" 29px "text=" button "></asp:Button>
<asp:dropdownlist id= "DropDownList1" style= "z-index:104; left:284px; Position:absolute; TOP:163PX "runat=" Server "width=" 188px "height=" 17px "autopostback=" True ">
<asp:listitem value= "a" >a</asp:ListItem>
<asp:listitem value= "B" >b</asp:ListItem>
<asp:listitem value= "C" >c</asp:ListItem>
</asp:DropDownList>
</form>
</body>
</HTML>

The content is very simple, defined a JavaScript function fcn1, put a button Button1 and a AutoPostBack DropDownList DropDownList1, run it, you can see: Click Button1, The FCN1 and postback are executed first, and the DropDownList1 options are selected, and the FCN1 is not triggered except for the postback.

Microsoft Autopostback=true WebControl Implementation of postback, the principle is this:

First, if the ASPX page has a autopostback=true WebControl, a JavaScript function called __doPostBack is defined by writing the following JavaScript statement.

<script language= "JavaScript" >
<!--
function __doPostBack (Eventtarget, eventargument) {
var theform;
if (Window.navigator.appName.toLowerCase (). IndexOf ("Netscape") >-1) {
Theform = document.forms["WebForm2"];
}
else {
Theform = document. WebForm2;
}
Theform.__eventtarget.value = Eventtarget.split ("$"). Join (":");
Theform.__eventargument.value = eventargument;
Theform.submit ();
}
-->
</script>

Two, for example is the above DropDownList, will render into:
<select name= "DropDownList1" onchange= "__doPostBack



Related Article

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.