Solved

How do I correctly send send Event Argss with ans ASP OnClick Event

Posted on 2008-10-11
4
415 Views
Last Modified: 2013-11-26
I am trying to send an image url to an OnClick method but am unsure of the syntax required.
Basically, I have this ImageButton (ASP2.0)

<asp:ImageButton ID="btnUpdate" runat="server"  ImageUrl ="~/images/mikey.jpg" OnClick = "btnUpdate_Click" CommandName = "passUrl" CommandArgument = "ImageButton2.ImageUrl.ToString()"/>

I would like the url in Command Argument to be passed to the following method

protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ImageButton1.ImageUrl = e.ToString();
    }

Whereby ImageButton1 would then display the same image as the ImageButton that was clicked.

Am I even close??
0
Comment
Question by:finnatic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Expert Comment

by:raja_ind82
ID: 22696555
Hi, Please check the following code...

    protected void btnUpdate_Click(object sender, ImageClickEventArgs e)
    {

        ImageButton btn = (ImageButton)sender;
        string a = btn.CommandName ;      
       
    }

Regards,
M.Raja
0
 
LVL 15

Accepted Solution

by:
NazoUK earned 500 total points
ID: 22696587
I think you're confusing 2 types of event here: the command event and the click event.

The click event is a simple event that reacts to the click and doesn't allow you to use the CommandName and CommandArgument Attributes.
You add it to your button using OnClick="clickHandler"

The function to handle this event has the signature:
protected void clickHandler(object sender, EventArgs e) (or ImageClickEventArgs e for an image button)

The only useful information you can get here is the coordinates of the click from the image click event args.

The command event allows you to pass a command name and an argument to you function and is added to the button using OnCommand="commandHandler" and has the signature.

protected void commandHandler(object sender, CommandEventArgs e)
and this time you can retrieve the command name and arguments from the CommandEventArgs object.

However in your case you can't do what you want using this method.
CommandArgument = "ImageButton2.ImageUrl.ToString()"/>
will result in the string literal "ImageButton2.ImageUrl.ToString()" being passed to the function, which obviously isn't what you want. What you want to do can be achieved quite easily though accessing the properties of the button directly:

<asp:ImageButton ID="btnUpdate" runat="server"  ImageUrl ="~/images/mikey.jpg" OnClick = "btnUpdate_Click" />

Then in your code behind:

protect void btnUpdate_Click(object sender, EventArgs e)
{
     string Imageurl = ImageButton2.ImageUrl;
}
0
 
LVL 7

Expert Comment

by:ajolly
ID: 22696756
why don't you just write:

protected void btnUpdate_Click(object sender, EventArgs e)
{
    ImageButton1.ImageUrl = btnUpdate.ImageUrl;    
}
0
 

Author Closing Comment

by:finnatic
ID: 31505404
Thanks NazoUK... I thought there must be an easy solution and you have presented it beautifully. It seems so obvious no of course...

Thanks again.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question