简体   繁体   中英

can't find variable in <%=%>

I added a new web form in asp.net application which named Home. In Home.aspx.cs I used these following codes :

public partial class Home : System.Web.UI.Page
{
    protected string str = "Hello and welcome to c#";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Then I go to Home.aspx but unfortunately It won't find str variable.

These are the codes in Home.aspx :

    <body>
    <form id="form1" runat="server">
    <div>
    <%=str %>
    </div>
    </form>

It seems it's a problem with asp.net error. Because it shows me error but when I start without debugging it show me the string and won't show any error after that.

By the way there's no problem with protected.

your str variable should be public not protected , use like:

public partial class Home : System.Web.UI.Page
{
    public string str = "Hello and welcome to c#";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM