aspx页面中的单个值绑定
我需要用数据库查询填充数十个属性的aspx页面。我知道怎么做是对财产的控制的Text属性后面的代码分配是这样的:aspx页面中的单个值绑定
protected void Page_Load(object sender, EventArgs e)
{
string param = Request.QueryString["param"];
// p will have dozens of properties
M.P p = new M.P(param);
aLabel.Text = p.aProperty;
anotherLabel.Text = p.anotherProperty;
而在ASPX代码:
<asp:Label ID="aLabel" runat="server"></asp:Label>
<asp:Label ID="anotherLabel" runat="server"></asp:Label>
我想做的是只是直接在aspx页面绑定属性,而不需要分配在后面的代码是这样的:
protected void Page_Load(object sender, EventArgs e)
{
string param = Request.QueryString["param"];
M.P p = new M.P(param);
this.DataBind();
Value of the aProperty: <%# p.aProperty %>
Value of the anotherProperty: <%# p.anotherProperty #>
但我失去了一些东西,因为编译器重要的给我的错误The name 'p' does not exist in the current context
。如何使它工作?
你可能有这样的事情(p
移动财产)
C#
protected M.P p {get; set;}
protected void Page_Load(object sender, EventArgs e)
{
string param = Request.QueryString["param"];
p = new M.P(param);
}
ASPX
<asp:Label ID="aLabel" runat="server"><%= p.aProperty %></asp:Label>
<asp:Label ID="anotherLabel" runat="server"><%= p.anotherProperty %></asp:Label>
似乎'DataBind()'是强制性的,所以我将它添加到你的答案中。 – 2012-07-06 17:04:51
@Clodoaldo:DataBind不需要使我的代码工作。如果有什么不及格让我知道,我们可以整理出来。 – 2012-07-06 17:13:37
是的,但只有'DataBind()' – 2012-07-06 17:32:05
注:请不要使用'ASP:Label'除非他们真的是标签。请参阅http://haacked.com/archive/2007/02/15/asp.net_tip_-_use_the_label_control_correctly.aspx – 2012-07-06 16:54:14
@mo。事实上,我在接受的答案的帮助下取消了这些标签。 – 2012-07-06 17:35:45