1
votes

I'm trying to use a .NET User Control Macro in Umbraco 6.0.6 editor, but using a formview control I can't get access to controls inside it on page_load event.

Ex:

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>

CODE-FILE:

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

MasterPage Template:

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

The result is an "Object reference not set to an instance of an object." on the line: CType(FormView1.FindControl("txt_name"), TextBox).Text = "name"

This only happens when the control is rendered from the umbraco editor, if I use the control normally in a web page or masterpage it works ok.

Anyone with the same results?

thanks

1
My apologies, I just noticed this question was asked over a week ago. Hopefully my answer is still helpful! I really suggest including the FindControlRecursive helper method in your project. I use it all the time.trnelson

1 Answers

0
votes

I've encountered this same issue. This happens because of the way the controls are nested in a different container in the editor and thus a standard FindControl() won't find it.

I've implemented Rick Strahl's recursive FindControl() helper method in all of my projects and that usually solves the problem: http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

Usually I put it in a static ControlHelper class and call it like so:

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

Optionally you can turn off the "Render Control in Editor" option in the Macro settings. 99% of the time I turn this off anyway since most of my macros don't require the presentation to display in the editor.