0

I am trying to access a string from code behind into javascript in aspx page

code behind

  protected void btnSearch_Click(object sender, EventArgs e)
        {            
            List<string> imageList = new List<string>();
            string images="";
            imageList = GetMatchingImages(@"C:\Users\Shahzad\Documents\Visual Studio 2013\Projects\ImageSearchEngine\ImageSearchEngine\Images", txtSearch.Text);

            foreach (var image in imageList)
            {
                images += "\"" + image + "\",";
            }
            images ="["+ images.Substring(0, images.Length - 1)+"]";

        }

        public List<string> GetMatchingImages(string path, string keyword)
        {
            var matches = new List<string>();

            var images = System.IO.Directory.GetFiles(path);

            foreach (var image in images)
            {
                if (image.Contains(keyword))
                {
                    matches.Add(image);
                }
            }

            return matches;
        }

and in the aspx page

<script type="text/javascript">
     var imagesList;
     function getImages()
     {
         return <%=images%>;
     }
     imagesList = getImages();
 </script>

but it is giving error 'images does not exist in the current context' plz help me find out where I am making the mistake

1 Answer 1

1

This must be issue of the scope of images variable. Try declaring it outside btnSearch_Click function.

This could possibly fix the problem as images is currently inaccesible outside of this function.

Thanks, zryw141

Sign up to request clarification or add additional context in comments.

2 Comments

Outside of the function and protected or public :-)
simba, if you can, mark the asnwers that helped you solve your problem as accepted answer. This will help other people that might have similar problem in the future and will help people focus on answers that haven't been answered yet.

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.