Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

19 Experts available now in Live!

Get 1:1 Help Now