简体   繁体   中英

Evaluate c# code as string in a aspx file

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.

eg 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?

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>

看一下这个项目,因为它帮助我解决了类似的问题: https//github.com/formosatek/dotliquid基本上你可以将任何对象绑定到模板,该模板可以调用你对象的属性,甚至可以使用条件逻辑和循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM