10

I have created a Handler which return integer value after doing some database work. i would like to know how can i get that value and assign that value to Label by calling that handler.

I have googled it and most of the example uses Jquery.AJAX calls to retrieve the value. I am sure i can also get the value by using that. BUT for some limitation in my company i am restricted to use code behind.

Any example will help.

Handler: http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC
which returns: 3

need to assign this to a label control

i have tried this much so far.

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Label1.Text = response.ToString() // this does not work
patel.milanb
  • 5,822
  • 15
  • 56
  • 92
  • what if you enter that URL into your browser, what do you got back? what's the format? – Quannt Jan 25 '13 at 10:26
  • if i enter the URL in browser i get the value.. exactly what i expect to get... but i dont know how to get that in aspx page. – patel.milanb Jan 25 '13 at 10:39
  • 1
    OKay,pls note that if you want to use HttpWebRequest, be careful with the response.Header, you may have to specify some header information in order to retrieve the data. – Quannt Jan 25 '13 at 10:43

2 Answers2

16

Use WebClient.DownloadString

WebClient client = new WebClient ();
Label1.Text = client.DownloadString ("http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC");

You could also directly call your handler using ajax and update the label.

Here is a jQuery example:

$.get('Stores/GetOrderCount.ashx?sCode=VIC', function(data) {
  $('.result').html(data);
});
nunespascal
  • 17,584
  • 2
  • 43
  • 46
5

Try this

System.IO.Stream stream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
string contents = reader.ReadToEnd();
Quannt
  • 2,035
  • 2
  • 21
  • 29