Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using system. IO;
Namespace data and communication: webrequest
{
Public partial class mainpage: usercontrol
{
Public mainpage ()
{
Initializecomponent ();
}
Private void usercontrol_loaded (Object sender, routedeventargs E)
{
List <book> books = new list <book> (){
New Book ("professional ASP. NET 3.5 "),
New Book ("ASP. NET Ajax in action "),
New Book ("Silverlight in action "),
New Book ("ASP. NET 3.5 unleashed "),
New Book ("Introducing Microsoft ASP. NET Ajax ")
};
Books. itemssource = books;
}
Private string bookno;
Void books_selectionchanged (Object sender, selectionchangedeventargs E)
{
Bookno = books. selectedindex. tostring ();
Uri endpoint = new uri ("http: // localhost: 8675/bookhandler. ashx ");
Webrequest request = webrequest. Create (endpoint );
Request. method = "Post ";
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. begingetrequeststream (New asynccallback (requestready), request );
}
void requestready (iasyncresult asyncresult)
{< br> webrequest request = asyncresult. asyncstate as webrequest;
stream requeststream = request. endgetrequeststream (asyncresult);
using (streamwriter writer = new streamwriter (requeststream)
{< br> writer. write (string. format ("NO = {0}", bookno);
writer. flush ();
}< br> request. begingetresponse (New asynccallback (responseready), request);
}
Void responseready (iasyncresult asyncresult)
{
Webrequest request = asyncresult. asyncstate as webrequest;
Webresponse response = request. endgetresponse (asyncresult );
Using (Stream responsestream = response. getresponsestream ())
{
Streamreader reader = new streamreader (responsestream );
String price = reader. readtoend ();
If (this. lblprice. checkaccess ())
{
This. lblprice. Text = "Price:" + price;
}
Else
{
This. Dispatcher. begininvoke (New uicontextchangedeventhandler (uicontextchanged), price );
}
}
}
Public Delegate void uicontextchangedeventhandler (Object State );
Private void uicontextchanged (object state)
{
This. lblprice. Text = "Price:" + (string) State;
}
}
}