How to update Inner controls value in list in windows phone 7? -
i working in windows phone application have 1 listbox control. , 1 button inner control.
so in list 20 buttons after binding data. want change data of button once clicked.
this sample code. first app windows phone.
<listbox grid.row="1" horizontalalignment="left" margin="10,10,0,0" name="lstinstagramtags" verticalalignment="top" width="444" height="562" selectionchanged="lstinstagramtags_selectionchanged"> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="horizontal" height="100" margin="-10,-10,-10,-10"> <button click="button_click" content="needtochangethisvalue" width="150" fontsize="13" height="60" margin="-560,35,5,5" /> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox> private void button_click(object sender, routedeventargs e) { httpwebrequest myrequest = (httpwebrequest)httpwebrequest.create(myuri); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.begingetrequeststream(new asynccallback(getrequeststreamcallback), myrequest); } void getrequeststreamcallback(iasyncresult callbackresult) { httpwebrequest myrequest = (httpwebrequest)callbackresult.asyncstate; // end stream request operation stream poststream = myrequest.endgetrequeststream(callbackresult); string postdata = "action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176"; // create post data string postdata = postdata; byte[] bytearray = encoding.utf8.getbytes(postdata); // add post data web request poststream.write(bytearray, 0, bytearray.length); poststream.close(); // start web request myrequest.begingetresponse(new asynccallback(getresponsetstreamcallback), myrequest); } void getresponsetstreamcallback(iasyncresult callbackresult) { httpwebrequest request = (httpwebrequest)callbackresult.asyncstate; httpwebresponse response = (httpwebresponse)request.endgetresponse(callbackresult); using (streamreader httpwebstreamreader = new streamreader(response.getresponsestream())) { **string result** = httpwebstreamreader.readtoend(); } }
i want change button data based on result (marked bold). not getting way update control on basis of result.
please suggest me way this.
thanks
i think question option based, here 3 ideas
- globul button
define button in current page
private button helerbutton; private void button_click(object sender, routedeventargs e) { helerbutton = (button)sender; httpwebrequest myrequest = (httpwebrequest)httpwebrequest.create(myuri); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.begingetrequeststream(new asynccallback(getrequeststreamcallback), myrequest); } void getresponsetstreamcallback(iasyncresult callbackresult) { httpwebrequest request = (httpwebrequest)callbackresult.asyncstate; httpwebresponse response = (httpwebresponse)request.endgetresponse(callbackresult); using (streamreader httpwebstreamreader = new streamreader(response.getresponsestream())) { string result = httpwebstreamreader.readtoend(); helerbutton.content = result } }
- second approach
you can change code architecture little bit
private void button_click(object sender, routedeventargs e) { httpwebrequest myrequest = (httpwebrequest)httpwebrequest.create(myuri); myrequest.method = "post"; myrequest.contenttype = "application/x-www-form-urlencoded"; myrequest.begingetrequeststream(r => { var httprequest = (httpwebrequest)r.asyncstate; var httpresponse = (httpwebresponse)httprequest.endgetresponse(r); stream poststream = myrequest.endgetrequeststream(r); string postdata = "action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176"; string postdata = postdata; byte[] bytearray = encoding.utf8.getbytes(postdata); poststream.write(bytearray, 0, bytearray.length); poststream.close(); httprequest.begingetresponse(q => { httpwebrequest request = (httpwebrequest)q.asyncstate; httpwebresponse response = (httpwebresponse)request.endgetresponse(q); using (streamreader httpwebstreamreader = new streamreader(response.getresponsestream())) { string result = httpwebstreamreader.readtoend(); dispatcher.begininvoke(() => { button b = (button)sender; b.content = result; //for consideration //b.isenabled }); } }, httprequest); }, myrequest); }
- final approach( i, personally, choose this)
is create tasks async , await http networking on windows phone
Comments
Post a Comment