Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings,

I've been stuck on a problem for a few days now and I can't find a solution.

my task: I am creating a simple photo viewer for a website for a band.
I'm creating the website using ASP and C# as codebehind.
I load the photo's into dynamically created ImageButton's, but the Clickevent wont fire...

Tried loading these in Page_load, OnInit and a few other options I found searching the net. Nothing wil work.

So please, what am I doing wrong??

C# source:
C#
public partial class Gallery : System.Web.UI.Page
    {        
        string webpath = "~/Images";
                   
        protected override void OnInit(EventArgs e)
        {
            LoadButtons();      
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        
        protected void LoadButtons()
        {
            string[] filelist=Directory.GetFiles(Server.MapPath(webpath));
            int count=0;
            while (count < filelist.Length)
            {
                ImageButton ImgBtn = new ImageButton();
                ImgBtn.ID= filelist[count];
                ImgBtn.ImageUrl=webpath+filelist[count].Replace(Server.MapPath(webpath),"");
                ImgBtn.CssClass="ImgBtn";
                ImgBtn.Click+=new ImageClickEventHandler(ImgBtn_Click);
                GalleryOverview.Controls.Add(ImgBtn);
                GalleryOverview.Controls.Add(new LiteralControl("<br />"));
                count+=1;
            }
        }

       protected void ImgBtn_Click(object sender, EventArgs e)
        {
            ImageButton ImgBtn = (ImageButton)sender;
            Image Img = new Image();
            Img.ImageUrl = ImgBtn.ImageUrl;
            ShowPhoto.Controls.Add(Img);
            PhotoViewUpd.Update();
        }
    }


.aspx source:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Gallery.aspx.cs" Inherits="PatAPorcupine.Gallery" %>

<!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></title>
    <link href="npap.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div class="MainDiv">
      <div class="Title">Never Pat a Porcupine
      </div>
      <div runat="server" id="GalleryOverview" class="Gallery"></div>
      <asp:UpdatePanel ID="PhotoViewUpd" runat="server" UpdateMode="Conditional">
      <ContentTemplate>
        <div runat="server" id="ShowPhoto" class="ViewPhoto"></div>
      </ContentTemplate>
      </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>


C#

Posted

1 solution

You are using wrong event handlert in calling method i think u should use ImageClickEventArgs
protected void ImgBtn_Click(object sender, ImageClickEventArgs e)
       {
           ImageButton ImgBtn = (ImageButton)sender;
           Image Img = new Image();
           Img.ImageUrl = ImgBtn.ImageUrl;
           ShowPhoto.Controls.Add(Img);
           PhotoViewUpd.Update();
       }
 
Share this answer
 
v2
Comments
BBBwex 17-May-13 10:37am    
No, I also tried regular buttons to see if they would fire..... before copy & paste my code to here I changed it back to my original idea using ImageButtons but forgot to change that back to ImageClickEventArgs. Just to be safe I just tried it and still nothing happens

ImgBtn_Click just does not get called when I click the ImageButton, it only reloads the page (PostBack)
Pallavi Waikar 17-May-13 10:42am    
if postback is ocured then ImgBtn_Click is getting called..hence problem in code u have written in ImgBtn_Click ..check it
BBBwex 17-May-13 11:22am    
When running this in VS2010 I've set the breakpoint to the first line of ImgBtn_Click, and I've stepped trough the whole code from start to end... I never get into the code in ImgBtn_Click. I don't know whats wrong here.....
Pallavi Waikar 17-May-13 11:59am    
ok.then add one temporary image control on page assign that url to image control..if image is displayed on image control then problem is here ShowPhoto.Controls.Add(Img);

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