如何在动态加载时获取用户控制数据

时间:2011-03-30 15:11:16

标签: asp.net user-controls

我有一个具有一些属性的用户控件。

public partial class ImageUpload : System.Web.UI.UserControl
{
    public Image img = new Image();
    protected void Page_Load(object sender, EventArgs e)
    {
        img.PG_Caption = txtImgCaption.Text;
        img.PG_Visible = chkVisible.Checked;
        img.File = fileImgUpload.FileBytes;
        img.PG_FileName = fileImgUpload.FileName;
    }
}

在另一个用户控件上我加载了page_load的一些第一个用户控件。

public partial class ImagesTab : System.Web.UI.UserControl
{
    public int Count { get; set; }
    public List<Image> Images
    {
        get { return GetImages(); }
    }

    private List<Image> GetImages()
    {
        List<Image> lI = new List<Image>();
        foreach (Control ctl in plhImages.Controls)
        {

        }
        return lI;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < Count; i++)
            {
                Control ImageUpload = LoadControl("ImageUpload.ascx");
                plhImages.Controls.Add(ImageUpload);
                Literal li = new Literal();
                li.Text = "<br />";
                plhImages.Controls.Add(li);
            }
        }
    }

}

如何在GetImages()上实现foreach循环以获取数据?

1 个答案:

答案 0 :(得分:1)

private List<Image> GetImages()
{
    List<Image> lI = new List<Image>();
    foreach (Control ctl in plhImages.Controls)
    {
        if (ctl is ImageUpload) lI.Add(((ImageUpload)ctl).img);
    }
    return lI;
}