locked
reading the Value of a variable xy RRS feed

  • Question

  • User883541315 posted

    In asp classic I have the following procedure to give over the value of variable xy to Value1

    ...

    Function Func_Replace(Variable)
    ...
        Value1 = Eval(Variable)
    ...
    end function

    now: Eval(...) doesn't work anymore in asp.net. How can I give over the Value of the variable xy to the new variable "value1"?

    Best regards

    Chris

    Tuesday, August 20, 2019 9:47 AM

All replies

  • User475983607 posted

    This is similar to your other thread(s).  In VbSCript Eval() executes a function and returns the results using the name of the function.  In .NET you design classes to do the same.  Rather than having an include file or writing a string expression you instantiate a class and invoke a method.  Keep in mind that eval() in classic ASP was considered a poor design as using eval() can easily lead to nasty injection vulnerabilities.  You really should rethink how you are designing the application.

    In Class asp...

        <%
            Dim result
    
            function Add(num1, num2 )
                Add = num1 + num2
            end function
    
            result = eval("Add(2,3)")
            Response.Write "<h1>" & result & "</h1>"
    
        %>

    In .NET, create a class.

        public class SimpleMath
        {
            public static int Add(int num1, int num2)
            {
                return num1 + num2;
            }
        }

    Then invoke the method anywhere within the project.

    int result = SimpleMath.Add(2, 3);

    Tuesday, August 20, 2019 10:37 AM
  • User883541315 posted

    thanks for your answer.

    I tried your code but I got the same error message :"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."

    well I have to be more exact:
    my problem is that I have only the names of the variables as a string - e.g. Variable="IsClosedUserGroup" or Variable="RecordID" or Variable = "Age", etc.
    But for calculation I need the value of this variable. To the function Func_GetValue(Variable) I am only giving the name of the variable (e.g. func_GetValue("RecordID") or func_GetValue("Age") ) to the function. The function should now return the value of the variable. In asp classic this was possible by eval(Variable).

    Eval() I can use only in context with a datafield.

    But now I am looking now for a way to get out the value of a variable (as a string) 

    E.g.
    IsClosedUserGroup = 1
    age = 28
    RecordID=3723

    Value = Func_GetValue("IsClosedUserGroup") ==> Value = 1
    Value = Func_GetValue("age") ==> Value = 28
    Value = Func_GetValue("RecordID") ==> Value = 3726

    Tuesday, August 20, 2019 11:40 AM
  • User753101303 posted

    Hi,

    mgebhard shown classic ASP code and what to do instead so it is expected that Eval doesn't work this way in ASP.NET (seems you tried to run the ASP classic code using ASP.NET)

    But why do you have the name of the variable in a string rather than using variables directly in your code? As pointed already it could be better to avoid bringing into ASP.NET old (and possibly bad) habits coming from ASP classic.

    Seems like you are trying to use "data binding" so if new to Web Forms I would suggest to have a look at https://docs.microsoft.com/en-us/aspnet/web-forms/overview/presenting-and-managing-data/model-binding/retrieving-data. You have also other older options to bind your UI to a data source using the name of the source property.

    If you try to use that in a more general context, at worst maybe a https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8 could help. It allows to store a unique key (the "variable" name) and the corresponding value.

    If you REALLY want to keep that you could likely reuse https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.databinder?view=netframework-4.8 (which is the feature behind one of the ASPX data binding approach) in a general context :

    using System;
    using System.Web.UI;
    
    namespace ConsoleApp1
    {
        class Data
        {
            public int MyProp { get; set; } = 10;
        }
        class Program
        {
            static void Main(string[] args)
            {
                var data = new Data();
                Console.WriteLine(DataBinder.GetPropertyValue(data, "MyProp")); // Shows 10
            }
        }
    }

    For now I believe you are trying to do something for which ASP.NET offers a better solution out of the box.

    Tuesday, August 20, 2019 12:46 PM
  • User-1716253493 posted

    Try like this

     <%# string.Format("{0}",DataBinder.Eval(Container.DataItem,"paramname")) %>

    Wednesday, August 21, 2019 1:18 AM