Front Desk Interface
Background code
//Button eventvoidsaveButton_Click (ObjectSenderRoutedEventArgsE) {if(UserName.Text.Trim () = =string. Empty) {Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text ="Please enter the user name!" "; Errmessage.visibility =Visibility. Visible;return; }//Call WebServiceWebServiceProxy.usermanagesoapclientUsermgrsoapclient =NewYJingLee.WebSrv.WebServiceProxy.usermanagesoapclient();//Create user ActionsUsermgrsoapclient.createuserasync (Username.text); usermgrsoapclient.createusercompleted + =NewEventHandler<yjinglee.websrv.webserviceproxy.Createusercompletedeventargs> (usermgrsoapclient_createusercompleted);}voidUsermgrsoapclient_createusercompleted (Objectsender, YJingLee.WebSrv.WebServiceProxy.CreateusercompletedeventargsE) {if(E.error = =NULL) {Errmessage.text ="Create User success!" "; Errmessage.foreground =NewSolidColorBrush(Colors. Blue); Errmessage.visibility =Visibility. Visible; }Else{Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text = E.error.tostring (); Errmessage.visibility =Visibility. Visible; }}
Querying data part of the foreground interface
We bind data using the DataGrid control that comes with Silverlight 2. The front desk is very simple, just a DataGrid control, but some time ago some classmates asked the DataGrid control somehow to get in. Here is a detailed explanation.
Step One: Add references in the Silverlight project
Step two: Find the System.Windows.Controls.Data assembly, add it in
Step Three: Add this reference in UserControl with IntelliSense. I'll name it data.
Write the code at the front desk as follows
<Buttonx:Name= "Rebutton"Content= "Refresh"Width= " the"Height= "+"Grid.Row= "0" ></Button><Data:DataGridx:Name= "Userdatagrid"Height= "$" Width= "The "Margin= "0,5,0,10"AutoGenerateColumns= "True" VerticalAlignment= "Top"Grid.Row= "1" ></Data:DataGrid>
Background code
//Display datavoidListingcontroldisplay (ObjectSenderRoutedEventArgse) {WebServiceProxy.usermanagesoapclientUsermgrsoapclient =NewYJingLee.WebSrv.WebServiceProxy.usermanagesoapclient(); Usermgrsoapclient.retrieveusersasync (); usermgrsoapclient.retrieveuserscompleted + =NewEventHandler<yjinglee.websrv.webserviceproxy.Retrieveuserscompletedeventargs> (usermgrsoapclient_retrieveuserscompleted);}voidUsermgrsoapclient_retrieveuserscompleted (Objectsender, YJingLee.WebSrv.WebServiceProxy.RetrieveuserscompletedeventargsE) {if(E.error = =NULL) Displaydata (E.result);}private voidDisplaydata (stringXmlcontent) {Try{if(Xmlcontent! =string. Empty) {XDocumentXmlusers =XDocument. Parse (xmlcontent);varUsers = fromUserinchXmlusers.descendants ("User")Select New{UserID =Convert. ToInt32 (user. Element ("UserID"). Value), UserName = (string) User. Element ("UserName"). Value};List<User> userslist =NewList<User> ();foreach(varUinchUsers) {UserUse =NewUser{UserID = U.userid, UserName = U.username}; Userslist.add (use); } Userdatagrid.itemssource = Userslist; }Else{Userdatagrid.itemssource =NULL; } }Catch(ExceptionEx) {Console. Write (ex. Message); }}Public classUser{public intUserID {Get;Set; }Public StringUserName {Get;Set; }}
Modify data part of the foreground interface
Background code
voidUpdatebutton_click (ObjectSenderRoutedEventArgsE) {if(UserID.Text.Trim () = =string. Empty) {Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text ="Please enter user id! "; Errmessage.visibility =Visibility. Visible;return; }if(UserName.Text.Trim () = =string. Empty) {Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text ="Please enter the user name!" "; Errmessage.visibility =Visibility. Visible;return; } webserviceproxy.usermanagesoapclientUsermgrsoapclient =NewYJingLee.WebSrv.WebServiceProxy.usermanagesoapclient();//Call Update user methodUsermgrsoapclient.updateuserasync (Int16. Parse (UserID.Text.Trim ()), UserName.Text.Trim ()); usermgrsoapclient.updateusercompleted + =NewEventHandler<yjinglee.websrv.webserviceproxy.Updateusercompletedeventargs> (usermgrsoapclient_updateusercompleted);}voidUsermgrsoapclient_updateusercompleted (Objectsender, YJingLee.WebSrv.WebServiceProxy.UpdateusercompletedeventargsE) {if(E.error = =NULL) {Errmessage.text ="Modify user success!" "; Errmessage.foreground =NewSolidColorBrush(Colors. Blue); Errmessage.visibility =Visibility. Visible; }Else{Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text = E.error.tostring (); Errmessage.visibility =Visibility. Visible; }}
Delete Data part of the foreground interface
Background code
voidDeletebutton_click (ObjectSenderRoutedEventArgsE) {if(UserID.Text.Trim () = =string. Empty) {Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text ="Please enter user id! "; Errmessage.visibility =Visibility. Visible;return; } webserviceproxy.usermanagesoapclientUsermgrsoapclient =NewYJingLee.WebSrv.WebServiceProxy.usermanagesoapclient();//Call Delete methodUsermgrsoapclient.deleteuserasync (Int16. Parse (UserID.Text.Trim ())); usermgrsoapclient.deleteusercompleted+=NewEventHandler<yjinglee.websrv.webserviceproxy.Deleteusercompletedeventargs> (usermgrsoapclient_deleteusercompleted);}voidUsermgrsoapclient_deleteusercompleted (Objectsender, YJingLee.WebSrv.WebServiceProxy.DeleteusercompletedeventargsE) {if(E.error = =NULL) {Errmessage.text ="Delete User success!" "; Errmessage.foreground =NewSolidColorBrush(Colors. Blue); Errmessage.visibility =Visibility. Visible; }Else{Errmessage.foreground =NewSolidColorBrush(Colors. Red); Errmessage.text = E.error.tostring (); Errmessage.visibility =Visibility. Visible; }}
Integration Program
In the Page.xaml page layout, and introduce the user control, add 4 Hyperlinkbutton, click the event user control to display in the middle area. For example, one of the following button events:
Visibility Visibility Visibility Visibility. Collapsed;
The end result is as follows:
Conclusion
Using this example we learned to use ASP. NET Web service for data crud operations in Silverlight 2, where some details are not perfect, such as the validation of input boxes. In the next section, we use the ADO data service to manipulate the information.
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
Silverlight 2 (beta1) data Manipulation (2)--Data crud operations with ASP.