Research on script resources in ASP. NET 2.0 (1)

Source: Internet
Author: User

Research on webresource in ASP. NET 2.0 (1)

Author: Truly

For ASP. NET 2.0, many script resource files are provided to embed resources inProgramCentralized:
Webforms. js
Webuivalidation. js
Smartnav.htm
Smartnav. js
Menu. js
Microsoftajaxwebforms. js
Microsoftajax. js
And so on

After we change the page attribute or add some server controls, the page automatically registers the reference addresses of these resources to the page.
For example, if maintainscrollpositiononpostback is enabled, the following statements are automatically registered on the page:

< Script SRC = " /Webresource. axd? D = znl1cdiew0dlwtt0rctn7a2 & amp; T = 633118963218359345 " Type = " Text/JavaScript " > </ Script >

 

This task is completed by the getwebresourceurl method of system. Web. Handlers. assemblyresourceloader.
It should be noted that this method is like many other methods. For some purpose, Ms defines it as an internal method. We know that, methods marked as internal can only be called by other classes in the same assembly. Therefore, we cannot manually register this script resource reference.
Fortunately, we do not need to do this in most cases.

However, because these resources define many script functions commonly used in webform, sometimes you may want to use these functions, so how can we get this script reference or register it on the page?

Here we will first introduce a technique for calling some internal methods in the. NET Framework class library. Actually, reflection is used:

Access the private or internal Member of the. NET class

Type T =   Typeof (System. Web. Handlers. assemblyresourceloader );
Object [] ARGs =   New   Object [] { Typeof (Page ), " Webforms. js " , True };

Bindingflags=Bindingflags. declaredonly|Bindingflags. Static|Bindingflags. nonpublic|Bindingflags. invokemethod;

string result = ( string ) (T. invokemember ( " getwebresourceurl " , bindingflags, null , null , argS ));

page. clientscript. registerstartupscript ( typeof (_ default ), " ABC " , String. format ( " " , result);

The above method is to directly obtain the correct resource file and register it on the page.

The following introduces another method, that is, calling the registerwebformsscript method of the page Class Using Reflection:

 

Type T =   Typeof (System. Web. UI. Page );
Bindingflags
= Bindingflags. declaredonly | Bindingflags. Static | Bindingflags. nonpublic | Bindingflags. invokemethod | Bindingflags. instance;
T. invokemember (
" Registerwebformsscript " , Bindingflags, Null , This , Null );

 

You can carefully compare the differences between the two in the runtime. In terms of presentation, the former is more flexible, but it may be possible to reregister the script reference, and the subsequent methods will not. In fact, for the former, a private variable in the page class records whether the page has been registered with a script reference. The field name is _ fwebformsscriptrendered, check whether the Script Reference has been registered on the page based on this variable.

In ASP. net2.0, another method is simpler:

This . Page. clientscript. registerclientscriptresource ( Typeof (System. Web. UI. Page ), " Webforms. js " );

The other statement is also useful. It is usually used to obtain custom embedded file references:

String Includelocation =
Page. clientscript. getwebresourceurl (
This . GetType (), " Mystylesheet.css " );

Here we can use:

String Includelocation =
Page. clientscript. getwebresourceurl (
Typeof (Page ), " Webforms. js " );

The URL format referenced by the resource is described as follows:

Webresource. axd? D = encrypted ID & t = Timestamp

Think 1Can fixed SRC be used to call resources? The answer is no.
Thinking 2Can it be used in HTML? The answer is yes, as long as the site is placed in ASP. in the net2.0 environment, it is usually supported. file request with the axd suffix, for webresource. requests like axd will be forwarded to system. web. handlers. assemblyresourceloader.
Think 3, Performance, unobfuscated.
Thinking 4, Script reuse.

So what methods does webform. js contain? I sorted it out. The method list is as follows:
 

Webform_postbackoptions (eventtarget, eventargument, validation, validationgroup, actionurl, trackfocus, clientsubmit)
Webform_dopostbackwitexceptions (options)
Webform_docallback (eventtarget, eventargument, eventcallback, context, errorcallback, useasync)
Webform_callbackcomplete ()
Webform_executecallback (callbackobject)
Webform_fillfirstavailableslot (array, element)
Webform_initcallback ()
Webform_initcallbackaddfield (name, value)
Webform_encodecallback (parameter)
Webform_reenablecontrols ()
Webform_redisablecontrols ()
Webform_firedefaultbutton (event, target)
Webform_getscrollx ()
Webform_getscrolly ()
Webform_savescrollpositionsubmit ()
Webform_savescrollpositiononsubmit ()
Webform_restorescrollposition ()
Webform_textboxkeyhandler (Event)
Webform_appendtoclassname (element, classname)
Webform_removeclassname (element, classname)
Webform_getelementbyid (elementid)
Webform_getelementbytagname (element, tagname)
Webform_getelementsbytagname (element, tagname)
Webform_getelementdir (element)
Webform_getelementposition (element)
Webform_getparentbytagname (element, tagname)
Webform_setelementheight (element, height)
Webform_setelementwidth (element, width)
Webform_setelementx (element, X)
Webform_setelementy (element, Y)

 
 
 
 
Field:
_ Disabledcontrolarray
Forbidden control array
_ Enabledcontrolarray depends on JS external

 

Function Name Description
Webform_postbackoptions (eventtarget, eventargument, validation, validationgroup, actionurl, trackfocus, clientsubmit) Add the specified callback parameter to this object as a property.
Webform_dopostbackwitexceptions (options) Set the actionurl and the last focus control and event parameters based on the data information generated by the webform_postbackoptions method.
Webform_docallback (eventtarget, eventargument, eventcallback, context, errorcallback, useasync) Callback Function
Webform_callbackcomplete () Callback completion processing function
Webform_executecallback (callbackobject) Execute callback
Webform_fillfirstavailableslot (array, element) Add the specified object to the first null position in the specified array.
Webform_initcallback () Initialize the callback and add the data of all input, select textarea, and other controls to the corresponding array and variable using the webform_initcallbackaddfield method.
Webform_initcallbackaddfield (name, value) Create a custom object that contains the passed key value attribute to be added to the _ theformpostcollection array and added to the _ theformpostcollection variable as a string of "name = Value &"
Webform_encodecallback (parameter) Uri encoding of callback parameters depends on the encodeuricomponent method of other JS class libraries.
Webform_reenablecontrols () Find all the disabled controls in the _ enabledcontrolarray array, enable them, and record them to _ disabledcontrolarray.
Webform_redisablecontrols () Disable the controls saved by _ disabledcontrolarray.
Webform_firedefaultbutton (event, target) When you press the enter button in the area except textarea, the specified default button is triggered. Target is the Client ID of the default button.
Webform_getscrollx () Obtain the x-axis coordinate of the current page (document. Body. scrollleft)
Webform_getscrolly () Obtain the y-axis coordinate of the current page (document. Body. scrolltop)
Webform_savescrollpositionsubmit () When submitting a form, save the scroll axis coordinates. The following three functions use the _ scrollpositionx hidden field in the form.
Webform_savescrollpositiononsubmit () Different from the preceding method, this method is only valid in IE browsers.
Webform_restorescrollposition () Restore the position of the scroll bar saved before the submission form
Webform_textboxkeyhandler (Event) It can be used to set the specified textbox to not respond to the carriage return event, for example: <input onkeydown = 'webform _ textboxkeyhandler (event )'
Webform_appendtoclassname (element, classname) Adds a specified style to a specified element.
Webform_removeclassname (element, classname) Deletes a specified style of a specified element.
Webform_getelementbyid (elementid) Obtains the element of a specified ID.
Webform_getelementbytagname (element, tagname) Obtains the first element of a specified tag in a specified element.
Webform_getelementsbytagname (element, tagname) Obtains the set of elements marked in a specified element.
Webform_getelementdir (element) Obtains the Dir attribute of a specified element.
Webform_getelementposition (element) Obtains the location and other information of a specified element.
Return type: custom object (VAR result = new object (); Result. x = 0; Result. Y = 0; Result. width = 0; Result. Height = 0 ;)
Webform_getparentbytagname (element, tagname) Obtain the most recent tagname of an element as the parent node of the specified tag.
Webform_setelementheight (element, height) Set the height of an element
Webform_setelementwidth (element, width) Set the width of an element.
Webform_setelementx (element, X) Set Y coordinates of an element
Webform_setelementy (element, Y) Set Y coordinates of an element

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.