Gloria
Gloria

Reputation: 1325

Passing a string argument to a javascript function

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 + "'}",

Upvotes: 0

Views: 2345

Answers (3)

chridam
chridam

Reputation: 103355

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);
        }
    }); 
}

Upvotes: 1

user1041792
user1041792

Reputation:

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');

Upvotes: 0

KJ Price
KJ Price

Reputation: 5964

I think you are missing a parenthesis ")":

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

Upvotes: 0

Related Questions