使用&LT;%=%GT;或LT;%#%&GT;在ASP.NET RUNAT =服务器 [英] using <%= %> or <%# %> with runat=server in ASP.NET
问题描述
我有一个Web控制,看起来像这样
I have a web control that looks like this
public class Foo : WebControl
{
[Bindable(true)]
[Category("Default")]
[DefaultValue("")]
[Localizable(true)]
public string Bar { get; set; }
protected override void Render(HtmlTextWriter output)
{
output.WriteLine(Bar);
}
}
我想把这个三夏在我的aspx页面像这样:
I want to put this webcontrol in my aspx page like so:
<cc1:Foo Bar="<%= Fa.La.La %>/otherstuff" runat="server" />
(显然这code简化为显示问题)
(obviously this code is simplified to show the problem)
在我的Render方法变量Fa.La.La未评估。它的到来作为原始文本&LT;%= Fa.La.La%>我怎么评价它?
In my Render method the variable Fa.La.La is not evaluated. It's coming in as the raw text "<%= Fa.La.La %>" How do I evaluate it?
我不是特别的变量是如何传递如果变量如果他们在为&lt传递进行评估;%#...%>,工作正常。问题的关键是我有之前我要评估一些服务器端的变量/而我的渲染()方法被调用。
I'm not particular how the variables are passed in. If the variables can be evaluated if they are passed in as <%# ... %>, that works fine. The point is I have some server-side variables I want evaluated before/while my Render() method is called.
我能想到的唯一的事情就是用正则表达式抢的内容和LT;%= ...%>和使用反射或东西,但必须有一个更优雅的方式来做到这一点
The only thing I can think of is to use a regex to grab the contents of <%= ... %> and use reflection or something, but there has to be a more elegant way to do this.
这个问题是类似于A HREF&LT使用服务器变量来 pretty;%= XX%&GT;与RUNAT =服务器,但它不是完全一样的,因为没有一个答案有有益的。
This question is pretty similar to using server variables in a href <%= xx %> with runat=server, but it's not exactly the same since none of the answers there were useful.
推荐答案
您应该使用绑定前pression &LT;%#EXPR%GT;
。
You should have to use binding expression <%# expr %>
.
<cc1:Foo Bar='<%# String.Concat(Fa.La.La,"/otherstuff")%>' runat="server" />
和呼叫的DataBind()
方法code-落后。
and call DataBind()
method in code-behind.
public void page_load()
{
DataBind();
}
这篇关于使用&LT;%=%GT;或LT;%#%&GT;在ASP.NET RUNAT =服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!