I have a custom server control with a property of Title. When using the control, I'd like to set the value of the title in the aspx page like so:
我有一个自定义服务器控件,其属性为Title。使用控件时,我想在aspx页面中设置标题的值,如下所示:
<cc1:customControl runat="server" Title='<%= PagePropertyValue%>' >
more content
</cc1:customControl>
When I do this, however, I am getting the exact String <%= PagePropertyValue%> being displayed rather than the Property Value that I would like to see.
但是,当我执行此操作时,我将显示正确的字符串<%= PagePropertyValue%>而不是我希望看到的属性值。
So after trying the databinding expression (as suggested below). I don't get the string literal that looked bad but I don't get anything else either.
所以在尝试数据绑定表达式后(如下所示)。我没有得到看起来很糟糕的字符串文字,但我也没有得到任何其他东西。
<cc1:customControl runat="server" Title='<%# PagePropertyValue%>' >
more content
</cc1:customControl>
What do I need to do to my custom control to take this sort of value? Or is there something I need to do to the page.
我需要对自定义控件做什么才能获得这种价值?或者我需要对页面做些什么。
5 个解决方案
#1
7
You cant. <%= %> will write the string directly to the response-stream, which happens after the server control is constructed. See this post for an explanation.
你不能。 <%=%>会将字符串直接写入响应流,这在构造服务器控件之后发生。请参阅此帖子以获得解释。
So its either codebehind, or <%# + databinding as Zachary suggests.
所以它的代码隐藏,或Zachary建议的<%#+数据绑定。
#2
2
As a followup to my own question, I have discovered that what I really wanted was to use ASP.NET Expressions using the <%$ syntax, since what I wanted to do was put in localized content.
作为我自己的问题的后续,我发现我真正想要的是使用<%$语法使用ASP.NET表达式,因为我想要做的是放入本地化内容。
This can be done with apparently no extra handling on the server control side.
这可以通过服务器控制端显然没有额外处理来完成。
<cc1:customControl runat="server" Title='<%$ Resouces: ResourceFile, ContentKey %>' >
more content and controls
</cc1:customControl>
This works just fine.
这很好用。
#3
1
Try using databinding syntax: <%# PagePropertyValue %>
尝试使用数据绑定语法:<%#PagePropertyValue%>
#4
1
For the bind property value to work correctly as suggested, you will have this in the aspx or ascx file :
要使bind属性值按照建议正常工作,您将在aspx或ascx文件中使用此属性:
<cc1:customControl runat="server" Title='<%# PagePropertyValue %>' >
more content
</cc1:customControl>
You will then need to actually bind data in your page wich you have to add this in you code behind file (code in C#)
然后,您需要在页面中实际绑定数据,您必须在代码隐藏文件中添加此数据(C#中的代码)
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
That way it will bind the data in your ascx or aspx file.
这样它将绑定ascx或aspx文件中的数据。
#5
0
Note that this is specific to control attributes. When using the <%= syntax outside control attributes meaning anywhere else in the page the syntax works as expected. So this <%=GetCapitalUserName()%> would call the correct method and inject the result of the call in the page.
请注意,这特定于控制属性。当使用<%=语法外部控制属性意味着页面中的任何其他位置时,语法按预期工作。所以这个<%= GetCapitalUserName()%>会调用正确的方法并在页面中注入调用的结果。
#1
7
You cant. <%= %> will write the string directly to the response-stream, which happens after the server control is constructed. See this post for an explanation.
你不能。 <%=%>会将字符串直接写入响应流,这在构造服务器控件之后发生。请参阅此帖子以获得解释。
So its either codebehind, or <%# + databinding as Zachary suggests.
所以它的代码隐藏,或Zachary建议的<%#+数据绑定。
#2
2
As a followup to my own question, I have discovered that what I really wanted was to use ASP.NET Expressions using the <%$ syntax, since what I wanted to do was put in localized content.
作为我自己的问题的后续,我发现我真正想要的是使用<%$语法使用ASP.NET表达式,因为我想要做的是放入本地化内容。
This can be done with apparently no extra handling on the server control side.
这可以通过服务器控制端显然没有额外处理来完成。
<cc1:customControl runat="server" Title='<%$ Resouces: ResourceFile, ContentKey %>' >
more content and controls
</cc1:customControl>
This works just fine.
这很好用。
#3
1
Try using databinding syntax: <%# PagePropertyValue %>
尝试使用数据绑定语法:<%#PagePropertyValue%>
#4
1
For the bind property value to work correctly as suggested, you will have this in the aspx or ascx file :
要使bind属性值按照建议正常工作,您将在aspx或ascx文件中使用此属性:
<cc1:customControl runat="server" Title='<%# PagePropertyValue %>' >
more content
</cc1:customControl>
You will then need to actually bind data in your page wich you have to add this in you code behind file (code in C#)
然后,您需要在页面中实际绑定数据,您必须在代码隐藏文件中添加此数据(C#中的代码)
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
That way it will bind the data in your ascx or aspx file.
这样它将绑定ascx或aspx文件中的数据。
#5
0
Note that this is specific to control attributes. When using the <%= syntax outside control attributes meaning anywhere else in the page the syntax works as expected. So this <%=GetCapitalUserName()%> would call the correct method and inject the result of the call in the page.
请注意,这特定于控制属性。当使用<%=语法外部控制属性意味着页面中的任何其他位置时,语法按预期工作。所以这个<%= GetCapitalUserName()%>会调用正确的方法并在页面中注入调用的结果。