簡易代碼之家

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            157 Posts :: 2 Stories :: 57 Comments :: 0 Trackbacks
          最后實現效果如下:

          設置欄如下:

          解決方案結構如下:


          fldtypes_RoyCustomField.xml 內容如下:
          <?xml version="1.0" encoding="utf-8" ?>
          <FieldTypes>
            
          <FieldType>
              
          <Field Name="TypeName">RoyCustomField</Field>
              
          <Field Name="ParentType">Text</Field>
              
          <Field Name="TypeDisplayName">RoyCustomField</Field>
              
          <Field Name="TypeShortDescription">RoyCustomField</Field>
              
          <Field Name="UserCreatable">TRUE</Field>
              
          <Field Name="FieldTypeClass">RoyCustomField.RoyCustomField,$SharePoint.Project.AssemblyFullName$</Field>
            
          </FieldType>
          </FieldTypes>

          RoyCustomField.cs 內容如下:
          using Microsoft.SharePoint;
          using Microsoft.SharePoint.WebControls;
          using System.Web.UI.WebControls;
          using System.Web.UI;
          using System;
          namespace RoyCustomField
          {
              
          public class RoyCustomField : SPFieldText
              {
                  
          public RoyCustomField(SPFieldCollection field, string strFieldName)
                      : 
          base(field, strFieldName)
                  {
                  }
                  
          public RoyCustomField(SPFieldCollection field, string strFieldName, string strDispName)
                      : 
          base(field, strFieldName, strDispName)
                  {
                  }
                      

                  
          public override BaseFieldControl FieldRenderingControl
                  {
                      
          get
                      {
                          BaseFieldControl fc 
          = new RoyText();
                          fc.FieldName 
          = this.InternalName;
                          
          return fc;
                      }
                  }
              }


              
          public class RoyText : BaseFieldControl
              {

                  
          private DropDownList _ddlProvince;
                  
          private DropDownList _ddlCity;
                  
          #region Province of autoPostBack
                  
          void Province_SelectedIndexChanged(object sender, EventArgs e)
                  {
                      _ddlCity.Items.Clear();
                      
          if (_ddlProvince.SelectedValue.ToString() == "河北省")
                      {
                          _ddlCity.Items.Add(
          new ListItem("石家莊市""石家莊市"));
                      }
                      
          else if (_ddlProvince.SelectedValue.ToString() == "山東省")
                      {
                          _ddlCity.Items.Add(
          new ListItem("濟南市""濟南市"));
                      }
                      
          else
                      {
                          _ddlCity.Items.Add(
          new ListItem("北京市""北京市"));
                      }
                  }
                  
          #endregion
                  
          protected override void CreateChildControls()
                  {

                      
          base.CreateChildControls();
                      
          this.TemplateContainer.FindControl("_ddlProvince");
                      _ddlProvince 
          = new DropDownList();
                      _ddlProvince.Items.Add(
          new ListItem("河北省""河北省"));
                      _ddlProvince.Items.Add(
          new ListItem("山東省""山東省"));

                      _ddlProvince.AutoPostBack 
          = true;
                      _ddlProvince.SelectedIndexChanged 
          += Province_SelectedIndexChanged;
                      
          this.Controls.Add(_ddlProvince);
                      
          this.Controls.Add(new LiteralControl(" - "));
                      
          this.TemplateContainer.FindControl("_ddlCity");
                      _ddlCity 
          = new DropDownList();
                      _ddlCity.Items.Add(
          new ListItem("石家莊市""石家莊市"));
                      _ddlCity.Items.Add(
          new ListItem("濟南市""濟南市"));
                      
          this.Controls.Add(_ddlCity);
                  }

                  
                  
          public override object Value
                  {
                      
          get
                      {
                          
          this.EnsureChildControls();
                          
          return _ddlProvince.Text+"-"+_ddlCity.Text;
                      }
                      
          set
                      {
                          
          string fv = (string)this.ItemFieldValue;
                          
          if (fv.Contains("-"))
                          {
                              
          string[] all = fv.Split('-');
                              _ddlProvince.Text 
          = all[0];
                              _ddlCity.Text 
          = all[1];
                          }
                      }
                  }
              }
          }

          注意事項:

          1.xml的文件名必須以fldtypes_開頭,否則不能識別。

          2.如果要設置默認值請Override DefaultValue,如果要驗證格式是否正確請Override GetValidatedString。


          本文參考:http://www.cnblogs.com/Roy_Cao/archive/2012/08/14/2637542.html 
          posted on 2013-07-02 10:09 Jakin.zhou 閱讀(375) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 通榆县| 仁布县| 岗巴县| 开江县| 南投市| 格尔木市| 辉南县| 靖安县| 宜昌市| 芦溪县| 莲花县| 甘孜| 黄龙县| 资溪县| 塘沽区| 忻城县| 和顺县| 堆龙德庆县| 安顺市| 呼伦贝尔市| 江安县| 馆陶县| 新宁县| 罗城| 洞头县| 乐业县| 黔西县| 会泽县| 普兰县| 旬阳县| 黄龙县| 华亭县| 铜川市| 台北市| 府谷县| 德江县| 将乐县| 兴化市| 秦安县| 冷水江市| 西乌珠穆沁旗|