Codebehind access list from aspx page

My code code looks like

    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 cannot access the list of images inside the .aspx page ... Any suggestions?

Thanks M

+3
source share
5 answers

Your variable Imagesis a local variable for the method getImgCarouseland cannot be seen outside of it.

Promote it to an open property (or field) to access it outside 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);
}
+2
source

You must make the Imagesfield inside your class (local variables can only be seen inside the area in which they were declared):

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);
}
+2
source

Images. . , .

0

aspx , , - // .

, aspx , .

, (.. Page_Load), . , .

, Image , - (class Image), private class Image, , , .

, , , , aspx.

:

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

, Andrew Bezzub , Image.

// 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

Source: https://habr.com/ru/post/1771754/


All Articles