Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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