Web - Multiple ImageButtons calling the came function

  • Thread starter Thread starter David P. Donahue
  • Start date Start date
D

David P. Donahue

This is a 2-part question:


1) I have a web form with multiple ImageButtons on it. I'd like them
all to do the same thing. Basically, in english-code, the function
would be as follows:
Set Session variable X to the ImageURL of the button that invoked me;
Forward the user to a specific URL;
That code is the easy part, of course. I just need to know how to tell
the ImageButtons to call that function when they're clicked. Preferably
within the HTML-view instead of the code-behind in Visual Studio.


2) Is there a way to have a control on a page (like a Repeater or
DataList) where I can give it a directory and a file mask of some kind
and it will make a list of ImageButtons, where each one's ImageURL is a
matching file in the directory, and set them all to use a specific
function as in part 1 above? I'm just looking for a way to speed up
development of something that's going to involve many, many very similar
pages.


Any help would be greatly appreciated. Thank you.


Regards,
David P. Donahue
(e-mail address removed)
 
1) Why use a WebServer Control such as the ImageButton if you want to
handle the event on the client side?

Use an HTML Image set as a Server Control and in your Page_Load method or in
a method called from Page_Load, do something like this:

myImage.Attributes.Add("onclick", "theNameOfMyClientSideJavaScriptMethod(\""
+ myVariablePathName
+ \"")";

2) To read the file system into a string array and DataBind your DataList
to it:

// for demonstration purposes, use the path that this web app is running
from

// but you may want to use a different path. If so, just make sure that the

// account that your web app is running under has access to read the
directory.

string myPath = Request.PhysicalApplicationPath;

string[] strArray =

System.IO.Directory.GetFiles(myPath,"*.gif");

DataList1.DataSource = strArray;

DataList1.DataBind();



Then, in the ASPX file, to display the image associated with the path given:

<ASP:DATALIST ID="DataList1" RUNAT="server">
<ITEMTEMPLATE>
<img src="<%# Container.DataItem %>">;
</ITEMTEMPLATE>
</ASP:DATALIST>



HTH



DalePres

MCAD, MCDBA, MCSE
 
1) Why use a WebServer Control such as the ImageButton if you want to
handle the event on the client side?

Mostly because I have some additional security that must be put in place
on the server side, and having the server-side and client-side code
intermingling may work, but wouldn't give me the confidence that I need
in the security implementation. In this particular case, the security
needs outweigh the minor performance hit. Not to mention that the only
way I know how to pass values from page to page on the client side is
through a URL query string, which is just ugly :)

Your code for part 2 of my question looks great, that will definitely be
the way to go in this case. The only thing now is putting something in
the DataList's ItemTemplate that tells the image to run a server-side
function when the image is clicked (html image or asp image button,
whatever's easier to point to a server-side function). And, from within
that function, be able to access the properties of the specific image
(its SRC or its ImageURL) that was clicked.


Regards,
David P. Donahue
(e-mail address removed)
 
Create your event handler as follows. Make sure you set the accessibility
to protected as shown.

protected void ImageButtonClick(object sender,
System.Web.UI.ImageClickEventArgs e)

{

Response.Write("the image was clicked");

}

Then, in the aspx file, add this attribute within your <ASP:IMAGEBUTTON >
tag:

ONCLICK="ImageButtonClick"

HTH

DalePres
MCAD, MCDBA, MCSE\
 
Back
Top