Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I want to make a photo gallery. But I have a problem to show the image in the list view. and images are stored in the database(SQL Server 2005).
I want to show image in a table format and if I click on any image, this will open into a new window with a big size. I am using a listView But it didn't work properly. Can I use another control to show image?
Posted
Updated 8-Jul-20 1:58am
v3

Hi,
You can even use GridView Control to achieve the same as it has lot of features.

First, Place the GridView in your main page as follows:
(Replace <Put-Your-DataSet-ImageURL-FieldName> by your actual field name)

<body>
    <form id="frmGallery" runat="server">
        <table id="Table6" align="center" cellpadding="0" cellspacing="0" style="border: 0px Solid Gray;
            width: 100%; font-family: tahoma; font-size: 10pt; background-color: white;">
            <tr>
                <td style="width: 100%; height: 200px;" align="left" valign="top">
                    <asp:GridView ID="gvImageGallery" runat="server"
                        PageSize="50" AutoGenerateColumns="false" Width="100%"
                        onrowcommand="gvImageGallery_RowCommand">
                        <Columns>
                            <asp:TemplateField HeaderText="Image">
                                <ItemStyle Width="100%" />
                                <ItemTemplate>
                                    <asp:ImageButton ID="imgBtnShow" runat="server" CommandName="ShowImage" CommandArgument="<%# Container.DataItemIndex %>"
                                        ImageUrl="<Put-Your-DataSet-ImageURL-FieldName>" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </form>
</body>





Bind your GridView with your DataSet and add the Event " gvImageGallery_RowCommand "

In this event, first find the selected row-index, then find the ImageButton control and finally get the ImageURL of this Button.

Add ClientScript to open the Popup window to show the Image(You need to create the page before that)
(find sample below)

C#
private void BindGridView()
{    
    DataSet ds = "your-dataset-retrieval-stuff-here";

    gvImageGallery.DataSource = ds;
    gvImageGallery.DataBind();
}

protected void gvImageGallery_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ShowImage")
    {
	// Get Selected Row Index through CommandArgument
	int index = Convert.ToInt32(e.CommandArgument);

	// Find ImageButton in GridView Row
	ImageButton imgBtn = (ImageButton)gvImageGallery.Rows[index].Cells[0].FindControl("imgBtnShow");

	// Get Selected Image's URL
	string imageURL = imgBtn.ImageUrl;

	// Open Popup window
	ClientScript.RegisterStartupScript(this.GetType(), "show", "<script language="javascript">window.open('Pop.aspx?URL=" + imageURL + "','ShowImage','height=300px,width=600px,scrollbars=1,center=yes');</script>");
    }
}



In your pop-up window Create a Image and Set ImageURL from QueryString value(sample mentioned below)

<body>
    <form id="frmPopup" runat="server">
     <asp:Image ID="imgShow" runat="server" />
    </form>
</body>



C#
public partial class Pop : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
	    if (!IsPostBack)
	    {
		if (Request.QueryString["URL"] != null)
		{
		    imgShow.ImageUrl = Request.QueryString["URL"];
		}
	    }
	}
}



Finally make the styles of GridView, ImageButton and Popup window height and width as per your requirements
 
Share this answer
 
v4
Comments
Sandeep Mewara 7-Apr-11 10:36am    
My 5!
Tarun.K.S 7-Apr-11 10:47am    
Good answer! have a 5!
hi,

Take Image Control in listview Itemtemplate
XML
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/Admin/Images/cross.gif"  />
br mode="hold" />
also set width and height of Image as your Requirement

and OnClientClick Write your function to pop a window and display Full Image
 
Share this answer
 
v2
this link help you to solve your issue
Image Handling In ASP.NET[^]
 
Share this answer
 
Comments
Mydsh 9-Aug-12 1:43am    
I want to bind images horizontally in listview how to do it using c# asp.net
raju melveetilpurayil 9-Aug-12 4:55am    
check this link, if you want to do "ListView repeat items horizontally"
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/68191cbc-9ee1-43a4-9d6f-147efc06662b

Thanks
Mydsh 9-Aug-12 5:06am    
Thank You
raju melveetilpurayil 9-Aug-12 5:49am    
Always welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900