1. Generally speaking, if you want to add some JavaScript characteristics to the Web Form Control on the ASPX page, you can use Attributes.add to achieve.
For example, for Textbox TXT, you can: you can:
txt.attributes.add ("OnClick", "fcn0 ();");
Then, when it is on the web page, the FCN0 JavaScript function is called.
1.1. The exception is the analysis of the attributes that IDE cannot recognize.
For example, for a Radiobutton RBT, IDE cannot recognize the attribute of OnClick, then the above statement,
RBT.ATTRIBUTES.ADD ("OnClick", "FCN1 (this);");
In the .NET Framework 1.1
<input type = radio id = rbt> ...
In the .NET Framework 1.0
<span> <input type = radio id = rbt> ... </span>
Note that in FCN1, the object corresponding to the parameter this is different. This is a slight difference.
2. For HTML Control, you need to do more things.
When designing the ASPX page, drag a Web Form Control from the toolbar. For example, TEXTBOX to the page will happen: two things will happen:
1. ASPX page more sentence
<ASP: Textbox ID = "Textbox1" Runat = "Server"> </asp: Textbox>
Second, code behind
Protected system.web.ui.webcontrols.textbox textbox1;
If it is HTML Control, then in the first sentence, Runat = "Server" will not appear, and the second game will not be automatically added.
Therefore, if you want to access HTML Control, you need
1. Add runat = "Server" attribute to the statement of the ASPX page, which becomes
<input type = "text" size = "9" id = "htxt" runat = "server">
Second, declaration displayed in code behind
Protected system.web.ui.htmlControls.htmlinputtext;
Note that the ID of the first sentence is the same as the variable name of the second sentence.
2.1、注意到,前面system.web.ui.webcontrols.textbox对应的html control是system.web.ui.htmlcontrols.htmlinputtext,对应的html的tag是<input type="text">,
Correspondingly, the corresponding HTML Control of HTML Tag <Body> is
public system.web.ui.htmlControls.htmlgnericControl mybody;
2.2. One exception is the incident on the onSubmit incident corresponding to the <form> tag of HTML. Look at such an ASPX page
< %@ page language = "c#" codebehind = "webform2.aspx.cs" autoeventwareup = "false" inherits = "testcs.webform2" %>
<! Doctype html public "-// W3C // dtd html 4.0 transitional // en">>
<html>
<head>
<Title> Webform2 </Title>
<meta name = "general" 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" Runat = "Server" Text = "Button"> </ASP: Button>
<ASP: DropdownList ID = "DropdownList1" Runat = "Server" 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. Define a JavaScript function FCN1, put a Button Button1 and an AutoPostback DropdownList DropdownList1, run it, you can see: click Button1, then postBack, and select Dropdow Different options of nlist1 will only be Postback, not trigger FCN1.
Microsoft Autopostback = True's WebControl implementation Postback, the principle is:
First, if this ASPX page has the webcontrol of AutoPostback = True, then you will write a javascript function called __dopostback.
<script language = "javascript">
<!--
Function __DOPOSTBACK (EventTarget, EventArgument) {
var theform;
if (Window.navigator.appname.tologyCase (). Indexof ("Netscape")> -1) {{
theform = document.forms ["webform2"];
}
else {
theform = document.webform2;
}
TheForm .__ EventTarget.value = EventTarget.split ("$"). Join (":");
Theform .__ eventargument.value = eventArgument;
theform.submit ();
}
//->
</script>
2. For example, the above DropdownList will be render:
<select name = "DropdownList1" Onchange = "__dopostback ('Dropdownlist1', '')" Language = "Javascript" id = "DropdownList1">
<option value = "a"> a </option>
<option value = "b"> b </option>
<option value = "c"> c </option>
</select>
In this way, call theForm.Submit (); come to Submit Form, Postback, but theform.submit will not trigger Form's onSubmit event!
This is a bug of Microsoft.
The solution can be seen here: http://www.devhawk.net/art_submitfirefixup.ashx. Here is a DLL and source code. When used, add this DLL in the reference of Project, and then add it in the web.config. Previous paragraph
<httpmodules>
<add type = "devhawk.web.submitfirefixupModule, submitfirefixupModule" name = "submitfirefixupModule" /> /> />
</httpmodules>
That's fine.
3. A application.
I often hear complaints that if JavaScript changes a <select> element with JavaScript on the Browser, then the DropdownList on the corresponding Server side cannot know this update.
This situation may appear in the DropdownList of the "Class Federation". For example, the first DropdownList is the province, and the second is the city; it may also appear from the first DropdownList to join some items to the second DropdownList.
Using the above technology, I made such a solution (similar to the method of ViewState):
First, I define a Textbox txtwrap with 0 length and width, and add all the DropdownList I want to handle with attributes such as Athososw = "TRUE" to prepare for processing.
2. Refer to the content of the above 2.2 above, I joined the SubmitFirefixupModule to ensure the onSubmit event that triggers Form.
Third, FORM's ONSUBMIT event will execute the JavaScript function FCNATHOSONSUBMITWRAP, which traversed the DropdownList of the Athososwa attribute to TRUE, recorded the data, and finally merged into TXTWRAP. In fact, this is a serialization process. The code is as follows:
Function FCNathosonsubmitwrap ()
{{
txtwrap = document.all ["txtwrap"];
var I;
var strwrap = '';
for (i = 0; I <document.all.Length; i ++)
{{
ctrl = document.all [i];
if (ctrl.tagname.touppercase () == 'Select' && Typeof (ctrl.athososw)! = 'Undefined'))
{{
if (ctrl.athososw.touppercase () == 'true')
{{
Strwrap + = fcnathoswrapselect (ctrl) + '&&&;
}
}
}
if (Strwrap.length> 3)
txtwrap.value = strwrap.substring (0, strwrap.length-3);
};
// Athososw
Function FCNATHOSWRAPSELECT (CtrlSelect)
{{
var I;
Var Strwrapselect = Ctrlselect.id + '&' + Ctrlselect.tagname;
var strvalue = '';
var strtext = '';
for (i = 0; I <CtrlSelect.Options.Length; I ++)
{{
strvalue = ctrlselect.options [i]. Value;
strtext = ctrlselect.options [i] .text;
Strwrapselect + = '&&' + I + I + '&' + Strvalue.replace (/&/G, '%26') + '&' + StrText.replace (/&/g, '%26');
};
Return Strwrapselect;
};
Fourth, Form's Page_load is called CLSCOMMON.UNWRAPCONTROL (this, txtwrap.text); CLSCOMMON is my tool class. The UNWRAPControl method code is as follows:
STATIC PUBLIC VOID University
{{
regex r3 = new regex ("(" ("(" ("(" (&&&)); // split on hyphens.
regex r2 = new regex ("(" ("(" ("(&&)); // split on hyphens.
regex r1 = new regex ("(" (&)); // split on hyphens.
string [] SA3, SA2, SA1;
string s3, s2, s1;
int i3, i2, i1;
String Strid, Strtagname;
System.web.ui.control ctrlunwrap;
DropdownList ddlunwrap;
ListITEM Liadd;
s3 = strunwrap;
SA3 = R3.Split (s3);
for (i3 = 0; i3 <(SA3.Length+1)/2; i3 ++)
{{
s2 = SA3 [i3*2];
if (S2.Length> 0)
{{
SA2 = R2.Split (s2);
if (SA2.Length> 1)
{{
s1 = SA2 [0];
SA1 = R1.Split (s1);
if (SA1.Length == 3)
{{
strid = SA1 [0];
strtagname = SA1 [2];
ctrlunwrap = pgunwrap.findControl (strid);
if (ctrlunwrap! = NULL)
{{
If (Strtagname == "Select")
{{
ddlunwrap = (DropdownList) ctrlunwrap;
ddlunwrap.items.clear ();
for (I2 = 1; I2 <(SA2.Length+1)/2; I2 ++)
{{
s1 = SA2 [i2*2];
SA1 = R1.Split (s1);
liadd = new system.web.ui.webcontrols.listitem (SA1 [4], SA1 [2]);
ddlunwrap.Items.add (liadd);
}
}
}
}
}
}
}
}
Athossmth all rights reserved, please indicate.