返回值字符串列表 [英] Return value list of string

查看:117
本文介绍了返回值字符串列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取整个页面上可见的ImageDescr列表的结果。

e。 g。:按下按钮并读取ImageDescr列表的索引(n)以显示值。



我尝试过:



I'm trying to get the result of the ImageDescr List, visible on the whole page.
e. g.: I press a Button and read the index (n) of the ImageDescr List to display the value.

What I have tried:

<pre>protected void Page_Load(object sender, EventArgs e)
 {
    ....
    ImageDescription ImageDescr = new ImageDescription();
   
    //ImageDescr is declared public in the class ImageDescription
    //after execute not see the element ImageDescr = null
    //
 }





完整列表类,从目录读取字符串





Class for full List , read string from Directory

public class ImageDescription
    {
        private string virtualPath;
        private string physicalPath;

        public List<string> ImageDescr { get; set; }

        public ImageDescription()
        {
            // from other class
            PathImages pImg = new PathImages();
            virtualPath = pImg.path1;
            physicalPath = pImg.path2;

            List<string> ImageDescr = new List<string>();

            try
            {
                var imagesFolder = new DirectoryInfo(physicalPath);
                foreach (var item in imagesFolder.EnumerateFiles())
                {
                    if (item is FileInfo)
                    {
                        //add virtual path of the image to the images list
                        ImageDescr.Add(string.Format("{0}/{1}", virtualPath, item.Name));
                    }
                }

                //the List ImageDescr is is correctly filled
            }
            catch (Exception ex)
            {
                //log exception
                //Logger.Log(ex.Message);
            }

        }        
    } 

推荐答案

嗯。您确实意识到您填充的版本会掩盖类级别版本:

Um. You do realize that the version you populated masks the class level version:
public List<string> ImageDescr { get; set; }

        public ImageDescription()
        {
...
            List<string> ImageDescr = new List<string>();

            try
            {
...
                        ImageDescr.Add(string.Format("{0}/{1}", virtualPath, item.Name));
...
            }
         }

这意味着你填写的数据在构造函数存在时被丢弃,留下 public ImageDescr的版本未分配且为null?

我建议您先遵循C#命名约定,或者指定值而不是使用新变量:

And what that means is that the data you filled in is discarded when the constructor exists, leaving the public version of ImageDescr unassigned and null?
I'd suggest that you start by following C# naming conventions, or assign the value instead of using a new variable:

        public List<string> ImageDescr { get; set; }

        public ImageDescription()
        {
...
            ImageDescr = new List<string>();

            try
            {


public partial class _Default : System.Web.UI.Page
{
    .........
    public List<string> ImgDes = new List<string>();

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
          ............
          GetNameImage();
        }
    }


 protected void GetNameImage()
    {
        PathDesc = MapPath(virtualPath + "/" + "DescrImages.txt");

        if (File.Exists(PathDesc))
        {
            using (var sr = new StreamReader(PathDesc))
            {
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    NameImage.ImgName.Add(line);
                }

                sr.Close();
            }

        ImgDes = NameImage.ImgName.ToList();
    }

public static class NameImage
    {
        public static List<string> ImgName = new List<string>();

    }
}


我的解决方案并不完美:
带有Scriptmanager + UpdatePanel的


当我点击DataList的图像在同一页面上执行其他操作时,List ImgDes 中存储的数据将丢失



没有UpdatePanel ImgDes 仍可在页面的每个部分使用。
my solution is not perfect:
with Scriptmanager + UpdatePanel
the data stored in the List ImgDes is lost when i click on an image of the DataList to perform other operations on the same page

without UpdatePanel ImgDes remaining usable in every part of the page.


这篇关于返回值字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆