Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of images in a web site. I am trying to check, is all images is there in the web site. So I have binded list of images in gridview. Then in button click I need to check the existance of image.If this happens asynchronously then it will be good to user.
So I tried to use Pagemethod in AJAX. I trigger Page method function from Javascript as follows,
Default.aspx
JavaScript
var lbl='Label';
function getDate() {
for (var x = 1; x < 10; x++)
{
lbl=lbl+x;
document.getElementById(lbl).innerText='Loading...';
PageMethods.GetData(x),OnSucceeded,OnFailed);
}
}
// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
var Arr=result.split('`');
Arr[0]='label'+Arr[0];
document.getElementById(Arr[0]).innerText = Arr[1];
}
// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
document.getElementById('label').innerText = 'Error occured';
}


Default.aspx.cs
C#
[System.Web.Services.WebMethod]
    public static string GetData(string IO)
    {
        string str, url;
        url = 'http://www.samplesite/images/'+IO+'gif';
        try
        {
            System.Net.HttpWebRequest webrequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
 
            System.Net.HttpWebResponse myresponse = (System.Net.HttpWebResponse)webrequest.GetResponse();
            if (myresponse.StatusCode == System.Net.HttpStatusCode.OK)
                str = "True";
            else
                str = "False";
        }
        catch (Exception e)
        {
            if (e.Message.Contains("404"))
                str = "False";
            else
                str = "Error";
        }
        return (IO + "`" + str);
    }

My issue is if I have this execute it works fine for x is less than 30 or 40. I have over 100 images in the site. If x exceeds 50 then pagemethod doesnot respond appropriately.

Kindly help in this regard.
Posted
Updated 18-Oct-10 7:59am
v6
Comments
Sandeep Mewara 18-Oct-10 12:12pm    
Can you elaborate more on what kinda issues? Whats not responding properly? your findings?
Abhinav S 18-Oct-10 12:30pm    
"Does not respond appropriately" - do you get an error?
eceramanan 18-Oct-10 13:30pm    
Thanks for reply Abhinav. This code works perfectly, not all times.If i run Javascript loop more than 40 times, the server function GetData(IO) doesnot get trigered. If i have 99 images, i will make JavaScript function as,

var lbl='Label';
function getDate()
{
for (var x = 1; x < 100; x++)
{
lbl=lbl+x; document.getElementById(lbl).innerText='Loading...';
PageMethods.GetData((x),OnSucceeded,OnFailed);
}
}


Now OnSucceeded(...) or OnFailed(...)have to be executed 99 times.But Either OnSucceeded(...) or OnFailed(...) is not executed completly(99 times).
eceramanan 19-Oct-10 4:03am    
am i able to clearly explain you people? My concern is, i am not able to hit and get response from Pagemethod 'GetData()' for more than 40 times in loop.Is there some thing that we will not be able to call Pagemethod from Javascript not more than certain number of times?

1 solution

Finally got the culprit. It's me, :)
C#
finally
  {
    if (myresponse != null) myresponse.Close();
  }


Default.aspx.cs
<code>
[System.Web.Services.WebMethod]    
public static string GetData(string IO)    
{        
  string str, url;        
  url = 'http://www.samplesite/images/'+IO+'gif';      
  try
  {        
    System.Net.HttpWebRequest webrequest = (System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create(url);       
    System.Net.HttpWebResponse myresponse = (System.Net.HttpWebResponse)webrequest.GetResponse();            
    if(myresponse.StatusCode==System.Net.HttpStatusCode.OK)       
       str = "True";            
    else
       str = "False";        
  }
  catch (Exception e)        
  {          
   if (e.Message.Contains("404"))              
     str = "False";            
   else
     str = "Error";        
  }     
  finally
  {
    if (myresponse != null) myresponse.Close();
  }

  return (IO + "`" + str);    
}
</code>

I have not closed my HttpWebResponse in my web method, which made this confusion. Pagemethod doenot have nothing to do here. Sorry for miss lead this issue. Learned a lesson "Approach the issue in all possible ways" and not in a single minded. Thanks to Sandeep and Abhinav.
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900