Accessing DataList in DataTable

Joined
Nov 20, 2009
Messages
1
Reaction score
0
Hi, I'm new to ASP, I'm pretty good with Java, but am only just looking at C# and asp due to a uni assignment.

I have a problem accessing data in a Datalist, which is held in a cell of a DataTable.

Code:
public class GalleryGrid : System.Web.UI.Page
{
    public int gridwidth = 4;
    public int gridheight = 3;
    
    ArrayList files = new ArrayList();

    public DataTable getTable()
    {

        DataTable dt = new DataTable();
        string colName;
        
        files = getFiles();
        
        for (int i = 0; i < gridwidth; i++)
        {
            colName = "gridImage"+i;
            //myNewColumn.ColumnName = colName;


            //DataColumn myNewColumn = new DataColumn();
            //dt.Columns.Add(myNewColumn);
            //dt.Columns[i].ColumnName = colName;
            dt.Columns.Add(colName, typeof(string));
        }
        if (files != null)
        {
            for (int j = 0; j < gridheight; j++)
            {
                
                DataRow myNewRow;

                myNewRow = dt.NewRow();


                for (int i = 0; i < gridwidth; i++)
                {

                    if ((i+(j*gridwidth)) < files.Count)
                    {
                        Thumbnail thumbnail = new Thumbnail();
                        
                        string path = files[(i + (j * gridwidth))].ToString();
                        string filepath = "~/Images/Images/"+path;

                        System.Drawing.Image image = thumbnail.GetThumbnail(path);
                        
                        int width = thumbnail.GetWidth();
                        int height = thumbnail.GetHeight();


                        ArrayList cell = new ArrayList();
                        cell.Insert(0, image);
                        cell.Insert(1, width);
                        cell.Insert(2, height);
                        cell.Insert(3, filepath);

                        DataList dataList = new DataList();
                        
                        dataList.DataSource = cell;
                        dataList.DataBind();
                        
                        myNewRow[i] = dataList;
                        Console.Write(files[(i + (j * gridwidth))]);
                    }
                    else
                    {
                        myNewRow[i] = "null";
                    }



                }
                dt.Rows.Add(myNewRow);

            }
        }
        return dt;
    }

    public string ResolveUrl(string originalUrl)
    {
        if (!string.IsNullOrEmpty(originalUrl) && '~' == originalUrl[0])
        {
            int index = originalUrl.IndexOf('?');
            string queryString = (-1 == index) ? null : originalUrl.Substring(index);
            if (-1 != index) originalUrl = originalUrl.Substring(0, index);
            originalUrl = VirtualPathUtility.ToAbsolute(originalUrl) + queryString;
        }

        return originalUrl;
    }

    public ArrayList getFiles()
    {
        
        int i = 1;
        foreach (string file in Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("Images") + "\\", "*.jpg"))
        {
            
            files.Add(Path.GetFileName(ResolveUrl(file)));//"ThumbnailCreator.aspx?ImageId=" + 
             i++;
        }
        return files;
    }

Code:
<asp:GridView ID="DataGridView1" runat="server" AutoGenerateColumns="false" 
            AutoGenerateEditButton="false" CellPadding="4" AllowPaging="true" 
            onselectedindexchanged="DataGridView1_SelectedIndexChanged" EnableViewState="false" OnRowCommand="DataGridView1_RowCommand">
        <Columns>
                
            <asp:TemplateField HeaderText="gridImage0" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    
                    <asp:ImageButton ID='Image0' runat='server' ImageURL='<%# DataBinder.Eval(Container.DataItem, "filepath")%>'  OnClick='lblClick1' CommandName="picture" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "gridImage0")%>'  ></asp:ImageButton>
                    
               </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="gridImage1" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:ImageButton ID='Image1' runat='server' ImageURL='<%# "Thumbnail.aspx?ImageId=" + DataBinder.Eval(Container.DataItem, "gridImage1")%>' OnClick='lblClick1' CommandName="picture" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "gridImage1")%>'  ></asp:ImageButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="gridImage2" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                   <asp:ImageButton ID='Image2' runat='server' ImageURL='<%# "Thumbnail.aspx?ImageId=" + DataBinder.Eval(Container.DataItem, "gridImage2")%>' OnClick='lblClick1' CommandName="picture" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "gridImage2")%>'  ></asp:ImageButton>
               </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="gridImage3" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
                <ItemTemplate>
                    <asp:ImageButton ID='Image3' runat='server' ImageURL='<%# "Thumbnail.aspx?ImageId=" + DataBinder.Eval(Container.DataItem, "gridImage3")%>' OnClick='lblClick1' CommandName="picture" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "gridImage3")%>'  ></asp:ImageButton>
               </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        
        </asp:GridView>

It's a bit of a mess, but I want to access the contents of the ArrayList cell, bound to DataList dataList, specifically the filepath object, in the HTML.

Any ideas anybody? All help greatly appreciated :)
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top