[关闭]
@Aiti 2019-03-21T09:14:13.000000Z 字数 13676 阅读 404

XPO随记

1.页面中Js如何对DevExpress的控件进行操作


可在浏览器中检查对应控件的ID,ID.value进行赋值;asp控件加上属性ClientIDMode="Static",可直接使用ID或者可用document.getElementById()对进行操作
JS:

  1. function Clear() {
  2. txtProjectName.value = "";
  3. txtName.value = "";
  4. txtBuildType_I.value = "";
  5. txtType_I.value = "";
  6. btnSearch.click();
  7. }

页面:

  1. <div class="search-name">立项名称:<asp:TextBox runat="server" ClientIDMode="Static" ID="txtProjectName" /></div>
  2. <div class="search-name">申请人姓名:<asp:TextBox runat="server" ClientIDMode="Static" ID="txtName" /></div>
  3. <div class="search-name">立项类型: <dx:ASPxComboBox runat="server" ID="txtBuildType" ClientIDMode="Static" style="float:right; margin-top:-10px;">
  4. <Items>
  5. <dx:ListEditItem Value="科研" Text="科研" />
  6. <dx:ListEditItem Value="工法" Text="工法" />
  7. </Items>
  8. </dx:ASPxComboBox></div>

2.XPO的几种类型查询方式


  1. XpoDs.Criteria = CriteriaOperator.Parse("StartsWith(Scope,?) AND LastUpdateTime BETWEEN(?,?) AND QualiTime BETWEEN(?,?)", dep.FullCode,
  2. StartDate.Date, EndDate.Date == DateTime.MinValue ? DateTime.MaxValue : EndDate.Date , txtStart.Date,txtEnd.Date).ToString();
  3. XpoProject.Criteria = $"StartsWith(Scope,'{ScopeHelper.CurrentScope()}')";
  4. var op = CriteriaOperator.Parse("StartsWith(Scope,?)", ScopeService.CurrentScope());
  5. op =CriteriaOperator.And(op,CriteriaOperator.Parse($"Department.Oid='{cmbDept.Value.ToString()}'"));
  6. op = CriteriaOperator.And(op,CriteriaOperator.Parse($"Number like '%{txtNumber.Text}%'"));
  7. op = CriteriaOperator.And(op, CriteriaOperator.Parse($"PlanTime <= #{txtEnd.Text}#"));
  8. XpoProject.Criteria = op.ToString();

3.GridView列表信息显示不存入数据库信息


Gridview 加事件 OnHtmlRowPrepared="gvExpert_HtmlRowPrepared"
页面

  1. <dx:GridViewDataTextColumn VisibleIndex="3" FieldName="Num" Caption="序号">
  2. <EditFormSettings Visible="False" />
  3. </dx:GridViewDataTextColumn>

表:[DisplayName("序号"), NonPersistent]
public int Num;
后台:

  1. protected void gvExpert_HtmlRowPrepared(object sender,DevExpress.Web.ASPxGridViewTableRowEventArgs e)
  2. {
  3. e.Row.Cells[0].Text = (e.VisibleIndex + 1).ToString();
  4. }

4.枚举类做成Checkbox


Js:

  1. function AllRStatusChange(s, e) {
  2. chxRStatus.SetEnabled(!s.GetValue());
  3. }
  4. $(function () {
  5. chxRStatus.SetEnabled(!AllRStatus.GetValue());
  6. });
  7. 页面
  8. <div class="search-name">研发状态:
  9. <dx:ASPxCheckBoxList runat="server" ID="chxRStatus" CssClass="Rnb" RepeatDirection="Horizontal" ClientIDMode="Static">
  10. </dx:ASPxCheckBoxList>
  11. <dx:ASPxCheckBox runat="server" ID="AllRStatus" ClientIDMode="Static" Text="全部" CssClass="Rnb" Checked="true">
  12. <ClientSideEvents CheckedChanged="AllRStatusChange" />
  13. </dx:ASPxCheckBox> </div>

加载:

  1. if (!IsPostBack){
  2. EnumHelper.SetListControl(chxRStatus, typeof(ResearchStatus));
  3. chxRStatus.Items[0].Selected = chxRStatus.Items[1].Selected = true;
  4. }
  5. 查询:
  6. if (!AllRStatus.Checked)
  7. {
  8. CriteriaOperator pStatus = CriteriaOperator.Parse("1=0");
  9. foreach (var i in chxRStatus.SelectedValues)
  10. {
  11. pStatus = CriteriaOperator.Or(pStatus, CriteriaOperator.Parse("ResearchStatus=?", i));
  12. }
  13. op = CriteriaOperator.And(op, pStatus);
  14. }

5.新建时,信息来自另一张表,Title本表字段名

  1. <dx:GridViewDataComboBoxColumn FieldName="Title!Key" VisibleIndex="8" Caption="职称">
  2. <PropertiesComboBox TextField="Name" ValueField="Oid" DataSourceID="XpoTitle">
  3. </PropertiesComboBox>
  4. </dx:GridViewDataComboBoxColumn>

6.枚举类新建时 或 数据显示英文时 需要 初始化 事件OnCellEditorInitialize

  1. protected void gvAll_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs e)
  2. {
  3. if (e.Column.ToString()=="项目类别")
  4. {
  5. e.DisplayText = EnumHelper.GetDescription(e.Value);
  6. }
  7. }
  8. protected void gvAll_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridViewEditorEventArgs e)
  9. {
  10. if (e.Column.FieldName == "ProjectType")
  11. {
  12. ASPxComboBox combo = e.Editor as ASPxComboBox;
  13. EnumHelper.SetListControl(combo, typeof(TechManager.Module.Component.Techs.ProjectType));
  14. }
  15. }
  16. protected void gvAll_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
  17. {
  18. if (e.ButtonType == ColumnCommandButtonType.New)
  19. {
  20. e.Visible = true;
  21. }
  22. }
  23. //....TreeList 枚举类
  24. <dx:TreeListComboBoxColumn FieldName="Type" Caption="类别" VisibleIndex="3">
  25. <DataCellTemplate><%# bim999.OA.Module.Engine.EnumHelper.GetDescription(Eval("Type")) %></DataCellTemplate>
  26. </dx:TreeListComboBoxColumn>

7.验证不能为空、信息验证/格式要求

http://www.cnblogs.com/Leon-Jenny/p/4923399.html

  1. <dx:GridViewDataTextColumn FieldName="Proposer" VisibleIndex="5" Caption="申请人姓名">
  2. <PropertiesTextEdit>
  3. <ValidationSettings>
  4. <RequiredField IsRequired="true" ErrorText="不能为空" />
  5. </ValidationSettings>
  6. </PropertiesTextEdit>
  7. </dx:GridViewDataTextColumn>
  8. <dx:GridViewDataMemoColumn FieldName="Actuality" VisibleIndex="18" Caption="国内外现状">
  9. <PropertiesMemoEdit>
  10. <ValidationSettings>
  11. <RequiredField IsRequired="true" ErrorText="不能为空" />
  12. </ValidationSettings>
  13. </PropertiesMemoEdit>
  14. <PropertiesMemoEdit Rows="5" >
  15. </PropertiesMemoEdit>
  16. <EditFormSettings Visible="True" ColumnSpan="2" />
  17. </dx:GridViewDataMemoColumn>
  18. <dx:GridViewDataTextColumn FieldName="Email" VisibleIndex="4">
  19. <PropertiesTextEdit>
  20. <ValidationSettings>
  21. <RegularExpression ErrorText="请填写正确Email"ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" />
  22. <RegularExpression ErrorText="请填写数字" ValidationExpression="^[1-9]\d*$" />
  23. </ValidationSettings>
  24. </PropertiesTextEdit>
  25. </dx:GridViewDataTextColumn>
  26. <dx:GridViewDataTextColumn FieldName="Actual" VisibleIndex="5" Caption="保留四位小数">
  27. <PropertiesTextEdit DisplayFormatString="0.0000"> </PropertiesTextEdit>
  28. </dx:GridViewDataTextColumn>

8.关联关系的对象存储

一对多

  1. public class Order : XPObject
  2. {
  3. public Order(Session session) : base(session) { }
  4. [DisplayName("单号")]
  5. public string No;
  6. [DisplayName("日期")]
  7. public DateTime Time;
  8. [DisplayName("备注")]
  9. public string Note;
  10. [Association("OrderSalesMan", typeof(Emp))]
  11. public XPCollection SalesMans => GetCollection("SalesMans");
  12. }
  13. public class Emp : XPObject
  14. {
  15. public Emp(Session session) : base(session) { }
  16. [Indexed(Unique = true)]
  17. [DisplayName("编号")]
  18. public string Number;
  19. [DisplayName("名称")]
  20. public string Name;
  21. [Association("OrderSalesMan")]
  22. public Order Order;
  23. }

如果需求是 当Emp中已经存在了一条关联Order记录,将不予再建记录加判断条件

  1. op = CriteriaOperator.And(op, CriteriaOperator.Parse("OrderSalesMan.Count=0"));

9.控件调用JS函数

  1. <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="javascript: WeChat();return ture;"
  2. CausesValidation="False" Onclick="page_clcik"><i class="**"></i>绑定微信</asp:LinkButton>

其中return ture;是继续执行后面的代码;可能导致窗口重新刷新;fasle则不继续执行;

10.获取Web.config配置文件信息

  1. <appSettings>
  2. <add key="XpoUserConnectionString" value="XpoProvider=MSSqlServer;data source=.;user id=sa;password=cx1234;initial catalog=CrecSh_TechMgr1;Persist Security Info=true" />
  3. <add key="XpoConnectionString" value="XpoProvider=MSSqlServer;data source=.;user id=sa;password=cx1234;initial catalog=CrecSh_TechMgr1;Persist Security Info=true" />
  4. <add key="Company" value="中铁上海工程局集团有限公司" />
  5. <add key="License" value="12F2129B6574DDE5240BB49A02ECE0B4" />
  6. <add key="DateFormat" value="yyyy-MM-dd" />
  7. <add key="AppCode" value="TechMgr" />
  8. <add key="SmsApiUrl" value="http://192.168.1.240:806/api" />
  9. <add key="SmsGroup" value="itech" />
  10. <add key="SmsKey" value="itech_99" />
  11. <add key="WeChatPID" value="itech_1" />
  12. </appSettings>

使用以下代码获取:

  1. Company.Text = ConfigurationManager.AppSettings["Company"];

11.XPO几种查询数据的方法

  1. //1、查询得到的数据类型不一样/
  2. var views = new XPCollection<View>(Session, CriteriaOperator.Parse("Article.Oid=?", Oid));
  3. //or
  4. //2
  5. new ArrayList(new XPQuery<Department>(session).Where(o => (o.IsScope && !o.Invalid && o.Level <= 2)).OrderBy(o => o.Level).ThenBy(o => o.Order).Select(o=>o.Name).ToArray());
  6. //or:
  7. string UID = "LILIN";
  8. var list = new XPQuery<ApprovalRecord>(session).Where(o => (o.IsExecute==false && o.Actor.LoginName== UID));
  9. //or:
  10. var ar = session.Query<ApprovalRecord>() as IQueryable<ApprovalRecord>;
  11. ar = ar.Where(o => o.Actor.LoginName == User.Identity.Name && o.Type == "Scheme" && o.IsExecute);
  12. //3、查询
  13. PatentOwner = "3,4,8"
  14. var owner = PatentOwner?.Trim(',');
  15. var names = new XPCollection(Session, typeof(Department), CriteriaOperator.Parse($"Oid IN ({owner})"));
  16. //4、查询xpcList中不包含ActID=5的项
  17. var list = xpcList.Where(o => o.ActID == 5).ToList();
  18. xpcList = xpcList.Where(o => !list.Select(p => p.Oid).Contains(o.Oid)).ToList();

12.在控件中数据添加链接

  1. <dx:GridViewDataTextColumn VisibleIndex="0" Caption="审批内容">
  2. <DataItemTemplate>
  3. <asp:Literal runat="server" Text='<%#TechManager.Module.Engine.ApprovalHelper.GetUrl(session,Eval("Type"),Eval("TargetId"), Eval("Name")) %>'> </asp:Literal>
  4. </DataItemTemplate>
  5. </dx:GridViewDataTextColumn>
  6. //类
  7. public static string GetUrl(Session session,object objCode, object oId ,object name)
  8. {
  9. string code = objCode.ToString();
  10. int id = int.Parse(oId.ToString());
  11. string s = "<a href='{0}' target='_blank'>{1}</a>";
  12. string url = "", text = "";
  13. CriteriaOperator op = CriteriaOperator.Parse("Oid=?", id);
  14. try
  15. {
  16. switch (code)
  17. {
  18. case "Person":
  19. var person = session.FindObject<Person>(op);
  20. url = "../Persons/ViewAll.aspx?id=" + id;
  21. text = $"[{person.Department.FullName}]{person.Name}";
  22. break;
  23. case "PersonTest":
  24. var personT = session.FindObject<Person>(op);
  25. url = "../Test/PersonView.aspx?id=" + id;
  26. text = $"[{personT.Department.FullName}]{personT.Name}";
  27. break;
  28. }
  29. }catch
  30. {
  31. return "";
  32. }
  33. return string.Format(s, url, text);
  34. }

其中Eval();就是针对控件使用的;

13.未将对象引用设置到对象的实例

错误可能:是否为null;没有new初始化对象;

  1. protected ArrayList AppList = new ArrayList();
  2. string s = GetType(a.Key);
  3. AppList.Add(s);

如果不new AppList;只定义protected ArrayList AppList;将提示这个错误;

14.字符串中替换字符

  1. String s = abcat”;
  2. String s1 = s.replace(‘a’,‘1’);
  3. string sourceString = "Ni hao 123";
  4. sourceString.Replace("123", "abc");

replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);
replaceAll的参数是regex,即基于规则表达式的替换,比如:可以通过replaceAll("\d", "*")把一个字符串所有的数字字符都换成星号;

15.在建实体类时,定义主键,设置长度大小,不能重复,唯一但不是主键

[Key, DisplayName("登录名"), Size(20)]
public string LoginName;
[DisplayName("内容"), Size(int.MaxValue)]
public string Content;
[DisplayName("机构"),Indexed(Unique =true)]
public Department Department;

16.对实体类“列”定义下拉菜单

  1. [DisplayName("性别"), Size(2)]
  2. public string Sex;
  3. //
  4. <dx:GridViewDataComboBoxColumn FieldName="Sex" VisibleIndex="6">
  5. <PropertiesComboBox ClientInstanceName="Sex">
  6. <Items>
  7. <dx:ListEditItem Value="男" />
  8. <dx:ListEditItem Value="女" />
  9. </Items>
  10. <ValidationSettings>
  11. <RequiredField IsRequired="true" ErrorText="不能为空" />
  12. </ValidationSettings>
  13. </PropertiesComboBox>
  14. </dx:GridViewDataComboBoxColumn>

17.XPO对象类中字段运算后存入数据库中

第一种不存入数据库,临时运算

  1. [DisplayName("字段")]
  2. public double CoalNum;
  3. private double _SO2;
  4. [DisplayName("SO2"),NonPersistent]
  5. public double SO2 {
  6. get { return _SO2; }
  7. set {
  8. _SO2 = (CoalNum * 0.012 * 0.8 * 2);
  9. }
  10. }

第二种字段存入数据库;但是运算过后数据会延迟存入数据库

  1. [DisplayName("字段")]
  2. public double CoalNum;
  3. private double _SO2;
  4. [DisplayName("SO2")]
  5. public double SO2 {
  6. get { return _SO2; }
  7. set {
  8. _SO2 = (CoalNum * 0.012 * 0.8 * 2);
  9. }
  10. }

第三种都存入数据库

  1. [DisplayName("字段")]
  2. public double CoalNum;
  3. [DisplayName("SO2")]
  4. public double SO2;
  5. protected override void OnSaving(){
  6. SO2 = (CoalNum * 0.012 * 0.8 * 2);
  7. }

18.GridvView中 信息多行填写

  1. <dx:GridViewDataMemoColumn FieldName="ContentAbstract" VisibleIndex="16" Caption="研究内容提要" Visible="false">
  2. <PropertiesMemoEdit Rows="5" >
  3. </PropertiesMemoEdit>
  4. <EditFormSettings Visible="True" ColumnSpan="2" />
  5. </dx:GridViewDataMemoColumn>

19.dev 控件新建时;给实体类 属性/列 赋值 事件

  1. protected void gvAll_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
  2. {
  3. e.NewValues["Author"] = currentUser;
  4. e.NewValues["列名"] = DateTime.Now;
  5. }

20.给控件列添加颜色自定义

  1. //单列数据颜色HtmlDataCellPrepared
  2. protected void gvAll_HtmlDataCellPrepared(object sender,ASPxTreeList.TreeListHtmlDataCellEventArgs e)
  3. {
  4. if (e.Column.FieldName == "Order")
  5. e.Cell.BackColor = System.Drawing.Color.Red;
  6. }
  7. //正行数据颜色HtmlRowPrepared
  8. protected void gvAll_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
  9. {
  10. if (!gvAll.IsEditing && gvAll.EditingRowVisibleIndex < 0 && e.VisibleIndex >= 0 && e.RowType == GridViewRowType.Data)
  11. {
  12. var InspectDate = gvAll.GetRowValues(e.VisibleIndex, "InspectDate");
  13. var eq = gvAll.GetRow(e.VisibleIndex) as EquipmentMeasure;
  14. if (InspectDate != null && ((DateTime)InspectDate).Subtract(DateTime.Now).TotalDays < 30 && eq.IsFirstComplete && eq.EquipmentStatus == EquipmentStatus.Normal)
  15. e.Row.BackColor = System.Drawing.Color.Red;
  16. }
  17. }

21.CriteriaOperator查询字段为空时

要用 is null

  1. XpoDs.Criteria = CriteriaOperator.Parse("Parent is null").ToString();

22.Dev时间控件值改变时;把存储的值也清空/*工具栏中时间搜索全部条件清空时,时间控件值没有清空

  1. 方式一:
  2. function Clear() {
  3. txtName.value = "";
  4. txtStart_I.value = "";
  5. txtEnd_I.value = "";
  6. Aspx.ETextChanged("txtStart");
  7. Aspx.ETextChanged("txtEnd");
  8. btnSearch.click();
  9. }
  10. /***********/
  11. <div class="search">开始时间:<dx:ASPxDateEdit ID="txtStart" runat="server" ClientIDMode="Static"> </dx:ASPxDateEdit></div>
  12. <div class="search">结束时间:<dx:ASPxDateEdit ID="txtEnd" runat="server" ClientIDMode="Static"> </dx:ASPxDateEdit></div>

如果不加ClientIDMode="Static";在JS函数中该控件的ID “有可能” 就不是这个;可在网页检查中看该控件的 ID就知道了!

方式二:
function Clear() {
txtName.value = "";
txtStart.SetValue();
txtEnd.SetValue();
btnSearch.click();
}

*注意!!注意!!
ID.value ="";清空数据后;调用changed函数,括号里的id 跟上面用的ID 不一样!这个函数可以在网页检查里控件上看到;一个是网页端的ID,一个是控件在后台的id。

23.新建过后再编辑使编辑框不可编辑改动、编辑数据不为空

  1. protected void gvUsers_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
  2. {
  3. if (e.Column.FieldName == "Password")
  4. if (gvUsers.IsNewRowEditing)
  5. e.Editor.Visible = true;
  6. else
  7. e.Editor.Visible = false;
  8. else if (e.Column.FieldName == "LoginName")
  9. if (gvUsers.IsNewRowEditing)
  10. e.Editor.ReadOnly = false;
  11. else
  12. e.Editor.ReadOnly = true;
  13. else if (e.Column.FieldName == "Rank!Key")
  14. {
  15. if (gvUsers.IsNewRowEditing == true)
  16. XpoRank.Criteria = CriteriaOperator.Parse("Invalid=false").ToString();
  17. else
  18. {
  19. var rankid = gvUsers.GetRowValues(e.VisibleIndex, "Rank!Key");
  20. XpoRank.Criteria = CriteriaOperator.Parse("Oid=? or Invalid=false", rankid).ToString();
  21. }
  22. }
  23. }

下面的几个if是编辑时绑定已建好的数据;使其在编辑时不为空

24.获取编辑当前一行记录

  1. protected void gvFunds_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
  2. {
  3. //((ProjectContractModel)gvAll.GetRow(e.VisibleIndex)).Num//可行
  4. var s = ((sender as ASPxGridView).GetRow(e.VisibleIndex) as ProjectContractFunds).Subject;
  5. if (e.Column.FieldName == "Subject")
  6. e.Editor.ReadOnly = true;
  7. }

25.设置判断 字段唯一,且不是主键(身份证号)

  1. [DisplayName("序号"), NonPersistent]
  2. public int Num;
  3. [DisplayName("姓名")]
  4. public string Name;
  5. [DisplayName("性别"), Size(2)]
  6. public string Sex;
  7. [DisplayName("生日")]
  8. public DateTime Birthday;
  9. [DisplayName("出生地")]
  10. public string Birthplace;
  11. #if DEBUG
  12. [DisplayName("身份证号码"), Size(18)]
  13. #else
  14. [DisplayName("身份证号码"), Size(18),Indexed (Unique = true)]
  15. #endif
  16. public string IDNumber;
  17. [DisplayName("毕业院校")]
  18. public string School;

26.查询主表中记录,条件限制在子表,一对多

  1. op = CriteriaOperator.Parse("Department.Level=3 AND (Roles[Code=?].Count>0 )", r.Substring(2));
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注