Share: ASPX page JavaScript Several tricks

xiaoxiao2021-03-06  16

1. In general, if you want to add some JavaScript features to the web form control on the ASPX page, you can use Attributes.Add to be implemented.

For example, for TextBox TXT, you can: txt.attributes.add ("onclick", "FCN0 ();" then, the FCN0 this JavaScript function will be called when Click is on the web page.

1.1, the exception is that the analysis of the attributes that cannot be identified.

For example, for a Radiobutton RBT, IDE cannot identify the onclick properties, then similar to the above statement, RBT.Attributes.add ("OnClick", "FCN1 (THIS);"); in .NET Framework 1.1, will be parsed into < INPUT TYPE = RADIO ID = RBT οnclick = "FCN1 (this);"> ... and in .NET Framework 1.0, it will be parsed into ... Note that the objects corresponding to the parameter THIS are different in FCN1. This is a subtle difference.

2, for HTML Control, you need to do more things.

When designing the ASPX page, drag from the toolbar, such as TextBox to the page, two things happen: 1. ASPX page Multiple sentence Second, code behind multi protected system.web.ui.webcontrols.textbox textBox1; if it is html control, then, first In the sentence, Runat = "server" does not appear, and the second game will not be added automatically. Therefore, if you want to access HTML Control, you need a Runat = "Server" property in the ASPX page statement, become Second, the declaration of the declared system.Web.ui.htmlControls.htmlinputText HTXT; note that the number of IDs and the number of variable names of the first sentence are the same.

2. Note that the HTML Control corresponding to the front system.web.ui.webcontrols.textbox is System.Web.ui.htmlControls.htmlinputText, the corresponding HTML tag is ,

Accordingly, HTML Tag corresponding HTML Control is a public system.web.ui.htmlControls.htmlGenericControl mybody; 2.2, there is a one-onSubmit's event for HTML

Tag. Look at such an ASPX page

<% @ Page language = "c #" codebehind = "Webform2.aspx.cs" autoeventwireup = "false" inherits = "testcs.webform2"%> 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" οnsubmit = "FCN1 ();" > <Asp: button id = "button1" style = "z-index: 103; left: 423px; position: absolute; top: 83px" runat = "width =" 86px "height =" 29px "text =" Button "> </ asp: button> <ask =" DropdownList1 "style =" z-index: 104; Left: 284px; position: absolute; top: 163px "Runat =" server "width =" 188px "Height = " 17px "autopostback =" true "> <asp: listitem value =" a "> a </ ask: listItem> <asp: listitem value =" b "> b </ asp: listItem> <ask: listitem value =" c "> c </ asp: listitem> <</p> <p>/ ask: DropDownList> </ form> </ body> </ html> Content is very simple, defined a JavaScript function FCN1, put a Button Button1 and an Autopostback DropDownList DropdownList1, run it, you can see: Click on Button1, The FCN1 will then execute the FCN1 and select DROPDOWNLIST1 will only postback, without triggering FCN1.</p> <p>Microsoft AutoPostback = True WebControl implements Postback, the principle is this:</p> <p>First, if this ASPX page has an autopostback = true WebControl, then the next section of the JavaScript statement will define a JavaScript function called __dopostback.</p> <p><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></p> <p>Second, for example, the above DropDownload, will be render into: <select name = "DropdownList1" οnchange = "__ dopostback ('dropdownload1', '')" Language = "JavaScript" id = "DropDownList1" style = "..." > <Option value = "a"> a </ option> <option value = "b"> b </ option> <option value = "c"> c </ option> </ select></p> <p>In this way, the form.submit () is called through Javscript; to Submit Form, Postback, but theform.submit will not trigger Form's onSubmit event!</p> <p>This is a bug of Microsoft.</p> <p>Solving the way you can see here: http://www.devhawk.net/art_submitfirefiXup.ashx, here is available here, when using, add this DLL in Project's Reference, then plus in Web.config Previous <httpmodules> <add type = "devhawk.web.submitfirefixupmodule, submitfirefiXupmodule" name = "submitfirefixupmodule" /> </ httpmodules>. 3, an application.</p> <p>I often hear complaints and say that if a <select> element is changed with JavaScript in the Browser end, the DropDownload's DropDownload's DropDownload cannot know this update.</p> <p>This situation may appear in the "cascaded" DropDownList, such as the first DropDownList is a province, the second is a city; it may also appear, select some item from the first DropDownList to join the second DROPDOWNLIST.</p> <p>For this technology, I made a solution (similar to viewState):</p> <p>First, I define a length of the TEXTBOX TXTWRAP and put all the properties that I want to deal with DropDownlist, ready to process. Second, refer to 2.2 above, I joined SubmitFirefixUPModule to ensure that Form's OnSubmit events were triggered. Third, the Form's onSubmit event will execute the JavaScript function FCNATHOSONSUBMITWRAP, which will traverse the ATHOSOSW attribute to True DropDownList, write down the data, and finally put it in txtwrap, in fact, this is a serialization process. The code is as follows: function fcnathosonSubmitWrap () {txtwrap = document.all ["txtwrap"];</p> <p>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.athosog.touppercase () == 'true') {strwrap = fcnathoswrapselect (ctrl) '&&&';}}}</p> <p>IF (strwrap.Length> 3) txtwrap.value = strwrap.substring (0, strwrap.length-3);</p> <p>// AthosOsw function fcnAthosWrapSelect (ctrlSelect) {var i; var strWrapSelect = ctrlSelect.id '&' ctrlSelect.tagName; var strValue = ''; var strText = ''; for (i = 0; i <ctrlSelect.options .length; i ) {strval = ctrlselect.Options [i] .value; strtext = ctrlselect.Options [i] .text; strwrapselect = '&&' i '&' strval.replace (/ & / g, '% 26') '&' Strtext.Replace (/ & / g, '% 26');}; return strwrapselect;}; 4, form's Page_Load call CLSCommon.UnwrapControl (this, txtwrap.text); Reverse sequencing. CLSCommon is my tool class, UnwrapControl method code is as follows:</p> <p>Static public void unwrapControl (System.Web.ui.page pgunwrap, strunwrap) {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 , stratagname; system.web.ui.control ctrlunwrap; dropdownloadlist ddlunwrap; listitem liadd;</p> <p>S3 = strunwrap; sa3 = r3.split (s3);</p> <p>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]; startAGName = SA1 [2]; ctrlunwrap = pgunwrap .FindControl (strike); if (ctrlunwrap! = Null) {if (stratagname == "SELECT") {ddlunwrap = (dropdownload) 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 (liad);}}}}}}}}}}}}}}</p> <p>ATHOSSMTH copyright, please specify please.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-44932.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="44932" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'CdurCDd7z1jIclhCxOU7_2BAwig8vBbMvf4wYULwT8M9vywKbh7lRM9vgdbikfKdYBWRdyuaOLem3us64J_2FR6EaA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>