• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

Using ImageButtons in a Datalist as thumbnails, how do I get the url of the image to pass to an open window function?

Hello, I am using a Datalist to display images from a database.  I am using ImageButton's to do this.  What I want is when the dynamically created images are displayed the user can click them to open a window with the full size version of the pic.  Does this make sense?  

I really want to handle this from the code-behind because i like it that way, but whatever works.  Can someone please provide some sample code to facilitate this?  Everything I find on the net so far has been, well hard to understand.  I thought this would be a simple thing when I started... actually I hope it still is.  

Sorry if I am not asking well, but that is how lost I am at this point!  
0
xfungalx
Asked:
xfungalx
  • 2
1 Solution
 
madhevan_pillaiCommented:
Hi,

That is possible

Please go thru the sample code. Instead of the grid and Image button i simply used the Image button.

Image from data base

Note: I had hard coded the image Id. change the code according to ur need.


aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="GetImage.aspx.vb" Inherits="Image_GetImaget" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>


codebehind

Imports System.Data.SqlClient
Imports System.io
Imports System.Drawing
Imports System.Drawing.Imaging
Partial Class Image_GetImaget
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim stream As MemoryStream = New MemoryStream()

        Dim connection As SqlConnection
        connection = New SqlConnection("Data Source=USTSDC1\RAINMAKERDEVELOP;Initial Catalog=Test;User ID=sa;pwd=sa")
        connection.Open()

        Dim cmd As SqlCommand = New SqlCommand("select [Image] from [Image] where [Id]=" + Me.Request.QueryString("Id"), connection)
        Dim image() As Byte = CType(cmd.ExecuteScalar(), Byte())
        stream.Write(image, 0, image.Length)
        Dim bitmap As Bitmap = New Bitmap(stream)
        Response.ContentType = "image/gif"
        bitmap.Save(Response.OutputStream, ImageFormat.Gif)
        connection.Close()

    End Sub
End Class


thumbnail

aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ShowImage.aspx.vb" Inherits="Image_ShowImage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language=javascript type="text/javascript">
            function ShowImage(Id)
           {
                       window.open("getImage.aspx?Id=" +1,"_blank") ;
           }
     </script>  
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ImageButton ID="ImageButton1" runat="server" Height="104px" Width="112px"    /></div>
    </form>
</body>
</html>


code behind
Imports System.Data.SqlClient
Imports System.io
Partial Class Image_ShowImage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.ImageButton1.Attributes.Add("Onclick", "ShowImage(1)")
            Me.ImageButton1.ImageUrl = "getImage.aspx?Id=1"
        End If
    End Sub
End Class


Zoom

aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="zoom.aspx.vb" Inherits="Image_zoom" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Image ID="Image1" runat="server" /></div>
    </form>
</body>
</html>

codebehind


Partial Class Image_zoom
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Image1.ImageUrl = "getImage.aspx?Id=1"
    End Sub
End Class







0
 
madhevan_pillaiCommented:
Hi,

I had given you three sample pages go thru the aspx page and codebehind
0
 
xfungalxAuthor Commented:
Thanks for that code, I am sure it works, and I will use it if necessary.  But I still thought there would be an easier way.  I suppose this code will resize the pics to thumbnail size to save bandwidth on page load however I was hoping for an alternative using my datalist.  Is there an easier way?  Like I said, I will try this sample code tonight and ultimately use it if necessary, so thank you for this.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now