Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate c# code as string in a aspx file

Tags:

string

c#

I have this problem: From a database, held up a string, which contains HTML mixed with C# code. I wish I could run correctly both codes on my page .aspx.

e.g. in my .aspx:

<div><%= Model.repo.getCode() %></div>

and the getCode() method give me this:

<div id="secondDiv"><p><%= Model.Person.Name %></p></div>

so I want the final html file look like:

<div><div id="secondDiv"><p>Jhon</p></div></div>

any suggestion?

like image 342
user1916015 Avatar asked Dec 19 '12 14:12

user1916015


People also ask

How do you evaluate C expressions?

C Expression Evaluation In the C programming language, an expression is evaluated based on the operator precedence and associativity. When there are multiple operators in an expression, they are evaluated according to their precedence and associativity.

What does evaluate mean C?

Expression evaluation in C is used to determine the order of the operators to calculate the accurate output. Arithmetic, Relational, Logical, and Conditional are expression evaluations in C.

Does C evaluate right to left?

There is no concept of left-to-right or right-to-left evaluation in C, which is not to be confused with left-to-right and right-to-left associativity of operators: the expression f1() + f2() + f3() is parsed as (f1() + f2()) + f3() due to left-to-right associativity of operator+, but the function call to f3 may be ...

What does '%' mean in C?

In C language '%' is known as modulus operator. It is used to find remainder. e.g- 15%2=1. In the above example when we divide 15 by 2 the remainder will be 1. Hence by using '%' operator we can find the remainder.


1 Answers

There may be direct way to bind such value, But if you could store String.Formatable into database then it would be easy to bind the data needed.

Using String.Format you achieve like,

returned string from Model.repo.getCode() (see curly braces)

    "<div id="secondDiv"><p>{0}</p></div>"; 

And in ASP code,

    <div><%= string.format(Model.repo.getCode(),Model.Person.Name) %></div>
like image 200
indiPy Avatar answered Oct 02 '22 04:10

indiPy