希望在明天

如果,你没有耐心等待成功的到来,那么,你将用一生的耐心去面对失败。

一个简单的Checkbox Custom Field Type


这个字段是一个普通的Single line of text,作用就是显示一个Checkbox,checkbox 的value为这个Text字段的值。
效果图:




下面介绍制作过程:

1. 首先,写一个派生自标准Field Type的类,
 1    public class SPCheckboxField : SPFieldText
 2    {
 3        public SPCheckboxField(SPFieldCollection fields, string fieldName)
 4            : base(fields, fieldName)
 5        {
 6        }

 7
 8        public SPCheckboxField(Microsoft.SharePoint.SPFieldCollection fields, string typeName, string displayName)
 9            : base(fields, typeName, displayName)
10        {
11        }

12
13        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl
14        {
15            get
16            {
17                Microsoft.SharePoint.WebControls.BaseFieldControl checkboxFieldControl = new CheckboxFieldControl();
18                checkboxFieldControl.FieldName = InternalName;
19                return checkboxFieldControl;
20            }

21        }

22    }

2. 写一个UC 命名为CheckboxField.ascx:
1<%@ Control Language="C#" Debug="true" %>
2<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
3<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %>
4
5<SharePoint:RenderingTemplate ID="CheckboxFieldControl" runat="server">
6    <Template>
7        <asp:TextBox ID="txtUrl" Enabled="false" MaxLength="255" runat="server" Columns="50" />
8    </Template>
9</SharePoint:RenderingTemplate>

3. 写一个对应CheckboxField.ascx 的类:
 1  public class CheckboxFieldControl : BaseFieldControl
 2    {
 3        protected TextBox txtUrl;
 4
 5        protected override string DefaultTemplateName
 6        {
 7            get
 8            {
 9                return "CheckboxFieldControl";
10            }

11        }

12
13        public override object Value
14        {
15            get
16            {
17                EnsureChildControls();
18                return txtUrl.Text.Trim();
19            }

20            set
21            {
22                EnsureChildControls();
23                txtUrl.Text = (string)this.ItemFieldValue;
24            }

25        }

26
27        public override void Focus()
28        {
29            EnsureChildControls();
30            txtUrl.Focus();
31        }

32
33        protected override void CreateChildControls()
34        {
35            if (Field == nullreturn;
36           base.CreateChildControls();
37
38            if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display)
39                return;
40
41            txtUrl = (TextBox)TemplateContainer.FindControl("txtUrl");
42
43            if (txtUrl == null)
44                throw new ArgumentException("txtUrl is null. Corrupted CheckboxField.ascx file.");
45
46            txtUrl.TabIndex = TabIndex;
47            txtUrl.CssClass = CssClass;
48            txtUrl.ToolTip = Field.Title;
49        }

50    }

4. 写一个xml:
fldtypes_CheckboxField.xml注意名字必须以fldtypes_开头.

 1<?xml version="1.0" encoding="utf-8"?>
 2<FieldTypes>
 3    <FieldType>
 4        <Field Name="TypeName">CheckboxField</Field>
 5        <Field Name="ParentType">Text</Field>
 6        <Field Name="TypeDisplayName">Checkbox Field</Field>
 7        <Field Name="TypeShortDescription">Checkbox Field</Field>
 8        <Field Name="UserCreatable">TRUE</Field>
 9        <Field Name="ShowInListCreate">TRUE</Field>
10        <Field Name="ShowInSurveyCreate">TRUE</Field>
11        <Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
12        <Field Name="ShowInColumnTemplateCreate">TRUE</Field>
13        <Field Name="Sortable">FALSE</Field>
14        <Field Name="AllowBaseTypeRendering">TRUE</Field>
15        <Field Name="Filterable">FALSE</Field>
16        <Field Name="FieldTypeClass">CheckboxCustomField.SPCheckboxField,CheckboxCustomField,Version=1.0.0.0,Culture=neutral,PublicKeyToken=5acbdc949ff711be</Field>
17        <RenderPattern Name="DisplayPattern">
18            <Switch>
19                <Expr>
20                    <Column/>
21                </Expr>
22                <Case Value="">
23                </Case>
24                <Default>
25                    <HTML><![CDATA[<input type='checkbox' name='my_select' value=']]></HTML>
26                    <Column HTMLEncode="TRUE"/>
27                    <HTML><![CDATA[' />]]></HTML>
28                </Default>
29            </Switch>
30        </RenderPattern>
31    </FieldType>
32</FieldTypes>

   都是些配置的信息,最主要显示的部分是这里:
1                    <HTML><![CDATA[<input type='checkbox' name='my_select' value=']]></HTML>
2                    <Column HTMLEncode="TRUE"/>
3                    <HTML><![CDATA[' />]]></HTML>


5. 这样就OK了,开始部署:
    1) 将编译后dll放入GAC
    2) 把UC放到C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES
    3) 把xml放到C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML
    4) 重启IIS

在create column会看到我们的Checkbox Field:

红色的就是这个custom field type,蓝色的是陈曦那个。


posted on 2007-08-17 19:52  蒜头  阅读(1830)  评论(12编辑  收藏

导航

统计