开发者

Access list in codebehind from aspx page

My code-behind looks like this

    class Image
    {
        public string tnImg { get; set; }
        public string Name { get; set; }
        public string city { get; set; }
        public string refPlace { get; set; }
        public string refInfo { get; set; }
        public string refInfoDynamic { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        getImgCarousel();
    }

    public void getImgCarousel()
    {
        开发者_运维技巧List<Image> Images = new List<Image>();

        var carouselImages = new Image();
        carouselImages.Name = "test";

        var carouselImages2 = new Image();
        carouselImages2.Name = "test2";

        Images.Add(carouselImages);
        Images.Add(carouselImages2);
    }

Then I would like to do the following in .aspx

<div class="wrapCarousel">  
 <div>My Images</div>
   <% foreach(var image in Images) { %>
      <div><%=image.Name%></div> <!-- format your markup here -->
   <% } %>

</div>

But it seems like I can't access the list Images inside the .aspx page... Any suggestions?

Thanks M


Your Images variable is a local variable to the getImgCarousel method and can't be seen outside of it.

Promote it to a public property (or field) in order to access it outside of the method:

// readonly property - will throw null reference if not initialized
public IList<Image> Images { get;}

public void getImgCarousel()
{
    this.Images = new List<Image>();

    var carouselImages = new Image();
    carouselImages.Name = "test";

    var carouselImages2 = new Image();
    carouselImages2.Name = "test2";

    Images.Add(carouselImages);
    Images.Add(carouselImages2);
}


You should make Images a field inside your class (local variables can be seen only inside of the scope they were declared in):

List<Image> images = new List<Image>();

protected List<Image> Images
{
    get { return this.images; }
}

public void getImgCarousel()
{
    var carouselImages = new Image();
    carouselImages.Name = "test";

    var carouselImages2 = new Image();
    carouselImages2.Name = "test2";

    Images.Add(carouselImages);
    Images.Add(carouselImages2);
}


Look at the accessibility of the Images field. It is a private temporary variable in your method. Make it a public field in your class and you will be able to access it.


At runtime, the aspx page is rendered by creating a new class that inherits from the code-behind class, so because of accessibility rules, any private classes / fields / methods are inaccessible.

You would have to declare anything that should be used by the aspx code as protected, because that enables access for any inherited classes.

In your code, Images is local to the method, so it could not be accessed even by other methods in the same class (i.e. Page_Load), much less by the generated subclass. As most of the other answers pointed out, you'll need to remove it from the method, and move it to a property.

But that would mean that you are exposing Image objects to your subclass, which are defined in your base class without any modifier (class Image) which the compiler treats as private class Image which means that no other class has access to the Image class in any way, shape or form.

So how, to remedy that, you would need to declare the nested class as protected, so it will be accessible to your subclass aspx.

The corrections I made are emphasized:

//accessible to the code-front
protected List<Image> Images {get; private set; }

//nested class also accessible to the code-front
protected class Image
{
    public string tnImg { get; set; }
    public string Name { get; set; }
    public string city { get; set; }
    public string refPlace { get; set; }
    public string refInfo { get; set; }
    public string refInfoDynamic { get; set; }
}

//exactly the reason why by default Page_Load is protected
protected void Page_Load(object sender, EventArgs e)
{
    getImgCarousel();
}

//this should be private as will not be called by anything outside the class
private void getImgCarousel()
{
    //uses the property
    Images = new List<Image>();

    var carouselImages = new Image();
    carouselImages.Name = "test";

    var carouselImages2 = new Image();
    carouselImages2.Name = "test2";

    Images.Add(carouselImages);
    Images.Add(carouselImages2);
}


You're close and Andrew Bezzub's answer helps but you also need to make the Image class accessible.

// Make the list protected
protected List<Image> Images;

// Make the class protected
protected class Image
{
    ...
}

protected void Page_Load( object sender, EventArgs e )
{
    ...
}

public void getImgCarousel()
{
    // Assign to the protected field.
    Images = new List<Image>();

    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜