SharePoint 2010自帶字段類型有很多,如單行文本,日期時間,下拉列表,數字等等。但往往這些不能滿足我們的需要,比如要求一個大寫金額的字段,用戶輸入數字,要求顯示成大寫,這時候就不能滿足需求了。那么我們就要使用自定義開發的字段類型了。下面以開發大寫金額字段來說明SharePoint 2010自定義字段開發。
SharePoint 2010創建欄時默認的字段類型如下:
開發自定義的大寫金額類型后,也會在這里出現,如下圖
先看一下效果吧,首先創建一個數字字段,即是用戶輸入數字的字段,點擊確定。
然后再創建一個我們開發的大寫金額字段
創建完這兩個字段后,在列表上點擊添加新項目
當在小寫文本框內輸入數字,大寫金額會自動跟着顯示出來,如下圖
看到效果后,下面介紹大寫金額字段下實現過程。
1、先了解MOSS內部的字段類型,如下表,大寫金額字段繼承 SPFieldMultiColumn這字段類型開發。
SPFieldText |
單行文本 |
這個可能是用的最為廣泛的字段類型了,它的輸入界面就是一個單行文本框,沒有數據驗證功能(除了是否為空)。可以設置最大長度(局限在255以內)。 |
SPFieldMultiLineText |
多行文本 |
輸入界面是一個textarea,根據設置不同,可以是純文本或者是帶格式文本的(按照html格式保存的)。 |
SPFieldNumber |
數字 |
輸入界面是textbox,但是帶有數據驗證(是否為數字,以及最大/最小值等)。 |
SPFieldCurrency |
貨幣 |
和數字其實差不多,只不過現實的時候會多一個貨幣符號。 |
SPFieldBoolean |
是/否 |
一個CheckBox |
SPFieldDateTime |
日期 |
一個帶picker的textbox,可以選擇“日期和時間”或“僅日期” |
SPFieldChoice |
選項(單選) |
可以以dropdownlist或者radio button的形式出現。這個字段有點點特別,雖然它看上去只能存一個值,但其實它是多選類(SPFieldMultiChoice)的子類 |
SPFieldMultiChoice |
選項(多選) |
如果使用多選,那么是通過一組checkbox輸入的。在這個類里面定義了這個字段中究竟有哪些選項(通過Choices屬性,自然,作為它子類的SPFieldChoice也有這個屬性)。於之相對應的,可以通過SPFieldMultiChoiceValue類來訪問它的值。 |
SPFieldRatingScale |
評估范圍 |
剛才介紹過了,它其實也是多選類(SPFieldMultiChoice)的子類。於之對應的值類型為SPFieldRatingScaleValue |
SPFieldUrl |
鏈接或圖片 |
可以是鏈接,也可以是圖片,它包含url和描述信息兩個部分,通過其值類型SPFieldUrlValue可以很方便的得到這兩部分。 |
SPFieldLookup |
查閱項 |
通過dropdownlist完成單選,一個特殊的listbox完成多選(wss3.0支持查閱項多選了!),由於每個被查閱的項會有id和文本,所以也需要有值類型,這個比較特殊,有兩種值類型,SPFieldLookupValue和SPFieldLookupValueCollection(因為支持多選了嘛)。然后在SPFieldLookup類中,定義了要查閱哪個列表的哪個字段,以及是哪個網站上的列表。是的!wss3.0中的查閱項其實是支持跨網站查閱的(通過設定LookupWebId屬性),但是在默認的界面上並沒有暴露出一點。所以一個跨網站查閱項是一個很值得一做的自定義字段類型! |
SPFieldUser |
用戶和用戶組 |
它的輸入是通過一個帶有AJAX支持的輸入框完成的,這是一個很強大的控件。其實這個類是SPFieldLookup的子類,因為它們做的事情在本質上都差不多。相應的,其值類型SPFieldUserValue也是SPFieldLookupValue的子類,還有SPFieldUserValueCollection…… |
SPFieldMultiColumn |
多欄 |
這是另一個很特殊的字段類型,默認情況下我們無法直接使用它,使用它的唯一途徑就是通過自定義字段類型繼承它來完成我們的需求。顧名思義,這是一個能在一個字段中儲存多個信息的字段類型。 |
大寫金額字段項目目錄情況
SPLegalAmountField.cs代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Xml; using System.Reflection; using Microsoft.SharePoint.Security; using System.Security.Permissions; using System.Text.RegularExpressions; using System.Collections.Specialized; namespace FlowMan.WebControls.SPLegalAmountField { [CLSCompliant(false)] public class SPLegalAmountField : SPFieldMultiColumn { //創建字段保存創建配置屬性:關聯的小寫字段 const string SPLegalAmountField_RELEVANCELISTFIELD = "SPLegalAmountFieldRelevanceListField"; //創建字段保存創建配置屬性:呈現出來的大小金額文本框長度 const string SPLegalAmountField_TEXTBOXWIDTH = "SPLegalAmountFieldTextboxWidth"; public SPLegalAmountField(SPFieldCollection fields, string fieldname) : base(fields, fieldname) { _splegalAmountFieldRelevanceListField = "" + base.GetCustomProperty(SPLegalAmountField_RELEVANCELISTFIELD); _splegalAmountFieldTextboxWidth = "" + base.GetCustomProperty(SPLegalAmountField_TEXTBOXWIDTH); } public SPLegalAmountField(SPFieldCollection fields, string typeName, string displaydname) : base(fields, typeName, displaydname) { _splegalAmountFieldRelevanceListField = "" + base.GetCustomProperty(SPLegalAmountField_RELEVANCELISTFIELD); _splegalAmountFieldTextboxWidth = "" + base.GetCustomProperty(SPLegalAmountField_TEXTBOXWIDTH); } public override object GetFieldValue(string value) { if (string.IsNullOrEmpty(value)) return null; return new SPLegalAmountFieldValue(value); } private string _splegalAmountFieldRelevanceListField; private string _splegalAmountFieldTextboxWidth; public string SPLegalAmountFieldRelevanceListField { get { return _splegalAmountFieldRelevanceListField; } set { _splegalAmountFieldRelevanceListField = value; this.SetCustomPropertytoCache(SPLegalAmountField_RELEVANCELISTFIELD, value); } } public string SPLegalAmountFieldTextboxWidth { get { return _splegalAmountFieldTextboxWidth; } set { _splegalAmountFieldTextboxWidth = value; this.SetCustomPropertytoCache(SPLegalAmountField_TEXTBOXWIDTH, value); } } private static readonly Dictionary<string, StringDictionary> CustomPropertiesCache = new Dictionary<string, StringDictionary>(); private string ContextKey { get { return this.ParentList.ID.ToString() + "_" + System.Web.HttpContext.Current.GetHashCode(); } } protected void SetCustomPropertytoCache(string key, string value) { StringDictionary plist = null; if (CustomPropertiesCache.ContainsKey(ContextKey)) { plist = CustomPropertiesCache[ContextKey]; } else { plist = new StringDictionary(); CustomPropertiesCache.Add(ContextKey, plist); } if (plist.ContainsKey(key)) { plist[key] = value; } else { plist.Add(key, value); } } protected string GetCustomPropertyFromCache(string key) { if (CustomPropertiesCache.ContainsKey(ContextKey)) { StringDictionary plist = CustomPropertiesCache[ContextKey]; if (plist.ContainsKey(key)) return plist[key]; else return ""; } else { return ""; } } public override void OnAdded(SPAddFieldOptions op) { base.OnAdded(op); Update(); } public override void Update() { base.SetCustomProperty(SPLegalAmountField_RELEVANCELISTFIELD, this.GetCustomPropertyFromCache(SPLegalAmountField_RELEVANCELISTFIELD)); base.SetCustomProperty(SPLegalAmountField_TEXTBOXWIDTH, this.GetCustomPropertyFromCache(SPLegalAmountField_TEXTBOXWIDTH)); base.Update(); } public override BaseFieldControl FieldRenderingControl { [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)] get { BaseFieldControl _renderingControl = new SPLegalAmountFieldControl(); _renderingControl.FieldName = InternalName; return _renderingControl; } } /// <summary> /// 提交表單時候的驗證數據類型 /// </summary> /// <param name="value"></param> /// <returns></returns> public override string GetValidatedString(object value) { string strValue = "" + value; if (Required && strValue == "") { throw new SPFieldValidationException(System.Web.HttpContext.GetGlobalResourceObject("FlowMan.WebControls", "SPLegalAmountField_Required").ToString()); } return base.GetValidatedString(value); } } }
SPLegalAmountFieldValue.cs類代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Xml; using System.Reflection; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.Security; using System.Security.Permissions; namespace FlowMan.WebControls.SPLegalAmountField { public class SPLegalAmountFieldValue : SPFieldMultiColumnValue { private const int numberOfFields = 2; public SPLegalAmountFieldValue() : base(numberOfFields) { } public SPLegalAmountFieldValue(string value) : base(value) { } public string AmountCapital { get { if (this != null && this.Count > 0) return this[0]; else return ""; } set { if (value != null) this[0] = value; else this[0] = ""; } } public string AmountNumber { get { if (this != null && this.Count > 1) return this[1]; else return ""; } set { if (value != null) this[1] = value; else this[1] = ""; } } } }
SPLegalAmountFieldEditor.ascx 代碼
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %> <%@ Import Namespace="Microsoft.SharePoint" %> <%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SPLegalAmountFieldEditor.ascx.cs" Inherits="FlowMan.WebControls.SPLegalAmountField.SPLegalAmountFieldEditor" %> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td> <b> <asp:Literal runat="server" ID="Literal2" Text="<%$Resources:FlowMan.WebControls,SPLegalAmountField_EditorField%>"></asp:Literal>:</b> </td> <td> <asp:DropDownList ID="DrRelevanceListField" runat="server"> </asp:DropDownList> </td> </tr> <tr> <td> <b> <asp:Literal ID="Literal1" runat="server" Text="<%$Resources:FlowMan.WebControls,SPLegalAmountField_EditorFieldTextboxWidth%>"></asp:Literal>:</b> </td> <td> <asp:TextBox ID="txtTextboxWidth" runat="server"></asp:TextBox> </td> </tr> </table>
SPLegalAmountFieldEditor.cs代碼
using System; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint; using System.Web; using System.Collections.Generic; using System.Text; using System.Collections.Specialized; using Microsoft.SharePoint.Utilities; namespace FlowMan.WebControls.SPLegalAmountField { using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Web.UI.HtmlControls; public class SPLegalAmountFieldEditor : UserControl, IFieldEditor { //定義編輯字段的兩個控件 protected DropDownList DrRelevanceListField = null; protected TextBox txtTextboxWidth = null; bool IFieldEditor.DisplayAsNewSection { get { return false; } } //編輯字段配置信息綁定到控件上 void IFieldEditor.InitializeWithField(SPField field) { if (!Page.IsPostBack) { this.EnsureChildControls(); BindSPListFieldData(SPContext.Current.ListId, DrRelevanceListField); } if (!Page.IsPostBack&&field != null) { SPLegalAmountField fields = (SPLegalAmountField)field; this.DrRelevanceListField.SelectedValue = fields.SPLegalAmountFieldRelevanceListField; this.txtTextboxWidth.Text = fields.SPLegalAmountFieldTextboxWidth; } } //綁定當前列表的數字字段到下拉控件 void BindSPListFieldData(Guid listid,DropDownList drlist) { using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID)) { using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID)) { drlist.Items.Clear(); SPList splist = web.Lists[listid]; SPFieldCollection splistfield = splist.Fields; foreach (SPField spfsitem in splistfield) { if (spfsitem.Reorderable) { if (spfsitem.Type == SPFieldType.Number || spfsitem.Type == SPFieldType.Currency) { string _text = spfsitem.Title; string _value = spfsitem.InternalName; System.Web.UI.WebControls.ListItem litem = new System.Web.UI.WebControls.ListItem(_text, _value); drlist.Items.Add(litem); } } } } } } //保存配置的值 void IFieldEditor.OnSaveChange(SPField field, bool isNewField) { this.EnsureChildControls(); if (field != null) { SPLegalAmountField CuserField = (SPLegalAmountField)field; CuserField.SPLegalAmountFieldRelevanceListField = DrRelevanceListField.SelectedValue; CuserField.SPLegalAmountFieldTextboxWidth = txtTextboxWidth.Text; } } } }
SPLegalAmountFieldControl.ascx 代碼
<%@ Control Language="C#" Debug="true" %> <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <SharePoint:RenderingTemplate ID="SPLegalAmountFieldControl" runat="server"> <Template> <span id="spSPLegalAmountFieldControl" > <asp:TextBox ID="txtSPLegalAmountField" class="txtSPLegalAmountField" onfocus="this.blur()" runat="server" ></asp:TextBox> <asp:HiddenField ID="hidSPLegalAmountField" Value="" runat="server" /> <asp:HiddenField ID="hidSPLegalAmountFieldPropery" Value="" runat="server" /> </span> <script type="text/javascript"> function convert(str, inputmoneycontrol) { // if (str != "") { value = AmountInWords(str,4); //調用自定義函數轉換 return value; // } // else return "零元整"; } function AmountInWords(dValue, maxDec) { // 驗證輸入金額數值或數值字符串: dValue = dValue.toString().replace(/,/g, ""); dValue = dValue.replace(/^0+/, ""); // 金額數值轉字符、移除逗號、移除前導零 if (dValue == "") { return "零元整"; } // (錯誤:金額為空!) else if (isNaN(dValue)) { return '<asp:Literal ID="Literal2" Text="<%$Resources:FlowMan.WebControls,SPLegalAmountField_AlertInputLawful%>" runat="server"></asp:Literal>'; } var minus = ""; // 負數的符號“-”的大寫:“負”字。可自定義字符,如“(負)”。 var CN_SYMBOL = ""; // 幣種名稱(如“人民幣”,默認空) if (dValue.length > 1) { if (dValue.indexOf('-') == 0) { dValue = dValue.replace("-", ""); minus = "負"; } // 處理負數符號“-” if (dValue.indexOf('+') == 0) { dValue = dValue.replace("+", ""); } // 處理前導正數符號“+”(無實際意義) } var vInt = "", vDec = ""; // 字符串:金額的整數部分、小數部分 var resAIW; // 字符串:要輸出的結果 var parts; // 數組(整數部分.小數部分),length=1時則僅為整數。 var digits, radices, bigRadices, decimals; //數組:數字(0~9——零~玖);基(十進制記數系統中每個數字位的基是10——拾,佰,仟);大基(萬,億,兆,京,垓,杼,穰,溝,澗,正);輔幣(元以下,角/分/厘/毫/絲)。 var zeroCount; // 零計數 var i, p, d; // 循環因子;前一位數字;當前位數字。 var quotient, modulus; // 整數部分計算用:商數、模數。 // 金額數值轉換為字符,分割整數部分和小數部分:整數、小數分開來搞(小數部分有可能四舍五入后對整數部分有進位)。 var NoneDecLen = (typeof (maxDec) == "undefined" || maxDec == null || Number(maxDec) < 0 || Number(maxDec) > 5); // 是否未指定有效小數位(true/false) parts = dValue.split('.'); // 數組賦值:(整數部分.小數部分),Array的length=1則僅為整數。 if (parts.length > 1) { vInt = parts[0]; vDec = parts[1]; // 變量賦值:金額的整數部分、小數部分 if (NoneDecLen) { maxDec = vDec.length > 5 ? 5 : vDec.length; } // 未指定有效小數位參數值時,自動取實際小數位長但不超5。 var rDec = Number("0." + vDec); rDec *= Math.pow(10, maxDec); rDec = Math.round(Math.abs(rDec)); rDec /= Math.pow(10, maxDec); // 小數四舍五入 var aIntDec = rDec.toString().split('.'); if (Number(aIntDec[0]) == 1) { vInt = (Number(vInt) + 1).toString(); } // 小數部分四舍五入后有可能向整數部分的個位進位(值1) if (aIntDec.length > 1) { vDec = aIntDec[1]; } else { vDec = ""; } } else { vInt = dValue; vDec = ""; if (NoneDecLen) { maxDec = 0; } } if (vInt.length > 44) { return '<asp:Literal ID="Literal3" Text="<%$Resources:FlowMan.WebControls,SPLegalAmountField_AlertInputLawful%>" runat="server"></asp:Literal>'; } // 准備各字符數組 Prepare the characters corresponding to the digits: digits = new Array("零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"); // 零~玖 radices = new Array("", "拾", "佰", "仟"); // 拾,佰,仟 bigRadices = new Array("", "萬", "億", "兆", "京", "垓", "杼", "穰", "溝", "澗", "正"); // 萬,億,兆,京,垓,杼,穰,溝,澗,正 decimals = new Array("角", "分", "厘", "毫", "絲"); // 角/分/厘/毫/絲 resAIW = ""; // 開始處理 // 處理整數部分(如果有) if (Number(vInt) > 0) { zeroCount = 0; for (i = 0; i < vInt.length; i++) { p = vInt.length - i - 1; d = vInt.substr(i, 1); quotient = p / 4; modulus = p % 4; if (d == "0") { zeroCount++; } else { if (zeroCount > 0) { resAIW += digits[0]; } zeroCount = 0; resAIW += digits[Number(d)] + radices[modulus]; } if (modulus == 0 && zeroCount < 4) { resAIW += bigRadices[quotient]; } } resAIW += "元"; } // 處理小數部分(如果有) for (i = 0; i < vDec.length; i++) { d = vDec.substr(i, 1); if (d != "0") { resAIW += digits[Number(d)] + decimals[i]; } } // 處理結果 if (resAIW == "") { resAIW = "零" + "元"; } // 零元 if (vDec == "") { resAIW += "整"; } // ...元整 resAIW = CN_SYMBOL + minus + resAIW; // 人民幣/負......元角分/整 return resAIW; } </script> </Template> </SharePoint:RenderingTemplate> <SharePoint:RenderingTemplate ID="SPLegalAmountFieldControlDisplay" runat="server"> <Template> <asp:Label ID="LabSPLegalAmountField" class="LabSPLegalAmountField" runat="server"></asp:Label> <asp:HiddenField ID="LabhidSPLegalAmountField" Value="" runat="server" /> </Template> </SharePoint:RenderingTemplate>
SPLegalAmountFieldControl.cs代碼
using System; using System.Data; using System.Runtime.InteropServices; using System.Web.UI.WebControls; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Web.UI; using System.Text; namespace FlowMan.WebControls.SPLegalAmountField { [CLSCompliant(false)] public class SPLegalAmountFieldControl : BaseFieldControl { //定義表單呈現狀態時的控件 protected TextBox txtSPLegalAmountField; protected HiddenField hidSPLegalAmountField; protected HiddenField hidSPLegalAmountFieldPropery; protected Label LabSPLegalAmountField; protected HiddenField LabhidSPLegalAmountField; protected override string DefaultTemplateName { get { //下面的用戶控件名,<SharePoint:RenderingTemplate> 控件的ID 需要等於這個值 return "SPLegalAmountFieldControl"; } } public override string DisplayTemplateName { get { return "SPLegalAmountFieldControlDisplay"; } } //取值與賦值 public override object Value { get { EnsureChildControls(); SPLegalAmountFieldValue fieldValue = new SPLegalAmountFieldValue(); SPLegalAmountField field = (SPLegalAmountField)base.Field; FormField txtAmountLower = GetCurrentFormFieldControl((Control)this.Page, field.SPLegalAmountFieldRelevanceListField); if (txtAmountLower == null || txtAmountLower.Value == null) { if (SPContext.Current.Item[field.SPLegalAmountFieldRelevanceListField] != null) { SPFieldType filetype = SPContext.Current.Item.Fields.GetFieldByInternalName(field.SPLegalAmountFieldRelevanceListField).Type; SPFieldCalculated filecal = (SPFieldCalculated)SPContext.Current.Item.Fields.GetFieldByInternalName(field.SPLegalAmountFieldRelevanceListField); string fieldCalculatedValue = filecal.GetFieldValueAsText(SPContext.Current.Item[field.SPLegalAmountFieldRelevanceListField]); double amount = Convert.ToDouble(fieldCalculatedValue); fieldValue.AmountCapital = new RMBCapitalization().RMBAmount(amount); fieldValue.AmountNumber = fieldCalculatedValue; } else { fieldValue.AmountCapital = txtSPLegalAmountField.Text; fieldValue.AmountNumber = hidSPLegalAmountField.Value; } } else { fieldValue.AmountCapital = txtSPLegalAmountField.Text; fieldValue.AmountNumber = hidSPLegalAmountField.Value; } return fieldValue; } set { EnsureChildControls(); SPLegalAmountFieldValue fieldValue = (SPLegalAmountFieldValue)value; if (LabSPLegalAmountField != null && fieldValue!=null) { LabSPLegalAmountField.Text = fieldValue.AmountCapital; LabhidSPLegalAmountField.Value = fieldValue.AmountNumber; } else if (txtSPLegalAmountField != null && fieldValue != null) { txtSPLegalAmountField.Text = fieldValue.AmountCapital; hidSPLegalAmountField.Value = fieldValue.AmountNumber; SPLegalAmountField field = (SPLegalAmountField)Field; hidSPLegalAmountFieldPropery.Value = field.SPLegalAmountFieldRelevanceListField; } base.Value = fieldValue; } } public override void Focus() { EnsureChildControls(); // txtCurrentUserDepart.Focus(); } protected override void CreateChildControls() { if (Field == null) return; if (this.ControlMode == SPControlMode.Display) { this.TemplateName = this.DisplayTemplateName; } base.CreateChildControls(); if (ControlMode == SPControlMode.Display) { LabSPLegalAmountField = (Label)TemplateContainer.FindControl("LabSPLegalAmountField"); if (LabSPLegalAmountField == null) throw new ArgumentException("未找到LabSPLegalAmountField控件"); LabhidSPLegalAmountField = (HiddenField)TemplateContainer.FindControl("LabhidSPLegalAmountField"); if (LabhidSPLegalAmountField == null) throw new ArgumentException("未找到LabhidSPLegalAmountField控件"); SPLegalAmountFieldValue fieldValue = (SPLegalAmountFieldValue)this.ItemFieldValue; if (fieldValue != null) { LabSPLegalAmountField.Text = fieldValue.AmountCapital; LabhidSPLegalAmountField.Value = fieldValue.AmountNumber; } } else { txtSPLegalAmountField = (TextBox)TemplateContainer.FindControl("txtSPLegalAmountField"); hidSPLegalAmountField = (HiddenField)TemplateContainer.FindControl("hidSPLegalAmountField"); hidSPLegalAmountFieldPropery = (HiddenField)TemplateContainer.FindControl("hidSPLegalAmountFieldPropery"); if (txtSPLegalAmountField == null) throw new ArgumentException("未找到txtSPLegalAmountField控件"); if (hidSPLegalAmountField == null) throw new ArgumentException("未找到hidSPLegalAmountField控件"); if (hidSPLegalAmountFieldPropery == null) throw new ArgumentException("未找到hidSPLegalAmountFieldPropery控件"); SPLegalAmountField field = (SPLegalAmountField)base.Field; if (!string.IsNullOrEmpty(field.SPLegalAmountFieldTextboxWidth)) txtSPLegalAmountField.Width = Convert.ToInt32(field.SPLegalAmountFieldTextboxWidth.Trim()); FormField txtAmountLower = GetCurrentFormFieldControl((Control)this.Page, field.SPLegalAmountFieldRelevanceListField); if (txtAmountLower != null) { string _txtAmountLowerId = txtAmountLower.Controls[0].ClientID + txtAmountLower.Controls[0].ClientID.Replace(txtAmountLower.ClientID, "") + "_TextField"; StringBuilder jsstr = new StringBuilder(); jsstr.AppendLine("<script type=\"text/javascript\">"); jsstr.AppendLine("function convertAmount"+_txtAmountLowerId+"(){"); jsstr.AppendLine("var _this=document.getElementById('" + _txtAmountLowerId + "');"); jsstr.AppendLine("var _amountlegal = convert(_this.value,_this);"); jsstr.AppendLine("document.getElementById('" + txtSPLegalAmountField.ClientID + "').value = _amountlegal;"); jsstr.AppendLine("document.getElementById('" + hidSPLegalAmountField.ClientID + "').value = _this.value;"); jsstr.AppendLine("}"); jsstr.AppendLine("if(/msie/i.test(navigator.userAgent)){"); jsstr.AppendLine("document.getElementById('" + _txtAmountLowerId + "').onpropertychange = convertAmount"+_txtAmountLowerId+""); jsstr.AppendLine("}"); jsstr.AppendLine("else"); jsstr.AppendLine("{"); jsstr.AppendLine("document.getElementById('" + _txtAmountLowerId + "').addEventListener(\"input\",convertAmount" + _txtAmountLowerId + ",false);"); jsstr.AppendLine("}"); jsstr.AppendLine("convertAmount" + _txtAmountLowerId + "();"); jsstr.AppendLine("</script>"); this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Amountchangejs" + field.SPLegalAmountFieldRelevanceListField, jsstr.ToString()); } } } public FormField GetCurrentFormFieldControl(Control control,string fieldName) { FormField result = null; if (control is FormField) { if (((FormField)control).Field.InternalName == fieldName) { result = (FormField)control; } } else { foreach (Control c in control.Controls) { result = GetCurrentFormFieldControl(c, fieldName); if (result != null) { break; } } } return result; } } }
FLDTYPES_SPLegalAmountField.xml 配置代碼
<?xml version="1.0" encoding="utf-8"?> <FieldTypes> <FieldType> <Field Name="TypeName">SPLegalAmountField</Field> <Field Name="TypeDisplayName">$Resources:FlowMan.WebControls,SPLegalAmountField_TypeDisplayName;</Field> <Field Name="TypeShortDescription">$Resources:FlowMan.WebControls,SPLegalAmountField_TypeShortDescription;</Field> <Field Name="ParentType">MultiColumn</Field> <Field Name="FieldTypeClass">FlowMan.WebControls.SPLegalAmountField.SPLegalAmountField, FlowMan.WebControls, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=9d3cecd1bba4cc9e</Field> <Field Name="FieldEditorUserControl">/_controltemplates/FlowMan.WebControls/SPLegalAmountFieldEditor.ascx</Field> <Field Name="UserCreatable">TRUE</Field> <Field Name="Sortable">TRUE</Field> <Field Name="Filterable">TRUE</Field> <Field Name="ShowOnSureyCreate">TRUE</Field> <Field Name="ShowOnListCreate">TRUE</Field> <Field Name="ShowOnColumnTemplateCreate">TRUE</Field> <PropertySchema> <Fields> <Field Name="SPLegalAmountFieldRelevanceListField" Type="Text" Hidden="TRUE" /> <Field Name="SPLegalAmountFieldTextboxWidth" Type="Text" Hidden="TRUE" /> </Fields> </PropertySchema> </FieldType> </FieldTypes>
fldtypes_SPLegalAmountField.xsl 配置代碼
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" ddwrt:oob="true"> <xsl:output method="html" indent="no"/> <xsl:template match="FieldRef[@FieldType='SPLegalAmountField']" mode="Note_body"> <!--Set the thisNode parameter to the current node--> <xsl:param name="thisNode" select="." /> <!--Store the current element--> <xsl:variable name="curElement" select="current()" /> <!--Store the field value we are rendering in a variable for easier access--> <xsl:variable name="fldVal" > <!--We want to return the value of the field that has the same name as the current element being processed--> <xsl:value-of select="$thisNode/@*[name()=$curElement/@Name]" disable-output-escaping="yes"/> </xsl:variable> <!--Render our result field with the home score - away score construct--> <!-- <xsl:value-of select="substring-after($fldVal, ', ')"/>--> <xsl:value-of select="substring-before($fldVal, ', ')" disable-output-escaping="yes" /> </xsl:template> </xsl:stylesheet>