简体   繁体   中英

Accessing variables from the aspx.cs class in my website! (ASP.NET Web Forms)

My plan is to access waypoints from a database and show them in bing maps as a polyline. The problem is the following: I have my default.aspx.cs class:

public partial class _Default : System.Web.UI.Page
{
    List<Waypoints> waypointList;
    IWaypointLogic waypointLogic;

    protected void Page_Load(object sender, EventArgs e)
    {
        waypointLogic = new WaypointLogic();
        waypointList = waypointLogic.GetWaypointsByTrackId(1);
    }
}  

And now I want to access the waypointList in my default.aspx page. I want to do something like:

<% foreach (GPS_Tracker.Entities.Waypoints waypoint in waypointList %>

But I can't access the waypointList like this, can I?

You can access it if you make it protected since the aspx markup generates a class that inherits your code behind. Also consider using some databound control if appropriate.

Change the scope of your field to protected. eg

protected List<Waypoints> waypointList;

try making your variables public .

public partial class _Default : System.Web.UI.Page
{
    public List<Waypoints> waypointList;//or protected
    public IWaypointLogic waypointLogic;//or protected

    protected void Page_Load(object sender, EventArgs e)
    {
        waypointLogic = new WaypointLogic();
        waypointList = waypointLogic.GetWaypointsByTrackId(1);
    }
}  

EDIT :Also you can make them protected to access them, you can refer to George's comment on my answer for an explanation why you should use protected instead of public .

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