ASP.NET Web開發(fā)框架之五 控件擴(kuò)展
當(dāng)在Web頁面中設(shè)計(jì)好表格布局之后,運(yùn)用Enterpris Solution提供的框架,以可視化的方式綁定數(shù)據(jù)。
首先,請?jiān)谂渲梦募刑砑尤缦碌某绦蚣?,以方便框架運(yùn)用反射找到當(dāng)前項(xiàng)目所引用的實(shí)體層。
然后打開Visual Studio,在設(shè)計(jì)時(shí)面板中,選擇一個(gè)控件。
如上圖所示,選擇控件的DataBindingString屬性,在屬性面板中打開它。如果已經(jīng)綁定了屬性,它會自動高亮顯示已經(jīng)綁定的實(shí)體及其屬性。這個(gè)屬性的代碼設(shè)計(jì),是這樣的
- [Category(CategoryName.OPTIONS)]
- [DefaultValue("")]
- [Description("Data Binding")]
- [Editor(typeof(QueryBindingTypeDialogEditor), typeof(UITypeEditor))]
- public virtual string DataBindingString
- {
- get
- {
- object obj = XState["DataBindingString"];
- return obj != null ? obj.ToString() : "";
- }
- set
- {
- XState["DataBindingString"] = value;
- }
- }
如代碼所示,它提供了一個(gè)自定義的屬性編輯器,也就是上圖中我們看到的Query Builder,綁定屬性。
返回所需要綁定屬性的關(guān)鍵代碼如下所示,它讀取實(shí)體層程序集并返回用戶所選擇的屬性值
- string path = "";
- IWebApplication webApp = (IWebApplication)provider.GetService(typeof(IWebApplication));
- Configuration config = webApp.OpenWebConfiguration(true);
- AppSettingsSection app = config.AppSettings;
- path = app.Settings["Assembly"].Value;
這幾句代碼的含義,從當(dāng)前Web項(xiàng)目中打開Web.config配置文件,并找到文章開頭設(shè)置的實(shí)體層程序集。
代碼生成
基于模板的代碼生成器,例如Code Smith,給代碼生成帶來了極大的便利。Enterprise Solution相關(guān)的代碼生成,均以Code Smith模板完成。熟悉ASP.NET的語法,應(yīng)該可以很快熟悉Code Smith的語法并對它的生產(chǎn)力感到滿意。
在最新版本的Code Smith 6.5中,支持.NET 3.x/4.0語法。還可以運(yùn)用它的SDK,把代碼生成功能集成到自己的開發(fā)工具中。比如,我想制作一個(gè)批量代碼生成的工具,直接調(diào)用Code Smith的模板文件:
- public void RunTemplate(string templateFile,string connectionString,string tableName,string targetFile)
- {
- CodeTemplateCompiler compiler = new CodeTemplateCompiler(templateFile);
- compiler.Compile();
- if (compiler.Errors.Count == 0)
- {
- CodeTemplate template = compiler.CreateInstance();
- DatabaseSchema database = new DatabaseSchema(new SqlSchemaProvider(), connectionString);
- TableSchema tableSchema = database.Tables[tableName];
- //如果都是字符串,應(yīng)該要考慮bool,int,object
- Dictionary<string, object> dic = new Dictionary<string, object>();
- string[] paramterValues = rtfParameter.Lines;
- foreach (string parm in paramterValues)
- {
- if (!String.IsNullOrEmpty(parm))
- {
- string[] values = Regex.Split(parm, "=");
- string key = values[0];
- object para =values[1].ToString().Trim();
- if (string.IsNullOrEmpty(values[1]))
- para = tableSchema;
- dic.Add(values[0], para);
- }
- }
- PropertyInfo[] pis = template.GetProperties();
- foreach (PropertyInfo pi in pis)
- {
- object val=null;
- if(dic.TryGetValue(pi.Name,out val))
- template.SetProperty(pi, val);
- }
- if(File.Exists(targetFile))
- File.Delete(targetFile);
- using (StreamWriter writer = new StreamWriter(targetFile))
- {
- template.Render(writer);
- writer.Flush();
- }
- }
- }
這個(gè)方法可以為你的代碼生成帶來諸多方便。比如以下面的參數(shù)調(diào)用之,則可以產(chǎn)生多個(gè)代碼文件
- RunTempate("Interface.cst","server=(local);uid=sa;pwd=holiday;database=TS","Employee","IEmployeeManager.cs)
- RunTempate("Manager.cst","server=(local);uid=sa;pwd=holiday;database=TS","Employee","EmployeeManager.cs);
每行產(chǎn)生一個(gè)代碼文件,接口層和數(shù)據(jù)訪問層立即產(chǎn)生完成。
數(shù)據(jù)字典
對于不變的數(shù)據(jù)字典,比如帳戶的借方和貸方,程序員每天的工作分類項(xiàng):Bug或Enhacement。對于這類不變的數(shù)據(jù)字典,Enterprise Solution以下面的方式處理它們。
先來看界面層的代碼,看看最終的開發(fā)人員,需要以何種方式應(yīng)用數(shù)據(jù)字典。以客戶組別為例子
- <ext:DropDownList ID="ddlCustomerGroup" Label="Customer Group" runat="server"> </ext:DropDownList>
后臺代碼如下所示,也相當(dāng)?shù)暮喚?/p>
- ddlCustomerGroup.InitializeValueListFromEnum(typeof(CustomeGroup));
在此,我并沒有直接擴(kuò)展DropDownList的代碼,而是加入擴(kuò)展方法,它的實(shí)現(xiàn)方法如下所示
- public static class DataBindingHelper
- {
- public static void InitializeValueListFromEnum(this ExtAspNet.DropDownList dropDownList, Type enumType)
- {
- dropDownList.Items.Clear();
- foreach (KeyValuePair<string, string> item in StringEnum.GetValueStringList(enumType))
- {
- ExtAspNet.ListItem listitem = new ExtAspNet.ListItem(item.Value, item.Key);
- dropDownList.Items.Add(listitem);
- }
- }
代碼的意圖相對簡單,依據(jù)參數(shù)的枚舉值分別產(chǎn)生ListItem達(dá)到生成數(shù)據(jù)字典項(xiàng)的目的。枚舉CustomerGroup的定義
- public enum CustomeGroup
- {
- [StringValue("Partner")]
- [DisplayText("Partner")]
- Partner,
- [StringValue("Supplier")]
- [DisplayText("Supplier")]
- Supplier
- }
StringValue和DisplayText分別用于ListItem的ValueItem和TextItem,用于顯示和實(shí)際的值。
對于需要從數(shù)據(jù)庫中取出的數(shù)據(jù)字典,Enterprise Solution框架暫未提供很方便的方法來完成綁定。依照基本的方法,讀取字段值,綁定到DropDownList中,過程也不復(fù)雜。
ExtAspNet控件擴(kuò)展
ExtAspNet是一套優(yōu)秀的控件,用它可以快速開發(fā)基于ExtJs的應(yīng)用。雖然功能相當(dāng)完善,但在細(xì)微處,仍需要稍微做一些調(diào)整,以滿足框架的應(yīng)用需要。
每一個(gè)需要綁定屬性的控件,均加入了下面的屬性,以方便綁定實(shí)體的屬性。
- [Category(CategoryName.OPTIONS)]
- [DefaultValue("")]
- [Description("Data Binding")]
- [Editor(typeof(QueryBindingTypeDialogEditor), typeof(UITypeEditor))]
- public virtual string DataBindingString
- {
- get
- {
- object obj = XState["DataBindingString"];
- return obj != null ? obj.ToString() : "";
- }
- set
- {
- XState["DataBindingString"] = value;
- }
- }
其次,我需要TextBox的文本標(biāo)簽是右對齊的,而不是左對齊,于是加入了下面的屬性。
- //fieldLabel居右對齊的問題
- OB.AddProperty("labelStyle", "text-align:right");
對于頁面中的每個(gè)選項(xiàng)卡頁面,我需要在它關(guān)閉時(shí),彈出提示確認(rèn)窗口,于是修改代碼
- NODES.mainTabStrip.addTab({
- 'id': tabID,
- 'url': url,
- 'title': title,
- 'closable': true,
- listeners: {
- 'beforeclose': conrirmTab
- }
- });
加了一個(gè)beforeclose確認(rèn),它的方法如下所示
- function conrirmTab(e) {
- Ext.MessageBox.show({
- title: 'Confirm',
- msg: 'Are you sure want to close <b>' + e.title + '</b> ?',
- buttons: Ext.MessageBox.YESNO,
- icon: Ext.MessageBox.QUESTION,
- fn: function (btn, text) {
- if (btn == 'yes') {
- NODES.mainTabStrip.remove(e);
- }
- }
- });
- return false;
- }
更精確的,我們應(yīng)該加在那些有數(shù)據(jù)修改的頁面,如果頁面被修改過,則彈出確認(rèn)提示,否則可以直接關(guān)閉選項(xiàng)卡。
再來看看兩個(gè)比較實(shí)用的腳本,關(guān)閉當(dāng)前選項(xiàng)卡和關(guān)閉所有選項(xiàng)卡
- //關(guān)閉活動的選項(xiàng)卡
- function closeActivePanel() {
- var tabpanel = Ext.getCmp('<%= mainTabStrip.ClientID %>');
- var _activeTab = tabpanel.getActiveTab();
- if (_activeTab.x_iframe_url == "/Management/admin/default.aspx")
- return;
- tabpanel.remove(_activeTab);
- }
- //關(guān)閉所有選項(xiàng)卡
- function closeAllPanel(){
- var tabpanel = Ext.getCmp('<%= mainTabStrip.ClientID %>');
- var _activeTab = undefined;
- for(var i=0;i< tabpanel.items.length;i++)
- {
- _activeTab = tabpanel.items.items[i];
- if (_activeTab.x_iframe_url!=undefined && _activeTab.x_iframe_url == "/Management/admin/default.aspx")
- continue;
- tabpanel.remove(_activeTab);
- i = 0;
- }
- tabpanel.doLayout();
- }
如果需要在后臺代碼中調(diào)用,可以這樣調(diào)用之
- string tab = "closeAllPanel();";
- PageContext.RegisterStartupScript(tab);
還有一些控制項(xiàng),有的還未找到實(shí)現(xiàn)方法,一并列舉如下
1 控制TextBox的輸入長度。比如maxLength=6,只允許輸入6個(gè)字符,第7個(gè)字符無法輸入。當(dāng)前控件仍然可以輸入,只是會有驗(yàn)證提示錯誤。
2 只允許大寫字母輸入。有的控件,比如用戶名,在C#.NET里面,MIS和mis的字母相同,但是比較equal時(shí),如果不設(shè)置忽略大小寫,則它們的值不等,但是在數(shù)據(jù)庫里面,它們是相同的值。為避免這種情況,我需要設(shè)置控件只允許輸入大寫字母,用戶如果輸入的是小寫,則轉(zhuǎn)化自動為大寫。
3 對于Tab.cs #267行,這一行代碼會有異常。如果前臺JS打開多個(gè)選項(xiàng)卡,在頁面刷新時(shí),會拋出IndexOutOfRange異常,它的代碼如下所示
- // 對于非激活Tab,其中的Iframe需要延遲加載
- if (this != tabStrip.Tabs[tabStrip.ActiveTabIndex])
- {
- OB.RemoveProperty("html");
- OB.RemoveProperty("x_iframe_loaded");
- OB.AddProperty("x_iframe_loaded", false);
- }
ActiveTabIndex的值是2或3,但是當(dāng)前只有一個(gè)tab選項(xiàng)卡,于是拋出索引越界異常。
4 PageRefresh時(shí),會關(guān)閉前臺頁面打開的選項(xiàng)卡。需要用cookie記住已經(jīng)打開的選項(xiàng)卡,在PageRefresh后,再打開被迫關(guān)閉的選項(xiàng)卡,以減少用戶困擾。
5 對ascx用戶控件的支持。新版本的ExtAspNet已經(jīng)內(nèi)置了此功能。如果仍使用的是3.1.8版本,需要這樣
- foreach (UserGroupMenuTypeEntity usermenuType in menu.UserGroupMenuTypes)
- {
- function c = LoadControl("function.ascx") as function;
- c.LineBreakPlaceHolder.Text =GetTranslation(usermenuType.Description.Replace("&", ""));
- c.DataList1.DataSource = usermenuType.UserGroupMenuDetails;
- c.DataList1.DataBind();
- ph.Controls.Add(c);
- Literal liter = new Literal();
- liter.Text = "<br />";
- ph.Controls.Add(liter);
- }
加載用戶自定義控件。因?yàn)閃eb不同于WinForms,Web的類型定義并不包含資源,圖片,控件,還必須引用頁面ASPX/ASCX文件,C/S類型的繼承則包含類型的方法,及其界面控件,相對方便很多。
6 Excel導(dǎo)入導(dǎo)出的支持。官方的例子中已經(jīng)包含EXCEL導(dǎo)出功能,但沒有導(dǎo)入功能,仍然需要提供Web方式下的EXCEL數(shù)據(jù)導(dǎo)入功能。
原文鏈接:http://www.cnblogs.com/JamesLi2015/archive/2012/09/26/2703361.html
【編輯推薦】