1.在_controls目录下新建一个web user control,名为PriceRange.ascx, 用于搜索某一个价格范围内的记录。
<%@ Control Language="C#" AutoEventWireup="False" CodeBehind="PriceRange.ascx.cs" Inherits="SplendidCRM._controls.PriceRange" %> <asp:Table ID="Table1" BorderWidth="0" CellPadding="0" CellSpacing="0" runat="server"> <asp:TableRow> <asp:TableCell> <table id="tblPriceRange" runat="server"> <tr> <td>Greater_than</td> <td><asp:TextBox ID="From_Price" runat="server"></asp:TextBox></td> </tr> <tr> <td>Less_than</td> <td><asp:TextBox ID="To_Price" runat="server"></asp:TextBox></td> </tr> </table> </asp:TableCell> </asp:TableRow> </asp:Table>
注意:设置AutoEventWireup="False" ,和其他页面保持一致,在cs中用#region Web Form Designer generated code代码段调用页事件
关于AutoEventWireup="False" 的详解:http://www.cnblogs.com/qiantuwuliang/archive/2009/07/04/1517013.html
http://msdn.microsoft.com/zh-cn/library/system.web.configuration.pagessection.autoeventwireup.aspx
设计样式如下图:
2. PriceRange.ascx.cs的全部代码内容:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace SplendidCRM._controls { public partial class PriceRange : SplendidControl { protected void Page_Load(object sender, EventArgs e) { } private bool bUIRequired; public bool UIRequired { get { return bUIRequired; } set { bUIRequired = value; } } public Unit Width //初始宽度100,接受设定值 { get { return From_Price.Width;} set { if (value.Value <= 0) { From_Price.Width = 100; To_Price.Width = 100; } else { From_Price.Width = value; To_Price.Width = value; } } } public short TabIndex { get { return From_Price.TabIndex;} set { From_Price.TabIndex = value; To_Price.TabIndex = value; } } public string FromPrice { get { return From_Price.Text; } set { To_Price.Text = value; } } public string ToPrice { get { return To_Price.Text; } set { To_Price.Text = value; } } public string FromPriceID { get { return From_Price.ID; } set { To_Price.ID = value; } } public string ToPriceID { get { return To_Price.ID; } set { To_Price.ID = value; } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
3. 在 SplendidCRM.Administration.DynamicLayout.EditViews.NewRecord.ascx页面代码的
<asp:DropDownList ID="lstFIELD_TYPE" OnSelectedIndexChanged="lstFIELD_TYPE_Changed" AutoPostBack="true" Runat="server">
中添加一个ListItem:
<asp:ListItem Value="PriceRange" >PriceRange</asp:ListItem>
然后在在 SplendidCRM.Administration.DynamicLayout.EditViews.NewRecord.ascx.cs的
protected override void lstFIELD_TYPE_Changed(Object sender, EventArgs e)
方法中添加一个case语句:
case "PriceRange": spnDATA.Visible = true; spnDATA_LABEL.Visible = true; spnREQUIRED.Visible = true; spnCHANGE.Visible = false; spnTEXT1.Visible = false; spnTEXT2.Visible = true; spnTEXT3.Visible = false; spnFORMAT_ROWS.Visible = false; spnLIST_NAME.Visible = false; spnGENERAL.Visible = true; break;
此时在管理页面中的FiledType下拉列表中已经可以看到PriceRange控件,但它现在还不能用,还需要做后续的工作才能让它正常工作起来。
4. 在_Code/SplendidDynamic.cs的
public static void AppendEditViewFields(DataView dvFields, HtmlTable tbl, IDataReader rdr, L10N L10n, TimeZone T10n, CommandEventHandler Page_Command, bool bLayoutMode)
方法中的if ~else if的sFIELD_TYPE的判断区域添加以下代码:
else if (String.Compare(sFIELD_TYPE, "PriceRange", true) == 0) { PriceRange ctlInput = tbl.Page.LoadControl("~/_controls/PriceRange.ascx") as PriceRange; ctlInput.Width = nFORMAT_SIZE; ctlInput.ID = sDATA_FIELD; ctlInput.TabIndex = nFORMAT_TAB_INDEX; ctlInput.UIRequired = bUI_REQUIRED; tdField.Controls.Add(ctlInput); try { if (rdr !=null) { ctlInput.FromPrice = Sql.ToString(rdr[sDATA_FIELD]); ctlInput.ToPrice = Sql.ToString(rdr[sDATA_FIELD]); } } catch (Exception ex) { SplendidError.SystemWarning(new StackTrace(true).GetFrame(0), ex); } if (bLayoutMode) { Literal litField = new Literal(); litField.Text = sDATA_FIELD; tdField.Controls.Add(litField); } }
6. 现在重新登录系统,reload一次,进入admin中的layout manager,在Select Edit View中选择Books.SearchBasic视图,然后将刚才制作的PriceRange用户控件添加到此视图的页面中。
现在回到Book模块default页面,就能看到搜索栏中多了名为Book_Price:的PriceRange用户控件了,但是它还只是个摆设,当你点击search button时并不会按照你的价格范围进行搜索,因为我还没有在SearchView用户控件中给它编写SQL方法。OK,下面继续…
7. 继续劳动^_* ,打开_Control/SearchView.ascx.cs文件,在 public virtual void SqlSearchClause(IDbCommand cmd) 方法中参照类似的DateRange用户控件的代码段写入以下代码:
else if (sFIELD_TYPE == "PriceRange") { PriceRange ctlPrice = FindControl(sDATA_FIELD) as PriceRange; //PriceRange ctlToPrice = FindControl("To_Price") as PriceRange; float fromPrice = 0; float toPrice = 0; if (ctlPrice != null) { if (!Sql.IsEmptyString(ctlPrice.FromPrice)) { fromPrice =Sql.ToFloat(ctlPrice.FromPrice); } if (!Sql.IsEmptyString(ctlPrice.ToPrice)) { toPrice = Sql.ToFloat(ctlPrice.ToPrice); } } if (fromPrice != 0 || toPrice != 0) Sql.AppendParameter(cmd, fromPrice, toPrice, sDATA_FIELD); //需要在sql.cs中构建方法签名与此调用相符的AppendParameter方法 }
8.在_code/sql.cs中构建一个签名为public static void AppendParameter(IDbCommand cmd, float fValue1, float fValue2, string sField) 的方法:
public static void AppendParameter(IDbCommand cmd, float fValue1, float fValue2, string sField) { if (fValue1 != 0) { cmd.CommandText += " and " + sField + " >= @" + sField +"_From" + ControlChars.CrLf; //cmd.Parameters.Add("@" + sField, DbType.DateTime, 8).Value = dtValue; Sql.AddParameter(cmd, "@" + sField + "_From", fValue1); } if (fValue2 != 0) { cmd.CommandText += " and " + sField + " <= @" + sField + "_To" + ControlChars.CrLf; //cmd.Parameters.Add("@" + sField, DbType.DateTime, 8).Value = dtValue; Sql.AddParameter(cmd, "@" + sField + "_To", fValue2); } }
编译一下,去Books页面的搜索栏试试吧,哈哈,按照价格范围搜索成功了!