my aim is the following:
- Put some ImageButtons on an aspx page (with masterpage)
- Each ImageButton is actually a PlaceHolder, where I want to load an Image. I assume to have, at start, no ImageButton.ImageUrl.
- When clicking on an ImageButton, I want the ImageButton to display an image (the final goal is to load it from local machine, but, for now I use a fixed image)
My idea is to change the ImageButton.imageUrl after a ImageButtonClick.
The Aspx page has an Asp:Panel with ID=FOGLIO, and contains an ImageButton with ID=HEADERLOGO.
I tried to google all related topics, but my code, actually refuses to work. Some basic mistake may be, because none of the controls in the page is retrieved, as evidenced by a Response.Write that I have put during a PostBack. I don’t see anyway to do what I want. Does anybody have any hint?
Aspx markup and code behind follow.
<%@ Page Title="" Language="C#" MasterPageFile="Site.Master" AutoEventWireup="true"
CodeBehind="Images.aspx.cs" Inherits="Monitas.Images" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<style media="all">
.Sheet {position: relative; left: 15%; top:15%; background: white; width: 800px;
height: 900px; border:3px solid #000;}
.HeaderLogo {position: relative; background: yellow; width:240px; height:100px;
left: 500px; top: 1px; border:1px solid #000;}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:Panel ID="FOGLIO" class="Sheet" runat="server">
<asp:ImageButton class="HeaderLogo" ID="HEADERLOGO" alternateText="Header Logo"
runat="server" ImageAlign="Middle" OnClick="OnImageHeaderClick" />
</asp:Panel>
</asp:Content>
The code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using static Monitas.GlobalVars;
namespace Monitas
{
public partial class Images : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack) // in a PostBack, following the OnImageHeaderClick
{
Panel p = (Panel)FindControl("FOGLIO");
Response.Write("<<" + p + ">>"); // <<<< always null
if (p!=null)
{
ImageButton img = (ImageButton)FindControl("HEADERLOGO");
Response.Write("<<" + img + ">>");
if (img!=null)
img.ImageUrl=HeadImage;
}
}
}
protected void OnImageHeaderClick(object sender, ImageClickEventArgs e)
{
HeadImage = Server.MapPath("../Monitas/Images/FooterLogo.png"); // use another image
after clicking on the previous
}
}
}
2
Answers
Barring compiler errors, all controls with
runat="server"
and anID
attribute, and which aren’t nested within a data-bound control, will be available as fields in the code-behind.In this case, you should be able to reference
HEADERLOGO
directly from your code-behind, without having to resort toFindControl
.If all else fails, the
sender
parameter of theOnImageHeaderClick
event handler will also be a reference to the image button.It’s also worth pointing out that you change the
HeadImage
property after setting theImageUrl
property. The update will not be visible until you click on the image button a second time.And finally,
Server.MapPath
will return the physical path of the file on your server. That won’t work as an image URL: the user browsing your site almost certainly won’t have an image file in the same path on their computer; and even if they did, it’s almost certainly not the same image as the one on your server; and even if it was, a website cannot reference images stored on the local filesystem.Yes, it probably better to use a listview (my favorte), but say lets use a grid view.
We have this grid view with some columns, and a image button:
eg this:
Now our data is this:
And code to load is this:
And now we have this:
Now, I have jQuery, but we COULD add some code to click on a image, and dispay it say in a popup – much larger.
So on the image button, we have a click event. We stuff the given row click (url) in to a MUCH larger image control we have hidden on the page, and then call jQuery.UI "dialog’ to display that image in a dialog.
So, lets add the click event – and WE CAN do this 100% client side!!
So, we add a div to hold the image, and this code:
So, now I click on the image in the row, and we have this:
So, use a grid or listview – even a repeater. Place one image control, and repeat it for the many pictures you want to display.
Fyi: I assume both jquery and jquery.ui for the dialog pop up.
Edit: click on image button – show image in button
So for image button click event, then this:
protected void OnImageHeaderClick(object sender, ImageClickEventArgs e)
{