开发者

Rendering template for newform and code behind

I'm trying to create a rendering template for my forms which need complex validations treatments. the rendering template is working fine wwith :

<XmlDocuments>
    <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/f开发者_StackOverflow中文版orms">
        <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
            <Display>ListForm</Display>
            <Edit>ChaisTemplate</Edit>
            <New>ChaisTemplate</New>
        </FormTemplates>
    </XmlDocument>          
</XmlDocuments>

and with my ascx :

<%@ Control AutoEventWireup="true" CodeBehind="ChaisTemplate.ascx.cs" 
Inherits="TransactionsFormsTemplates.ControlTemplates.ChaisTemplate, TransactionsFormsTemplates"
    Language="C#"  %>
<%@ Assembly Name="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    Namespace="Microsoft.SharePoint.WebControls" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBar" Src="~/_controltemplates/ToolBar.ascx" %>
<%PageLoad(this, null);%>
<SharePoint:RenderingTemplate ID="ChaisTemplate" runat="server">

.....

but I can't have any control mappings in my code behind (in the webapp bin with cas policies, and it's correctly deployed) this :

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

is null when it comes to :

 public partial class ChaisTemplate : System.Web.UI.UserControl 
{
    protected TextBox test;

so everytime I call test.Text on functions, I get a nullreferenceexception, because test is never mapped to my ascx textbox. why ? plus, the PageLoad is never called like in classic asp.net pages, even with <%PageLoad(this, null);%> at the beginning. however every event works :

 <asp:Button runat="server" ID="button" OnClick="button_OnClick"/>

this will actually call button_OnClick in my code behind. But all my properties are null because not mapped.

did I miss something?


I'v been doing it like this. Take this as an example RenderingTemplate:

<SharePoint:RenderingTemplate ID="ParentItemsListView" runat="server">
    <Template>
        <table cellpadding=0 cellspacing=0>
            <tr><td nowrap class="UserGenericHeader"><asp:Label ID="lblParentItems" runat="server" /></td></tr>
            <tr><td><SharePoint:ListViewByQuery ID="listViewByQuery" runat="server" /><br /></td></tr>
        </table>
    </Template>
</SharePoint:RenderingTemplate>

Then in Render event i can reference these controls like this:

    [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
    protected override void Render(HtmlTextWriter output)
    {
        if (this.Visible)
        {
            Label lblParentItems = this.TemplateContainer.FindControlRecursive<Label>("lblParentItems");
            lblParentItems.Text = "Pievienotie " + this.ChildList.Title;

            ListViewByQuery listView = TemplateContainer.FindControlRecursive<ListViewByQuery>("listViewByQuery");
            listView.List = this.ChildList;
            ...
            base.Render(output);
        }
   }

Ahh, yes, i use a handy extension function

public static T FindControlRecursive<T>(this Control parentControl, string id) where T : Control
{
    T ctrl = default(T);

    if ((parentControl is T) && (parentControl.ID == id))
        return (T)parentControl;

    foreach (Control c in parentControl.Controls)
    {
        ctrl = c.FindControlRecursive<T>(id);

        if (ctrl != null)
            break;
    }
    return ctrl;
}

However i had an issue where i inherited from SaveButton that i couldn't reference a control. In that case i changed so that i inherit from FormControl and i could find my control under this.Controls.

Also note that you cannot reference controls up until OnLoad event has executed (i.e if you're in OnLoad function, call base.OnLoad(e) before referencing controls.

Why is it so? I guess because it's not like you are having a template and then code behind (working with the same class/object), but it's more like you are rendering that ascx template in a different class/object file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜