newform 和代码隐藏的渲染模板

发布于 2024-11-27 13:55:26 字数 2001 浏览 2 评论 0 原文

我正在尝试为需要复杂验证处理的表单创建一个渲染模板。 渲染模板工作正常 wwith :

<XmlDocuments>
    <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
        <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
            <Display>ListForm</Display>
            <Edit>ChaisTemplate</Edit>
            <New>ChaisTemplate</New>
        </FormTemplates>
    </XmlDocument>          
</XmlDocuments>

和我的 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">

.....

但我不能在我的代码后面有任何控制映射(在带有 cas 策略的 webapp bin 中,并且它已正确部署) this :

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

当涉及到 : 时,

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

为 null,所以每次我在函数上调用 test.Text 时,我都会得到 nullreferenceException,因为 test 从未映射到我的 ascx 文本框。为什么 ? 另外,即使使用 <%PageLoad(this, null);%> ,PageLoad 也不会像经典的 ASP.NET 页面那样被调用。一开始。 但是每个事件都有效:

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

这实际上会在我的代码后面调用button_OnClick。但我的所有属性都是空的,因为没有映射。

我错过了什么吗?

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/forms">
        <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2024-12-04 13:55:26

我一直都是这样做的。以此为例 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>

然后在渲染事件中我可以像这样引用这些控件:

    [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);
        }
   }

啊,是的,我使用了一个方便的扩展函数

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;
}

但是我有一个问题,我从 SaveButton 继承,但我不能' t 引用一个控件。在这种情况下,我进行了更改,以便从 FormControl 继承,并且可以在 this.Controls 下找到我的控件。

另请注意,在 OnLoad 事件执行之前您无法引用控件(即,如果您在 OnLoad 函数中,请在引用控件之前调用 base.OnLoad(e)

为什么会这样?我猜是因为它是不像你有一个模板,然后在后面编写代码(使用相同的类/对象),但它更像是你在不同的类/对象文件中渲染该 ascx 模板。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文