Pages

Men

rh

11/12/2012

How to bind the Images to Data List in C#

1)First i have stored some images in Application Folder. Please find the below Image.




2) Please find the Data base table data which i have used.




































3)ASPX Code:



<table>

<tr>

  <td>
    <asp:DataList ID="dlMenu" TabIndex=1 runat="server" RepeatDirection="Horizontal" 
         CellSpacing="3" Height="154px" Width="908px">
      <ItemTemplate>

         <asp:Label ID="ReportName" runat="server" Text='<%# Eval("ImageName") %>' />

           <br />
            <a href='<%# Eval("ImagePath") %>' ">
            <img src='<%# Eval("ImagePath") %>'  style="height: 100px; width: 100px;border: 1px solid gray;" />
            </a>

       </ItemTemplate>

     </asp:DataList>
   </td>
 </tr>
</table>
    
<table border="0" style="width: 928px">
  <tr>
    <td align="center" >

      <asp:ImageButton ID="imgfirst" runat="server" ToolTip = "First Button"           ImageUrl="~/Images/first.gif" onclick="imgFirst_Click" />

                &nbsp;&nbsp;&nbsp;&nbsp;

     <asp:ImageButton ID="ImgPrevious" runat="server" ToolTip = "Previous Button" ImageUrl ="~/Images/prev.gif" onclick="ImgPrevious_Click" />

                &nbsp;&nbsp;&nbsp;&nbsp;

     <asp:ImageButton ID="imgNext" runat="server" ToolTip = "Next Button" ImageUrl="~/Images/next.gif" onclick="imgNext_Click" />

                &nbsp;&nbsp;&nbsp;&nbsp;

     <asp:ImageButton ID="imgLast" runat="server" ToolTip = "Last Button" ImageUrl="~/Images/last.gif" onclick="ImgLast_Click" />

                &nbsp;&nbsp;&nbsp;&nbsp;
   </td>
  </tr>
</table>




.4) Aspx.cs File Code

      Int32 IntCurrentPageIndex = 0;

        PagedDataSource pgd = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IntCurrentPageIndex = 0;
                this.ViewState["Data"] = 0;
                IntCurrentPageIndex = (int)this.ViewState["Data"];
                BindImagesData();
            }
        }

      
       
        protected void BindImagesData()
        {
           
            DataSet dsImage = new DataSet();
            SqlConnection con = new SqlConnection("Data Source=CHANDU-PC\\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");
            con.Open();                            
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * FROM tbl_ImagesData";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
          
            da.Fill(dsImage);
            try
            {

                if (dsImage.Tables.Count > 0)
                {
                    if (dsImage.Tables[0].Rows.Count > 0)
                    {
                        pgd.DataSource = dsImage.Tables[0].DefaultView;
                        pgd.CurrentPageIndex = IntCurrentPageIndex;
                        pgd.AllowPaging = true;
                        pgd.PageSize = 7;
                        pgd.CurrentPageIndex = IntCurrentPageIndex;
                        dlMenu.DataSource = pgd;
                        dlMenu.DataKeyField = "ImageID";
                        dlMenu.DataBind();
                        con.Close();

                    }
                }


            }
            catch (Exception ex)
            {

            }
        }



        protected void ImgLast_Click(object sender, ImageClickEventArgs e)
        {
            IntCurrentPageIndex = pgd.PageCount - 1;
            BindImagesData();
        }

        protected void imgFirst_Click(object sender, ImageClickEventArgs e)
        {
            IntCurrentPageIndex = 0;
            BindImagesData();
        }

        protected void ImgPrevious_Click(object sender, ImageClickEventArgs e)
        {
            IntCurrentPageIndex = (int)this.ViewState["Data"];
            IntCurrentPageIndex--;
            this.ViewState["Data"] = IntCurrentPageIndex;
            BindImagesData();
        }

        protected void imgNext_Click(object sender, ImageClickEventArgs e)
        {
            IntCurrentPageIndex = (int)this.ViewState["Data"];
            IntCurrentPageIndex++;
            this.ViewState["Data"] = IntCurrentPageIndex;
            BindImagesData();


        }