?
Solved

C#: What's wrong with my function call?

Posted on 2010-01-04
10
Medium Priority
?
323 Views
Last Modified: 2012-05-08
I am trying to change the ImageUrl of an ASP.NET image with a click.  I have the attached function.  Using the call below, I want to pass the name of the image to the function.  When I do, I'm told "changeImage(string)' has some invalid arguments".  What am I doing wrong?

<asp:ImageButton ID="ibnTopRight" runat="server" ImageUrl="top-right_THUMB.png" OnClick="changeImage('1')" />

Jeremy


public void changeImage(String strImageUrl)
  {
    imgChoice.ImageUrl = strImageUrl + ".png";
  }

Open in new window

0
Comment
Question by:mrcoulson
10 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 26172613
where is the code that throws the error? where do you call changeImage. you most likely aren't passing in a string.
0
 
LVL 14

Expert Comment

by:ajitha75
ID: 26172627
The function changeImage should have the standard signature

public void changeImage(object sender, ImageClickEventArgs e)
{
}

Thanks
Ajitha
0
 
LVL 8

Expert Comment

by:Jon500
ID: 26172633
A couple of things:

1) Try

(string strImageUrl)

in your function declaration. The string type you want is "string" not "String".

2) Why not go to the designer of your page, click the imagebutton control, access the "thunderbolt" events list, then double-click the "click" event and let VS create the event delegate and signature for you automatically? This would avoid type mismatches in the future.

Regards,
Jon500
0
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!

 
LVL 6

Expert Comment

by:jello024
ID: 26172638
server side button click events have an object and eventArgs arguments.
Example:

<asp:ImageButton ID="ibnTopRight" runat="server" ImageUrl="top-right_THUMB.png" OnClick="changeImage" />

protected void changeImage(object sender, ImageClickEventArgs e)
{
      //do stuff
 }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26172667
@Jon500

In C#, "string" is an alias for "String". Either will suffice.
0
 
LVL 8

Expert Comment

by:Jon500
ID: 26173265
@kaufmed:Hi. I wasn't sure if there is a compiler setting that makes one (string vs. String) preferred over the other since some environments can be set with different levels of "strictness". Thank you for clarifying and letting me know.
0
 

Author Comment

by:mrcoulson
ID: 26173336
Oh, I suppose I see.  I have usually done it with the thunderbolt events list or just by double-clicking the control, but I was hoping to be able to pass along a string for the URL instead of having a function that says "If the URL is this, make it this; otherwise make it this."  I was assuming there would be a better way to do it.

Jeremy
0
 
LVL 8

Expert Comment

by:Jon500
ID: 26173398
@mrcoulson: I don't understand why you would need to examine the value of the Url in order to append a ".png" to it in your event handler. Why not just do as you're doing and instead cast the sender to an ImageButton type and set its Url to its Url + ".png". Isn't that the same thing you're trying to do with your string-only event handler?

Jon500
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 total points
ID: 26173436
for Passing values on click....you should be using onCommand event and not the onclick.
Like this:

<asp:ImageButton ID="imgChoice" runat="server"
            oncommand="imgChoice_Command"
            CommandArgument="YourUrl"
            CommandName="MyClick"/>

protected void imgChoice_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "MyClick")
        {
            string url = (string)e.CommandArgument;
            imgChoice.ImageUrl = url + ".png";
        }
    }
0
 

Author Comment

by:mrcoulson
ID: 26173476
THAT does it.  That allows me to set the URL on the imagebutton so if this thing ever gets much bigger, I can have one tiny function instead of many tiny functions or one big function that says "if this is clicked, if this is clicked, if this is clicked..."

Thanks!

Jeremy
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

755 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