locked
image from sql inside div RRS feed

  • Question

  • User1203467145 posted

    please i want to put image from sql inside div in asp.net web page , i want loop while inside of it there is many divs .please someone tell me how cani i putthis image from sql into div in code behind please  

    Thursday, March 15, 2018 9:48 PM

All replies

  • User283571144 posted

    Hi ayma,

    please i want to put image from sql inside div in asp.net web page , i want loop while inside of it there is many divs .please someone tell me how cani i putthis image from sql into div in code behind please  

    Could you please tell me the image type searched from the sql server? Image link or Base64?

    Normally, we will firstly find the these div in code behind, then add the image html control in it.

    For example, we should firstly add runat="server" in the div , then we could find it in the code behind.

    Code as below:

    <div id="dvimage" runat="server">
     </div>

    Code behind:

        HtmlGenericControl sortImagedv = item.FindControl("dvimage") as HtmlGenericControl;
        HtmlImage image = new HtmlImage();
    //here you need fill the image url from sql image.Src = "Images/active.png"; image.ID = "imgupdated"; sortImagedv.Controls.Add(image);

    I suggest you could try to use gridview, repeater or some web control to help use generate the code.

    More details, you could refer to below code sample.

            <div>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <div>
                            <asp:Label ID="Label1" runat="server"><%#Eval("Product_Name")%></asp:Label>
                            <asp:Label ID="Label2" runat="server"><%#Eval("Quantity")%></asp:Label>
                            <asp:Label ID="Label3" runat="server"><%#Eval("Price")%></asp:Label>
                            <asp:Image ID="Image1" runat="server" ImageUrl="<%#Eval("path")%>" />
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>

    Code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindRepeater();
                }
            }
            protected void BindRepeater()
            {
    
                //Fill the data from database then bind with the reapter
                DataSet ds = new DataSet();
               var  dt = new DataTable();
                var pName = new DataColumn("Product_Name", Type.GetType("System.String"));
                var pQty = new DataColumn("Quantity", Type.GetType("System.Int32"));
                var pPrice = new DataColumn("Price", Type.GetType("System.Int32"));
                var pPath = new DataColumn("path", Type.GetType("System.String"));
                dt.Columns.Add(pName);
                dt.Columns.Add(pQty);
                dt.Columns.Add(pPrice);
                dt.Columns.Add(pPath);
               var  dr = dt.NewRow();
    
                dr["Product_Name"] = "Product 1";
                dr["Quantity"] = 2;
                dr["Price"] = 200;
                dr["path"] = "image/Produc1.jpeg";
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["Product_Name"] = "Product 2";
                dr["Quantity"] = 5;
                dr["Price"] = 480;
                dr["path"] = "image/Produc2.jpeg";
    
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["Product_Name"] = "Product 3";
                dr["Quantity"] = 8;
                dr["Price"] = 100;
                dr["path"] = "image/Produc3.jpeg";
                dt.Rows.Add(dr);
    
                dr = dt.NewRow();
                dr["Product_Name"] = "Product 4";
                dr["Quantity"] = 2;
                dr["Price"] = 500;
                dr["path"] = "image/Produc4.jpeg";
                dt.Rows.Add(dr);
    
                ds.Tables.Add(dt);
                Repeater1.DataSource = ds.Tables[0];
                Repeater1.DataBind();
            }

    Best Regards,

    Brando

    Friday, March 16, 2018 5:24 AM