Dynamics CRM 2011编程系列(41):Portal的制造(二)
Dynamics CRM 2011编程系列(41):Portal的制作(二)
上篇博文介绍了Portal的数据显示控件,它们分别是:CrmMetadataDataSource,SavedQueryDataSource,XrmServiceContext。那本文来看看在Portal中对CRM的数据进行CRUD。
我们在Portal中对Dynamics CRM 系统中的数据进行CRUD操作可以有两个方向:1.使用ADO.NET直接操作Dynamics CRM 系统的数据库;2.使用Dynamics CRM 系统为制作Portal而提供的前期绑定代码(在上篇博文中使用过)。本文就不介绍第一种方法啦,就来好好琢磨下第二种方法啦! :D
我们来看个小DEMO吧:
涉及实体
客户
实现需求
1.显示现有客户基本信息
2.能修改客户的基本信息
3.能删除客户
4.能创建客户
实现步骤
图1
图2
图3
图4
图5
图6
程序代码
Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
height: 19px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4"
ForeColor="#333333" GridLines="None"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
DataKeyNames="name,telephone1,emailaddress1"
onrowdeleting="GridView1_RowDeleting" PageSize="20">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField runat="server" ID="hfAccountId" Value='<%#Eval("accountId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="名称">
<ItemTemplate>
<asp:Label runat="server" ID="lblName" Text='<%#Bind("name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtName" Text='<%#Bind("name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="电话">
<ItemTemplate>
<asp:Label runat="server" ID="lblTelephone1" Text='<%#Bind("telephone1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtTelephone1" Text='<%#Bind("telephone1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label runat="server" ID="lblEmailAddress1" Text='<%#Bind("emailaddress1") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEmailAddress1" Text='<%#Bind("emailaddress1") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
<div>
<br /><br /><br />
<table style="width:500;border-color:Black;" border="1" cellpadding="5" cellspacing="0">
<caption>
添加客户记录
</caption>
<colgroup>
<col id="info" width="100px" />
<col id="inputd" />
</colgroup>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="名称"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style1">
<asp:Label ID="Label2" runat="server" Text="电话"></asp:Label>
</td>
<td class="style1">
<asp:TextBox ID="txtTelephone1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtEmailAddress1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="添加" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Default2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xrm;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindData();
}
protected void BindData()
{
var xrm = new XrmServiceContext("Xrm");
var activedAccount = xrm.AccountSet;
GridView1.DataSource = activedAccount.Where(new Func<Account, bool>(a => { return a.StateCode ==0; }));
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var xrm = new XrmServiceContext("Xrm");
Account uptAccount = new Account();
GridViewRow uptRow = GridView1.Rows[e.RowIndex];
uptAccount.AccountId = Guid.Parse(((HiddenField)uptRow.Cells[0].FindControl("hfAccountId")).Value);
uptAccount.Name = ((TextBox)uptRow.Cells[1].FindControl("txtName")).Text;
uptAccount.Telephone1 = ((TextBox)uptRow.Cells[2].FindControl("txtTelephone1")).Text;
uptAccount.EMailAddress1 = ((TextBox)uptRow.Cells[3].FindControl("txtEmailAddress1")).Text;
xrm.Update(uptAccount);
xrm.SaveChanges();
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
var xrm = new XrmServiceContext("Xrm");
GridViewRow uptRow = GridView1.Rows[e.RowIndex];
Guid AccountId = Guid.Parse(((HiddenField)uptRow.Cells[0].FindControl("hfAccountId")).Value);
xrm.Delete("account", AccountId);
BindData();
}
protected void Button1_Click(object sender, EventArgs e)
{
var xrm = new XrmServiceContext("Xrm");
Account addAccount = new Account();
addAccount.Name = txtName.Text;
addAccount.Telephone1 = txtTelephone1.Text;
addAccount.EMailAddress1 = txtEmailAddress1.Text;
xrm.AddObject(addAccount);
xrm.SaveChanges();
BindData();
}
}