Solution to the problem that eval cannot define variables in asp.net

Source: Internet
Author: User
Tags constant eval function definition table name versions visual studio visual studio 2010

 eval can not define the problem of variables to do, there is a good solution, you can refer to the following

The code is as follows: eval.asp  <%@ language= ' javascript '%>  <script language= ' JavaScript ' runat=server>  Eval ("Var f1=1,f2=2,f3=3;");   Response.Write (f1+ "<br/>");  Response.Write (f2+ "<br/>");  Response.Write (f3+ "<br/") > ");  </script>  Run results:  1  2  3  eval01.aspx  <%@ ' JAVASCRIPT ' Debug= "true"%>  <script language= "javascript" runat=server>  eval ("var f1=1,f2=2,f3=3;");   Response.Write (f1+ "<br/>");  Response.Write (f2+ "<br/>");  Response.Write (f3+ "<br/") > ");  </script>  the third line has syntax errors!  you can solve the problem of defining variables by table fields by dynamically adding properties to an empty object:  table name:t  F1 int,   F2 char (a),  F3 datetime  asp:  eval ("var f1= ', f2= ', f3= ';");   Response.Write ("f1=" +f1);  aspx:  var t={};  t["F1"]= "";  t["F2"]= "";  t["F3"]= "";   Response.Write ("f1=" +t.f1);  <%@ language= ' JScript ' debug= ' true '%>  <%  var tab={ };  var n=12;  var fld;  for (Var i=0;i<n;i++)   {  fld= "F0" + (i&lt ; 10)? ("i<100"):(? ("0"):()) +i;  tab[fld]=i+1000; }  for (var i=0;i<n;i++)   {  fld= "F0" + ((i<10)? (" ):((i<100)? ("0"):("")) +i;  Response.Write (tab[fld]+ "<br/>"); } %>    :    http://msdn.microsoft.com/zh-cn/library/8e4z2w8w (v=vs.90) on Jscript8.0 on Microsoft's official website. aspx# jsconupgradingapplicationcreatedinpreviousversionsofjscriptanchor7    Upgrade applications created in previous versions of JScript     Visual Studio 2008    Other versions     Visual Studio 2010    Visual Studio 2005    This topic has not been rated-evaluate this topic     Update: November 2007     Most existing JScript code makes good use of JScript The enhancements included in 8.0, because JScript 8.0 is almost completely backwards compatible for previous versions. The new features of JScript 8.0 create a New world.     The JScript 8.0 program is compiled in quick mode by default. Because quick mode has some restrictions on the type of code that is allowed, programs can be more efficient and execute faster. However, some of the features available in previous versions are not available in quick mode. Most of these features are incompatible with multi-threaded applications and make generationCode inefficiencies. For programs compiled with the command-line compiler, you can turn off quick mode and take advantage of full backward compatibility. Note that code compiled in this way runs slower and is less fault tolerant. Fast mode cannot be turned off in the ASP.net application because of the stability problem. For more information, see/fast.     Quick mode     in quick mode, the following JScript behavior is triggered:    All variables must be declared.     The function becomes a constant.     Internal objects cannot have expando properties.     You cannot list or change the properties of an internal object.     The arguments object is not available.     You cannot assign a value to a read-only variable, field, or method.     The Eval method cannot define identifiers within the enclosing scope.     The Eval method executes the script in the restricted security context.     must declare all variables     Previous versions of JScript do not require explicit declaration of variables. Although this feature saves the programmer the number of keystrokes, it makes tracking errors difficult. For example, you might assign a value to a misspelled variable name, which would neither generate an error nor return the desired result. Also, undeclared variables have global scope and can cause other confusion.     Quick mode requires a declaration variable to be displayed. This helps to avoid the various errors mentioned earlier and can produce code that runs faster.     JScript. NET also supports variables of type annotation. This binds each variable to a specific data type, which can store only that type of data. Although type annotations are not required, they can be used to help avoid errors associated with accidentally storing error data in variables and to increase the speed at which programs are executed.     For more information, see JScript variables and constants.     function becomes constant   in previous versions of JScript, functions declared with function statements are treated as equals to variables that hold function objects. In particular, any function identifier can be used as a variable to store any type of data.     in fast mode, functions become constants. Therefore, you cannot assign new values or redefine functions for a function. This avoids accidentally changing the meaning of the function.     If your script needs to make a function change, you can explicitly use a variable to hold an instance of the functions object. Note, however, that the Function object is slow to transport. For more information, see Function objects.     Internal objects cannot have expando properties     In previous versions of JScript, you can add expando properties to internal objects. For example, this behavior can be used to add a method to a string object to trim the space before the string.     In quick mode, this is not allowed. If your script uses this feature, you must modify the script. You can define functions globally, rather than attaching those functions to objects as methods. Then, rewrite each instance in the script (in which the expando method is invoked from the object) to pass the object to the appropriate function.     An important exception to this rule is the Global object, which can still have the expando attribute. All modifiers in the global scope are actually properties of the global object. Obviously, the global object must be dynamically extensible to support the addition of new global variables.     cannot list or change properties of internal objects     In previous versions of JScript, you can delete, enumerate, or write to predefined properties of an internal object. For example, this behavior can be used to change the default ToString method for a Date object.     In quick mode, this is not allowed. Because internal objects cannot have expando properties, this feature is no longer required, and the properties of each object are listed in the reference section. For more information, see objects.     Arguments object not available     Previous versions of JScript provide a arguments object in the function definition that allows a function to accept any argument. The Parameter object can also reference the current function and the calling function.     In quick mode, the arguments object is not available. However, JScript 8.0 allows function declarations to specify an array of parameters in the function argument list. This allows the function to accept any number of arguments, replacing some of the functionality of the arguments object. For more information, see function statements.     There is no way to directly access and reference the current function or call function in quick mode.     Cannot assign a value to a read-only variable, field, or method &nbSp   In previous versions of JScript, statements seemed to be able to assign values to read-only identifiers. This assignment will fail silently, and the only way to find the assignment to fail is to test whether the value actually changed. Assigning a value to a read-only identifier is usually caused by an error because it does not have any effect.     In quick mode, if you attempt to assign a value to a read-only identifier, a compile-time error is generated. Either you can remove the assignment or you can try to assign a value to an identifier that is not read-only.     If quick mode is turned off, assigning a value to a read-only identifier will fail silently at run time, but a compile-time warning will be generated.     Eval method cannot define identifiers in enclosing scope     In previous versions of JScript, functions and variables can be defined locally or globally by invoking the Eval method.     in quick mode, functions and variables can be defined in calls to the Eval method, but they can only be accessed from this particular invocation. Once the eval has been completed, functions and variables defined within the eval can no longer be accessed. The computed results within the eval can be assigned to any variable that is accessible in the current scope. Calls to the Eval method are slow, and you should consider overriding the code that contains those calls.     The previous behavior of the Eval method can be restored when quick mode is turned off.     Eval method executes scripts in a restricted security context     In previous versions of JScript, code passed to the Eval method would run in the same security context as the calling code.     to protect the user, code passed to the Eval method executes in the restricted security context unless the string "unsafe" is passed as the second argument. Restricted security contexts prohibit access to system resources, such as file systems, networks, or user interfaces. If your code attempts to access these resources, a security exception is generated.     When the second parameter of eval is the string "unsafe", the code passed to the Eval method executes in the security context in which the calling code resides. In this way, the previous behavior of the Eval method can be restored.     Safety Note:    using eval in unsafe mode only code strings obtained from known sources  
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.