Usercontrol .ascx变量访问

时间:2013-02-10 19:41:02

标签: c# asp.net user-controls compiler-errors

如何解决访问问题?

标记:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Header.ascx.cs" Inherits="Version_2.Themes.Anatema.Header" %>

<%= deneme %>

代码:

namespace Version_2.Themes.Anatema
{
    public partial class Header : UserControl
    {
        public string deneme = "asd";
    }
}

错误:

  

当前上下文中不存在名称'deneme'

1 个答案:

答案 0 :(得分:1)

试试这个,它应该有效。

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="user.ascx.cs" Inherits="user" %>
<asp:Label ID="Label1" runat="server">
<%=test%>
</asp:Label>

代码背后

using System;

public partial class user : System.Web.UI.UserControl{

public string test = "This is a test";
protected void Page_Load(object sender, EventArgs e){
    this.DataBind();

}
}