Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now