0

The path is javascript path

    var fileName = args.get_fileName();
    lstImg.src = <%=GetListImageFilePath(fileName) %>

file name is error because it is javascript and not in .NET how to put this argument in .NET Code

1
  • is file name coming from a control? Commented Aug 1, 2012 at 5:30

5 Answers 5

3

You'll need to use AJAX. One easy way to do it would be to use PageMethods. First, add a [WebMethod] attribute to your method:

[WebMethod]
protected static string GetListImageFilePath(string fileName)
{

This method must be static.

Then set EnablePageMethods="True" on your script manager. Then you can call your c# code from JavaScript like this:

var fileName = args.get_fileName();
PageMethods.GetListImageFilePath(fileName, function (path) {
    lstImg.src = path;
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can't. The JavaScript runs on the client, and the asp.net code is on the server. You need to use some other way of communicating with the server eg: Ajax to a web service, a postback, etc

Comments

0

You simply can not do it because javascript is running at client side i.e on browser where as server code run at server. What you could do is change the your GetListImageFilePath function so that it returns the base URL for your image directory and then append the file name to create the image path.

var fileName = args.get_fileName();
lstImg.src = <%=GetListImageFilePath() %> + '/' + fileName;

For more information, like how server tags in Javascript are processed, I have answered a StackOverFlow thread here. Please have a look to clarify your doubt.

Comments

0

I think get_fileName() is server side function. So you can call it from the HTML directly. Check these links

http://weblogs.asp.net/jalpeshpvadgama/archive/2012/01/07/asp-net-page-methods-with-parameters.aspx http://stackoverflow.com/questions/7633557/asp-net-is-it-possible-to-call-methods-within-server-tag-using-eval

If you call the javascript function using RegisterStartupScript() or RegisterClientScriptBlock() then these will be called in client side not in server side.

If you want to call the javascript function immediately in server side then declare an equivalent server side function.

Comments

0

add an ashx(http handler) in your website, then you can use lstImg.src = '/example.ashx?name=' + fileName.

public class ExampleHandler: IHttpHandler {

    public void ProcessRequest (HttpContext context) {
    var request = context.Request;
        string fileName = (string)request.QueryString["name"];
        // your logic
        context.Response.Write(yourpath)
    }

    public bool IsReusable {
    get {
        return false;
    }
    }
}

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.