0

I tested the following codes many times. It works well and I get the result of "return photoMenu_sb.ToString();" as soon as I remove the string argument photoFileName. But when I declare and try to pass the string photoFileName nothing happens. Why?

I think the problem lies in declaring and passing the string photoFileName to the javascript function but I can't identify the problem since I don't get any error. Is there any missing quotations somewhere?

[WebMethod]
public static string LoadPhotosPopup(int offset, int fetch, string IDuserInput)
{
        StringBuilder photosPopup_sb = new StringBuilder();           
        var photosPopup_query = db.Query("SELECT IDphoto], [photoFileName] FROM [photos] WHERE ([IDuser] = @0 AND [photoPublished] = 'True')  ORDER BY [photoDate] DESC OFFSET @1 ROWS FETCH NEXT @2 ROWS ONLY", IDuserInput, offset, fetch);
        foreach (var item in photosPopup_query)
        {
            var IDphoto = item.IDphoto;
            var photoFileName = item.photoFileName;
            photosPopup_sb.Append("<img src=\"Images/Design/openmenu.png\" onclick=\"photoMenu(" + IDphoto + ", '" + photoFileName + "') \" />");                         
         }           
        return photosPopup_sb.ToString();
    }

JavaScript

function photoMenu(IDphoto, photoFileName) {   
$.ajax({
    type: "POST",
    url: "UserControls/photosFunctions.aspx/photoMenu",
    data: "{ IDphoto: " + IDphoto + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        $("#CommentsMenu" + IDphoto).html(data.d);
    }
})

}

[WebMethod]
            {   StringBuilder photoMenu_sb = new StringBuilder();                        
        photoMenu_sb.Append("<label>Testing </label>");
        return photoMenu_sb.ToString();
    }

UPDATE Since the public static string photoMenu(int IDphoto, string photoFileName) expects 2 arguments then

 data: "{ IDphoto: " + IDphoto + "}", in the javascript function should be 
 data: "{ IDphoto: " + IDphoto + ", photoFileName: '" + photoFileName + "'}",
9
  • What's the item.IDphoto type? Commented Jan 30, 2015 at 14:06
  • the item.IDphoto is int Commented Jan 30, 2015 at 14:09
  • In your javascript function photoMenu(IDphoto, photoFileName) I don't see where photoFileName is being used? Commented Jan 30, 2015 at 14:43
  • 1
    I think that's why it's failing because the ajax call is expecting a data parameter with photoFileName as well. I've edited my answer to include this, you could try that and see if it works for you. Commented Jan 30, 2015 at 14:54
  • 1
    I saw your answer.... One final thing plz update your answer by adding the quotaion marks as follows data: "{ IDphoto: " + IDphoto + ", photoFileName: '" + photoFileName + "'}", Commented Jan 30, 2015 at 15:09

3 Answers 3

1

Try creating a string variable that holds the javascript function string:

var img = String.Format("<img src='Images/Design/openmenu.png' onclick='photoMenu({0},'{1}')' />", item.IDphoto, item.photoFileName);
photosPopup_sb.Append(img);  

In your JavaScript, you need to also set the json data property for the photofileName parameter:

function photoMenu(IDphoto, photoFileName) {   
    $.ajax({
        type: "POST",
        url: "UserControls/photosFunctions.aspx/photoMenu",
        data: "{ IDphoto: " + IDphoto + ", photoFileName: '" + photoFileName + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
           $("#CommentsMenu" + IDphoto).html(data.d);
        }
    }); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Try single quotes in the place of the escaped double quotes - editing my answer to reflect this
Still nothing ..... However when I remove photoFileName and I stop passing it to the JavaScript function then the code works well. var img = String.Format("<img src='Images/Design/openmenu.png' onclick='photoMenu({0})' />", item.IDphoto);
And if you remove the single quotes inside the photoMenu function like ...'photoMenu({0}, {1})', item.IDphoto, item.photoFileName);?
You need to inspect what item.photoFileName is returning, @Shannon raised some very valid points: it could be that the filename can have incorrectly formatted file path? Just my $0.02
0

I think you are missing a parenthesis ")":

photosPopup_sb.Append("<img src=\"Images/Design/openmenu.png\" onclick=\"photoMenu(" + IDphoto + ", '" + photoFileName + "')\" />"); 

1 Comment

There is a paranthesis in my original code. I must have deleted it by mistake while typing the codes in here. I've edited my question.
0

I don't see an example value of your photoFileName but if it has backslashes, you would probably have to make sure that the string is escaped properly.

For example:

//causes errors
called(1, 'file\path\img.png');

//successful
called(1, 'file/path/img.png');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.