从我的网站中的 aspx.cs 类访问变量! (ASP.NET Web 表单)

发布于 2024-12-28 06:18:12 字数 598 浏览 1 评论 0原文

我的计划是从数据库访问航点并在 bing 地图中将它们显示为折线。 问题如下:我有我的default.aspx.cs类:

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);
    }
}  

现在我想访问我的default.aspx页面中的waypointList。 我想做类似的事情:

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

但我无法像这样访问 waypointList,可以吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

微凉 2025-01-04 06:18:12

如果将其设置为受保护,则可以访问它,因为 aspx 标记会生成一个继承您的代码的类。如果合适的话,还可以考虑使用一些数据绑定控件。

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.

倚栏听风 2025-01-04 06:18:12

将您的字段范围更改为受保护。例如

protected List<Waypoints> waypointList;

Change the scope of your field to protected. e.g.

protected List<Waypoints> waypointList;
心房敞 2025-01-04 06:18:12

尝试将您的变量设为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);
    }
}  

编辑:您也可以将它们设置为protected来访问它们,您可以参考乔治对我的答案的评论,以了解为什么应该使用protected而不是公共

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文