使用<%= tag%>

时间:2012-02-29 09:13:49

标签: asp.net

大家好我在使用<%=%>中的属性时遇到问题标签。 由于某种原因,编译器不会获取<%=%>内的属性。标签。 正在使用的IDE是MVWD 我得到的错误是

Error   2   'ASP.webusercontrol1_ascx' does not contain a definition for  
'UserName' and no extension method 'UserName' accepting a first argument of type 
'ASP.webusercontrol1_ascx' could be found (are you missing a using directive or an 
assembly reference?)    c:\Users\jonny\Documents\Visual Studio    2010\Projects\WebApplication8\WebApplication8\WebUserControl1.ascx    2   36  WebApplication8

我的WebUserControl1.ascx文件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"   
    Inherits="WebApplication8.WebUserControl1" %>
<b>Information about </b> <%= this.UserName%> 
<br /><br />

<%= this.UserName %> is <%= this.UserAge %> years old and lives in <%= this.UserCountry %>

这是我的Webusercontrol1.ascx.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication8
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{

    private string userName;
    private int userAge;
    private string userCountry;

    public string UserName
    {
        get { return userName; }
        set { userName = value; }
    }

    public int UserAge
    {
        get { return userAge; }
        set { userAge = value; }
    }

    public string UserCountry
    {
        get { return userCountry; }
        set { userCountry = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }


 }

2 个答案:

答案 0 :(得分:1)

我认为你可以将字段放入文字控件中,这确实是aspx看起来很难看。像这样的东西(我用于资源,但应该是相似的)

<asp:Literal ID="Literal6" runat="server" Text="<%$ Resources:General, lblFunctie %>"></asp:Literal>

答案 1 :(得分:0)

我认为你只需删除“这个”。前缀...