473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Web - Multiple ImageButtons calling the came function

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
dd******@ccs.neu.edu
Nov 16 '05 #1
3 1335
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

"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:Oj**************@TK2MSFTNGP10.phx.gbl...
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
dd******@ccs.neu.edu

Nov 16 '05 #2
> 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
dd******@ccs.neu.edu
Nov 16 '05 #3
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\
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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
dd******@ccs.neu.edu

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
2
by: Martin | last post by:
Hi, I want to use Imagebuttons () to submit a form, as following: <form ....> <Input name="buttonEdit" type=Image onclick="ActionHandle("2")> <Input name="buttonnew" type=Image ...
9
by: Richard Hunt | last post by:
What is the best way to get two values back from a function? I am working through 'The C Programming Language', but I felt like taking a bit of time off to write another program. The program...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
5
by: Lau Lei Cheong | last post by:
Hello, Let's say that I have multiple submit buttons on a form (imagebuttons actually, but documentations say that <input type=image> which a called image buttons should behave like submit...
2
by: charliewest | last post by:
I am dynamically creating my datagrid, building each column in real-time via code-behind (using c#). The only way i have read to add ImageButtons to my grid dynamically is by creating a separate...
4
by: iporter | last post by:
I wonder if anyone can clear up an OOP issue for me, specifically, how to have multiple child objects of a parent object. Consider the code below: var parentObj={ childCount: 0, childObj: {...
0
by: grbCPPUsr | last post by:
I am new to Python. I would like to use Python for the specialized purpose of dynamic expressions parsing & evaluation in my C++ application. I would like to encapsulate the expressions to be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.