您的位置:首页 > 其它

开发自定义字段类型 sharepoint

2011-09-06 21:43 417 查看
网上有好多类似的文章,但是他们写的注释太少了,我研究了一个星期才弄好。

我也写写吧,希望对新接触的朋友有帮助。

首先:写好一个xml文件

<?xml version="1.0" encoding="utf-8" ?>

<FieldTypes>

<FieldType>

<Field Name="TypeName">PeopleSelector</Field> --类名

<Field Name="ParentType">Text</Field> --字段类型

<Field Name="TypeDisplayName">People Selector</Field>

<Field Name="TypeShortDescription">PictureTag</Field> --显示名称

<Field Name="UserCreatable">TRUE</Field>

<Field Name="FieldTypeClass">

RownSearch(命名空间).PictureSelector(要调用的类名), RownSearch(命名空间), Version=1.0.0.0, Culture=neutral, PublicKeyToken=1a47724c4e1447d0

</Field>

</FieldType>

</FieldTypes>



namespace RownSearch

{

//继承

class PeopleSelector : SPFieldText(必须继承)

{

public PeopleSelector(

SPFieldCollection fields, string fieldName) :

base(fields, fieldName) { }

public PeopleSelector(

SPFieldCollection fields,

string typeName, string displayName) :

base(fields, typeName, displayName) { }

//上面是必写的

//两种方式一种自定义用户控件

public override BaseFieldControl FieldRenderingControl

{

get

{

//转到定义页

BaseFieldControl fieldControl = new PictureSelector_Control();

fieldControl.FieldName = InternalName;

return fieldControl;

}

}

}

}

namespace RownSearch

{

//必须继承BaseFieldControl

class PictureSelector_Control : BaseFieldControl

{

//自定义控件

protected TextBox Picture_TextBoxField;

protected HiddenField Picture_TextBoxFields;

protected Button Picture_Btn;

//创建控件

protected override void CreateChildControls()

{

//在编辑和新增下显示

if (this.Field == null || this.ControlMode == SPControlMode.Display)

{

return;

}

base.CreateChildControls();

//从模版中获取控件

Picture_TextBoxField = TemplateContainer.FindControl("Picture_TextBox") as TextBox;

//获取button

Picture_Btn = TemplateContainer.FindControl("btn_Picture") as Button;

}

//在显示模式下显示模版

//public override string DisplayTemplateName {

// get {

// return "";

// }

//}

//在默认模式下显示模版 //模版SharePoint:RenderingTemplate的id

protected override string DefaultTemplateName

{

get

{

return "Picture_FieldTemplate";//用户控件里SharePoint:RenderingTemplate的ID

}

}

//重写value

public override object Value

{

get

{

this.EnsureChildControls();

if (Picture_TextBoxField != null)

{

return Picture_TextBoxField.Text.ToString(); ;

}

else

{

return null;

}

}

set

{

this.EnsureChildControls();

if (Picture_TextBoxField != null)

{

Picture_TextBoxField.Text = (string)this.ItemFieldValue;

}

}

}

}

用户控件

<SharePoint:RenderingTemplate Id="TagTree_FieldTemplate" runat="server" >

<Template>

<script type="text/javascript">

function OpenToSearchPeople() {

var options = {

url: "/_layouts/showTagTree.aspx",

width: 600,

height: 480,

title: "标签",

dialogReturnValueCallback: TagTreeCloseCallback

};

SP.UI.ModalDialog.showModalDialog(options);

}

function TagTreeCloseCallback(result, value) {

debugger;

if (result == SP.UI.DialogResult.OK) {

var txtID = "ctl00_m_g_5a16be22_8c73_4a3f_a3ee_03fe2a571152_ff121_ctl00_ctl00_People_TextBox";

document.getElementById(txtID).value += value;

}

}

</script>

<asp:TextBox ID="People_TextBox" runat="server"/>

<asp:Button ID="Button1" runat="server" OnClientClick="javascript:OpenToSearchPeople();return false;" Text="标签" />

</Template>

</SharePoint:RenderingTemplate>

用户控件调一个模式化窗体,这个不是所有人都用的到的

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

<script type="text/javascript">

function btonTree_ClientClick() {

var treeTagBack = document.getElementById("<% =this.TreeTagHidden.ClientID %>").value;

SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, treeTagBack);

}

</script>

<asp:Button ID="BackTreeTag" runat="server" Text="确定" OnClientClick="javascript:btonTree_ClientClick();" />

<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" ShowCheckBoxes="All">

<ParentNodeStyle Font-Bold="False" />

<HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />

<SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />

<NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />

</asp:TreeView>

<asp:HiddenField ID="TreeTagHidden" runat="server"/>

</asp:Content>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: