【问题标题】:Umbraco - Using .net macro in editor with binding controlsUmbraco - 在编辑器中使用 .net 宏和绑定控件
【发布时间】:2013-06-06 20:40:44
【问题描述】:

我正在尝试在 Umbraco 6.0.6 编辑器中使用 .NET 用户控件宏,但使用 formview 控件我无法在 page_load 事件中访问其中的控件。

例如:

ASCX:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="teste.ascx.vb" Inherits="usercontrols_teste" %>
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert">
    <InsertItemTemplate>
        <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
    </InsertItemTemplate>
</asp:FormView>

代码文件:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    FormView1.DefaultMode = FormViewMode.Insert
    CType(FormView1.FindControl("txt_name"), TextBox).Text = "name"
End Sub

母版页模板:

<form id="form1" runat="server">    
   <umbraco:Item ID="Item1" field="conteudoPagina" runat="server"></umbraco:Item> 
</form>

结果是“对象引用未设置为对象的实例”。在线上: CType(FormView1.FindControl("txt_name"), TextBox).Text = "name"

这仅在从 umbraco 编辑器呈现控件时发生,如果我在网页或母版页中正常使用该控件,它可以正常工作。

有相同结果的人吗?

谢谢

【问题讨论】:

  • 抱歉,我刚刚注意到这个问题是在一个多星期前提出的。希望我的回答仍然有帮助!我真的建议在您的项目中包含 FindControlRecursive 辅助方法。我一直在使用它。

标签: asp.net .net vb.net umbraco webforms


【解决方案1】:

我也遇到过同样的问题。这是因为控件嵌套在编辑器中的不同容器中的方式,因此标准 FindControl() 找不到它。

我在所有项目中都实现了 Rick Strahl 的递归 FindControl() 辅助方法,这通常可以解决问题:http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

通常我把它放在一个静态的 ControlHelper 类中并这样调用它:

((TextBox)ControlHelper.FindControlRecursive(this, "txt_name")).Text = "name";

您可以选择在宏设置中关闭“在编辑器中渲染控件”选项。 99% 的时间我都会关闭它,因为我的大多数宏不需要在编辑器中显示演示文稿。

【讨论】:

    猜你喜欢
    • 2016-05-12
    • 2014-02-04
    • 2019-12-04
    • 2011-09-03
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多