Dan McClain
Dan McClain

Reputation: 11920

ASP.Net controls appear on page, but not in a user control on that page

I am using an ASP.Net Web Application project. I have a user control which has an asp.net button in it. When i use that user control on the page, the button does not appear, but if i put the button directly on the page, the buttons shows up. Any idea what the problem is?

Also, inside that user control, i can override the render method and the test passed to the render method works, but I still do not get a button

The assembly is registered in the web.config

EDIT:After dave's post, i found that anything put in the .ascx file does not work, while overriding that user control's render method works

The Page

<%@ Page Title="Home" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Site.Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div>
        <uc:SomeCustomControl ID="myControl" runat="server" />
        <asp:Button runat="server" Text="outControl" />
    </div>
</asp:Content>

the control .ascx file

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="Site.Controls.TestControl" %>
    <asp:Button runat="server" Text="InControl" /><!--cant see this button-->
    <p>I can't see this</p><!--cant see this text-->

the code behind for the .ascx file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Site.Controls
{
    public partial class TestControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("I can see this");
            base.Render(writer);
        }

    }
}    

And the page source

        I can see this
        <input type="submit" name="ctl00$ContentPlaceHolder1$Button1" value="outControl" id="ctl00_ContentPlaceHolder1_Button1" />

Upvotes: 2

Views: 2529

Answers (3)

AndreasKnudsen
AndreasKnudsen

Reputation: 3491

If you register the control in the top of the page like this

<%@ Register TagPrefix="uc" TagName="SomeCustomControl" Src="~/PathTo/TestControl.ascx" %>

(the key point here is the Src="..." part )

then the markup and controls in the TestControl.ascx will be visible and usable. if you only specify the namespace of the control for instance in web.config <pages> directive (or in the top of the page) then ONLY the codebehind is used, and any markup in the codefront (.ascx) is ignored

Upvotes: 3

Jeff Martin
Jeff Martin

Reputation: 11022

Is this page and control in a standard default application? Is there any chance that a web.config or machine.config is having some influence over your UserControl? Perhaps an HttpModule?

In addition to checking from a "default" web site setup, you may also want to do some debugging and check the properties of your objects. You can do a write in your render method. Maybe look at how many controls are in the user control at render time. (maybe something is removing or clearing the controls container).

Upvotes: 0

Dave Swersky
Dave Swersky

Reputation: 34820

Is there any code in the user control affecting the button's visibility?

Does the rest of the user control appear, or does it only contain the button? If it's only a button then the entire user control may not be rendering properly. Add some text to the user control as a test to make sure it is just the button that's not working, not the entire user control.

You could also try enabling tracing for the page. The user control and button should show up in the control hierarchy listing. If they don't, then one or both isn't rendering properly.

Upvotes: 0

Related Questions