Solved

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

Posted on 2008-10-11
4
410 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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