后台代码
//按钮事件voID savebutton_Click(object sender,RoutedEventArgs e){ if (username.Text.Trim() == string.Empty) { errMessage.Foreground = new SolIDcolorBrush(colors.Red); errMessage.Text = "请输入用户名称!"; errMessage.Visibility = Visibility.Visible; return; } //调用WebService WebServiceProxy.UserManageSoapClIEnt userMgrSoapClIEnt = new YJingLee.WebSrv.WebServiceProxy.UserManageSoapClIEnt(); //创建用户 *** 作 userMgrSoapClIEnt.createuserAsync(username.Text); userMgrSoapClIEnt.createuserCompleted += new EventHandler<YJingLee.WebSrv.WebServiceProxy. createuserCompletedEventArgs> (userMgrSoapClIEnt_createuserCompleted);}voID userMgrSoapClIEnt_createuserCompleted(object sender,YJingLee.WebSrv.WebServiceProxy.createuserCompletedEventArgs e){ if (e.Error == null) { errMessage.Text = "创建用户成功!"; errMessage.Foreground = new colors.Blue); errMessage.Visibility = Visibility.Visible; } else { errMessage.Foreground = new colors.Red); errMessage.Text = e.Error.ToString(); errMessage.Visibility = Visibility.Visible; }}查询数据部分 我们使用Silverlight 2自带的DataGrID控件绑定数据。前台非常简单,只是一个DataGrID控件,但是前段时间有的同学问DataGrID控件不知怎么弄进来。这里详细说明一下。
第一步:在Silverlight工程中添加引用
第二步:查找System.windows.Controls.Data程序集,添加进来
第三步:在UserControl中添加这个引用,有智能感知。我将其命名为Data。
在前台编写代码如下
<button x:name="rebutton" Content="刷新" WIDth="50" Height="30" GrID.Row="0"></button><Data:DataGrID x:name="userDataGrID" Height="200" WIDth="700" margin="0,5,10" autoGenerateColumns="True" VerticalAlignment="top" GrID.Row="1"></DataGrID>//显示数据voID ListingControldisplay(object sender,175)">RoutedEventArgs e){ WebServiceProxy.UserManageSoapClIEnt(); userMgrSoapClIEnt.RetrIEveUsersAsync(); userMgrSoapClIEnt.RetrIEveUsersCompleted += new RetrIEveUsersCompletedEventArgs> (userMgrSoapClIEnt_RetrIEveUsersCompleted);}voID userMgrSoapClIEnt_RetrIEveUsersCompleted(object sender,175)">RetrIEveUsersCompletedEventArgs e){ if (e.Error == null) displayData(e.Result);}private voID displayData(string xmlContent){ try { if (xmlContent != string.Empty) { Xdocument xmlUsers = Xdocument.Parse(xmlContent); var users = from user in xmlUsers.Descendants("User") select new { UserID = Convert.ToInt32 (user.Element("UserID").Value),Username = (string) user.Element("Username").Value }; List<User> usersList = new User>(); foreach (var u in users) { User use = new User { UserID = u.UserID,Username = u.Username }; usersList.Add(use); } userDataGrID.ItemsSource = usersList; } else { userDataGrID.ItemsSource = null; } } catch (Exception ex) { Console.Write(ex.Message); }}public class User{ public int UserID { get; set; } public string Username { get; set; }} 修改数据部分 voID updatebutton_Click(object sender,175)">RoutedEventArgs e){ if (userID.Text.Trim() == string.Empty) { errMessage.Foreground = new "请输入用户ID!"; errMessage.Visibility = Visibility.Visible; return; } if (username.Text.Trim() == string.Empty) { errMessage.Foreground = new Visibility.Visible; return; } WebServiceProxy.UserManageSoapClIEnt(); //调用更新用户方法 userMgrSoapClIEnt.UpdateUserAsync (Int16.Parse(userID.Text.Trim()),username.Text.Trim()); userMgrSoapClIEnt.UpdateUserCompleted += new UpdateUserCompletedEventArgs> (userMgrSoapClIEnt_UpdateUserCompleted);}voID userMgrSoapClIEnt_UpdateUserCompleted(object sender,175)">UpdateUserCompletedEventArgs e){ if (e.Error == null) { errMessage.Text = "修改用户成功!"; errMessage.Foreground = new 删除数据部分 voID deletebutton_Click(object sender,175)">UserManageSoapClIEnt(); //调用删除方法 userMgrSoapClIEnt.DeleteUserAsync (Int16.Parse(userID.Text.Trim())); userMgrSoapClIEnt.DeleteUserCompleted+= new DeleteUserCompletedEventArgs> (userMgrSoapClIEnt_DeleteUserCompleted);}voID userMgrSoapClIEnt_DeleteUserCompleted(object sender,175)">DeleteUserCompletedEventArgs e){ if (e.Error == null) { errMessage.Text = "删除用户成功!"; errMessage.Foreground = new 整合程序
在Page.xaml页面中布局,并引入用户控件,添加4个Hyperlinkbutton ,单击事件用户控件在中间区域显示。例如下面一个按钮事件:
deleteCtl.Visibility = Visibility.Visible;entryCtl.Visibility = Visibility.Collapsed;ListingCtl.Visibility = Visibility.Collapsed;editCtl.Visibility = Visibility.Collapsed;
最终效果图如下所示:
结语
利用这个实例我们学习了在Silverlight 2中使用ASP.NET Web Service进行数据CRUD *** 作,这里有一些细节没有完善,比如输入框的验证问题等。下一篇我们利用ADO.NET Data Service来 *** 作数据。
总结以上是内存溢出为你收集整理的Silverlight 2 (beta1)数据 *** 作(2)——使用ASP.NET Web Service进行数据CRUD *** 作(下)全部内容,希望文章能够帮你解决Silverlight 2 (beta1)数据 *** 作(2)——使用ASP.NET Web Service进行数据CRUD *** 作(下)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)