SplendidCRM系统通常是先从SQL中取出要用的数据,存入系统的cache中以备调用,主要是为了减少反复读数据库导致的负载。
现在,我要创建一个名为Accounts的DropdownList,并将Accounts模块的vmAccounts视图中的NAME数据绑定到这个DropdownList,步骤如下:
1. 在\Administration\DynamicLayout\_code\NewRecord.cs的Page_Load方法中的string[] arrCachedLists = new string[]数组中添加一个新的字段值,
private void Page_Load(object sender, System.EventArgs e) { if ( !this.IsPostBack || lstLIST_NAME.Items.Count == 0 ) { try { DbProviderFactory dbf = DbProviderFactories.GetFactory(); using ( IDbConnection con = dbf.CreateConnection() ) { con.Open(); string sSQL; sSQL = "select LIST_NAME " + ControlChars.CrLf + " from vwTERMINOLOGY_PickList" + ControlChars.CrLf + " order by LIST_NAME " + ControlChars.CrLf; using ( IDbCommand cmd = con.CreateCommand() ) { cmd.CommandText = sSQL; using ( DbDataAdapter da = dbf.CreateDataAdapter() ) { ((IDbDataAdapter)da).SelectCommand = cmd; DataTable dt = new DataTable(); da.Fill(dt);
string[] arrCachedLists = new string[] { "" , "AssignedUser" , "Currencies" , "Release" , "Manufacturers" , "Shippers" , "ProductTypes" , "ProductCategories" , "ContractTypes" , "ForumTopics" , "Accounts" //加入模块名accounts到arrCachedLists数组 }; for ( int i = 0; i < arrCachedLists.Length; i++ ) { DataRow row = dt.NewRow(); row["LIST_NAME"] = arrCachedLists[i]; dt.Rows.InsertAt(row, i); }
//绑定dt table的数据dropdownlist类型的lstLIST_NAME lstLIST_NAME.DataSource = dt.DefaultView;
lstLIST_NAME.DataBind(); } } } lstFIELD_TYPE_Changed(null, null); } catch(Exception ex) { SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex); lblError.Text = ex.Message; } } }
2. 在_code\SplendidCache.cs中添加一个方法,用来将vmAccounts视图中的NAME数据存入CACHE, 代码如下:
public static DataTable Accounts() { System.Web.Caching.Cache Cache = HttpRuntime.Cache; DataTable dt = Cache.Get("vwACCOUNTS") as DataTable; if (dt == null) { try { DbProviderFactory dbf = DbProviderFactories.GetFactory(); using (IDbConnection con = dbf.CreateConnection()) { con.Open(); string sSQL; sSQL = "select ID " + ControlChars.CrLf + " , NAME " + ControlChars.CrLf + " from vwACCOUNTS" + ControlChars.CrLf + " order by DATE_ENTERED " + ControlChars.CrLf; using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = sSQL; using (DbDataAdapter da = dbf.CreateDataAdapter()) { ((IDbDataAdapter)da).SelectCommand = cmd; dt = new DataTable(); da.Fill(dt); Cache.Insert("vwACCOUNTS", dt, null, DefaultCacheExpiration(), Cache.NoSlidingExpiration); } } } } catch (Exception ex) { SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex); // 10/16/2005 Paul. Ignore list errors. } } return dt; }
代码截图:
3.在 public class SplendidCache 的CustomCaches 数组中加入一行Accounts的语句:
, new SplendidCacheReference("Accounts" , "ID" , "NAME" , new SplendidCacheCallback(SplendidCache.Accounts ))
代码截图:
4. 在Admin页面中将Books的EditView, DetailView和ListView中的Book_Concern都从原来的Textbox改为ListBox,使用新创建的Accounts Dropdownlist作为Book_Concern的下拉列表。
5. 打开新建BOOK的页面(editview),看到Book_Concern的下拉列表的数据已经成功的绑定到了Accounts模块。
Book_EditView:
Book_ListView:
Book_ListView:(GridView)
搞定,收工!