Why are my ASP.NET webforms controls not rendering? - c#

I have a feeling I'm missing one small thing. I have very simple page, created from the ASP.NET templates in VS2010. My Default.aspx consists of simply the following code. The Site.Master page is doing what it's supposed to.
<%#Page
Title="Home Page"
Language="C#"
MasterPageFile="~/Site.master"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="UserControlTest._Default" %>
<%#Register
TagPrefix="tsi"
Namespace="UserControlTest.Controls"
Assembly="UserControlTest" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<!-- HERE BE DRAGONS -->
<tsi:BigHelloBanner runat="server" />
<tsi:SmallHelloBanner runat="server" />
</asp:Content>
BigHelloBanner contains this:
<%#Control
Language="C#"
AutoEventWireup="true"
Visible="true"
CodeBehind="BigHelloBanner.ascx.cs"
Inherits="UserControlTest.Controls.BigHelloBanner" %>
<h1>HI!</h1>
Both the codebehind files in both objects are empty, and inherit from UserControl. The behavior is the same inheriting from Control. When I view-source on the rendered output, nothing from the HelloBanners is output, except some newlines. The HERE BE DRAGONS comment is visible, which indicates to me that the master page and all that works fine. I am expecting to see the <h1>HI!</h1> markup in the output as well. What am I missing? This seems really basic.

It looks like that you're referring to the empty code-behind class instead of the ASCX file with the output. Use the src attribute in your #Register directive:
<%#Register
TagPrefix="tsi"
TagName="BigHelloBanner"
Src="BigHelloBanner.ascx" %>

I can't see the src attribute here where is your control held ?
<%#Register
TagPrefix="tsi"
Namespace="UserControlTest.Controls"
Assembly="UserControlTest"
src="?" %>

Since BigHelloBanner is a web user control, you should try registering it like this:
<%#Register TagPrefix="tsi" TagName="BigHelloBanner" Src="~/pathToUserControls/BigHelloBanner.ascx" %>

Don't you still need to give an ID to each control instance?

Related

Finding What Populates Content Placeholder on ASP.NET Site

I have very little experience with ASP or .NET but have a need to make some content and style edits to one of such sites. The problem is that I am having a hard time finding the section which I need to edit.
In the main file called default.aspx, there is code like this:
<%# Page Title="" Language="VB" MasterPageFile="~/Company_New_NoMenus.master" CodeFile="Default.aspx.vb" Inherits="_Default" AutoEventWireup="false" %>
<%# Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
content here
The content which needs to be edited is located inside the ContentPlaceHolderID="HeadContent". I cannot find where it is populated. I tried searching the directory of the site to no avail.
If you use Windows Server, IIS and ASP, I would love some ideas about where I could look for it. Could it be set in the environment or be done in some other strange way? How would you look for it?
Any help is appreciated!

Getting Error in using user control(.ascx) on asp.net mvc layout page

I am integrating Asp.net User control(.ascx) in my Mvc Layout page i.e is master page and i am using razor engine.
Error:Error executing child request for handler
'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerWrapper ascx.
Control '1_hdfData' of type 'HiddenField' must be placed inside a form tag with runat=server.
This is my code:
_Layout.cshtml:
#Html.Partial("~/UserControls/Data.ascx")
Data.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Data.ascx.cs" Inherits="MyNameSpace.Data" %>
<asp:HiddenField ID="hdfData" runat="server" />
--Rest all other asp.net server side controls---.
Data.ascx.cs:
namespace MyNameSpace.Data
{
public partial class Data : ViewUserControl
{
//Page_Load events and other code
}
}
Is it like if i am inheriting User Control(.ascx) from ViewUserControl so i cant use asp.net control?
Can anybody help me with this?
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Data.ascx.cs" Inherits="MyNameSpace.Data" %>
<form runat="server">
<asp:HiddenField ID="hdfData" runat="server" />
--Rest all other asp.net server side controls---.
</form>
I am sharing one idea, but it's not good practice.
#Html.Partial("_ASCX_FILE")
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%# Register Assembly="SomeAssembly" Namespace="SomeNs" TagName="ASCX_FILE" %>
<ASCX_FILE:SomeControl runat="server" ID="fooControl" />
Possibly above solution can sort out your issue.
Suggesting again, try using pure MVC flavor. :)

user control not rendering content of ascx

i thought this was a simple issue until i started searching for answers and realised it's so simple i'm the only one who has it
my user control isnt displaying anything. what am i doing wrong? (besides letting this be my life...)
control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ctrl.ascx.cs" Inherits="proj.UserControls.ctrl" %>
asdjkldasfjasdfljdfasjklasdfjkl
use:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="page.aspx.cs" Inherits="proj.Admin.page" %>
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<cc1:ctrl ID="test" runat="server" />
</asp:Content>
Change:-
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
To
<%# Register TagPrefix="cc1" TagName="ctrl" Src="/path/to/ctrl.ascx" %>
You're missing TagName, which represents the text following the colon in the control declaration. You're also not telling the engine where to find the source file ( Src attribute ). Change /path/to to represent the path from root to your control.
IF you have created custom control then you should add reference of the dll of your custom control ( from choose items from ToolBox of Visual Studio). and then Use the following tag in the page :
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
If you created User Control then add the following line in your page:
<%# Register src="~/UserControls/ctrl.ascx" TagName="ctrl" tagprefix="cc1" %>
instead of
<%# Register assembly="proj" namespace="proj.UserControls" tagprefix="cc1" %>
use
<%# Register src="~/UserControls/ctrl.ascx" TagName="ctrl" tagprefix="cc1" %>
Make sure you haven't set visible="false" on a panel or div containing your control.
That would've saved me a good hour.
I had to add a new user control to an existing project that already contained a lot of them, and wondered why mine was not rendering. Turns out you can also specify this in Web.config, under configuration, system.web, pages, like this:
<controls>
<add tagPrefix="cc1" tagName="ctrl" src="~/UserControls/ctrl.ascx" />
...which will register the control for the whole project, so you don't have to specify this on every page. Useful for controls that are heavily reused.

Moving code around results in "unrecognized tag prefix or design filter" error

I have a Visual Web Developer 2010 Express Edition Web Application. The Default.aspx starts off as follows:-
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MixedZone.Default"%>
<%# Register assembly="ComponentArt.Web.UI"
namespace="ComponentArt.Web.UI"
tagprefix="ComponentArt" %>
<%# Register TagPrefix="MixedZone"
TagName="NumberInput"
Src="Common/UserControls/NumberInput.ascx" %>
The NumberInput.ascx starts like this:-
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="NumberInput.ascx.cs" Inherits="MixedZone.UserControls.NumberInput" %>
<%# Register assembly="ComponentArt.Web.UI" namespace="ComponentArt.Web.UI" tagprefix="ComponentArt" %>
<asp:Literal ID="Literal1" runat="server" Visible="False"></asp:Literal>
<ComponentArt:NumberInput ID="niNumberInput"
runat="server"
CssClass="valid"
...etc
So far, so good. But I wish to move this code to a different directory. I do this (editing the csproj file by hand) and change the aspx and aspc files accordingly:-
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MixedZone.Default"%>
<%# Register assembly="ComponentArt.Web.UI"
namespace="ComponentArt.Web.UI"
tagprefix="ComponentArt" %>
<%# Register TagPrefix="MixedZone"
TagName="NumberInput"
Src="../MixedZone/Common/UserControls/NumberInput.ascx" %>
and
<%# Control Language="C#" AutoEventWireup="True" CodeBehind="../MixedZone/Common/UserControls/NumberInput.ascx.cs" Inherits="MixedZone.UserControls.NumberInput" %>
<%# Register assembly="ComponentArt.Web.UI" namespace="ComponentArt.Web.UI" tagprefix="ComponentArt" %>
<asp:Literal ID="Literal1" runat="server" Visible="False"></asp:Literal>
<ComponentArt:NumberInput ID="niNumberInput"
runat="server"
CssClass="valid"
...etc
but when I do so, the ComponentArt: is marked with the green underline, and the message
Unrecognized tag prefix or device filter 'ComponentArt'
(about five hundred compilation errors appear as a result). The directory Common\UserControls was moved in its entirety, and nothing else has been changed. Does anyone have any idea what the problem might be and what I might do about it?
I worked around this problem by using SubVersion to include the code in the old location even though it isn't "really" there. I don't like this very much as it seems like a clever-clever trick and a trap for future maintenance. But it won't have the directory if it isn't below the project's, and it won't follow a shortcut if I place one there.

Using nested Master Pages

I'm very new to ASP.NET, help me please understand MasterPages conception more.
I have Site.master with common header data (css, meta, etc), center form (blank) and footer (copyright info, contact us link, etc).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="_SiteMaster" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="tagHead" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<form id="frmMaster" runat="server">
<div>
<asp:ContentPlaceHolder ID="holderForm" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="holderFooter" runat="server">Some footer here</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
and I want to use second master page for a project into sub directory, which would contains SQL query on Page_Load for logging (it isn't necessary for whole site).
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<asp:ContentPlaceHolder ID="holderForm" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooter" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
But I have a problem: footer isn't displayed.
Where is my mistake? Am I right to use second master page as super class for logging?
Project page looks like this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="Server">
Some footer content
</asp:Content>
I've been working with nested master pages and have run in to something similar. From what I see where you have "Some footer here" in the Site.Master is where the problem lies and I've had similar problems with having content with-in a contentplaceholder tag. if you try this instead
<asp:ContentPlaceHolder ID="holderFooter" runat="server"/>Some footer here
Then you should be able to see the footer content.
I'm not sure I'd use master pages for this. If it's really just going to do logging, I'd implement IHttpModule, register it in web.config, and then check whether or not to log based on the path of the request. I think of master pages as being about content rather than other processing such as logging.
See the IHttpModule walkthrough on MSDN for an example - in your BeginRequest handler, you'd probably check the request path and log appropriately if it matched.
Apologies if I misunderstood what you're trying to do though.
You should leave your ContentPlaceHolder empty, for it gets substituted by the content of the Content in your actual Page...
When you move the "Some footer here" text to your Content, you will see your lines of text :)
HTH
This link gives a simple explanation on Master pages,
http://waxtadpole.wordpress.com/2009/01/16/master-page-content-not-visible-visual-studio-2008/
The question are you right to use child Master pages in this instance - I would say master pages should be helping you solve issues around building a consistent layout, not for whether or not logging should occur.
The problem is, when the text elements placed inside Default.aspx are put in their relative Content Placeholders, they are written on the placeholders of your Site.master page and not those of Project.master (which have the same names).
You should resolve the naming conflict, by assigning different ContentPlaceHolderIDs to the the placeholders in Project.master (this means you'll also have to change the references in Default.aspx).
This would be your Project.master file:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Project.master.cs" Inherits="_ProjectMaster" MasterPageFile="~/Site.master" %>
<asp:Content ContentPlaceHolderID="holderForm" runat="server">
<!-- whatever... -->
<asp:ContentPlaceHolder ID="holderFormInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
<!-- ... -->
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooter" runat="server">
<asp:ContentPlaceHolder ID="holderFooterInternal" runat="server" EnableViewState="true"></asp:ContentPlaceHolder>
</asp:Content>
And thus, your .aspx pages that use the Project master page instead of the global Page.master must be changed to:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/Project.master" %>
<asp:Content ContentPlaceHolderID="holderFormInternal" runat="server">
<p>Hello World!</p>
</asp:Content>
<asp:Content ContentPlaceHolderID="holderFooterInternal" runat="Server">
</asp:Content>
If the only reason is to implement loggin why would you mess around with masterpages?
If the logging isent supposed to display any text!?
You either do as Skeet proposed with an IHTTP handler.. Or lazier one would be do have a class that derives from webpage and implement logging in that class and make your pages that need logging dervice from that..
ex:
public class LoggingPage : : System.Web.UI.Page
{
public override void OnLoad()
{
// Do logging
}
}
partial class OneOfTheWebPages : LoggingPage
{
public void onLoad()
{
base.onLoad();
}
}
I may be misunderstanding your problem - but from the code you've posted, there isn't anything in the footer.
In your Project page, the <asp:Content> tag for the holderFooter content place holder doesn't have anything in it.
I have next inheritance tree:
Site.master <-- Page1.aspx
<-- Project.master <-- Page2.aspx
And I don't know why Page2 display only content of itself and it's master page - Project. But doesn't display a content of Site (as Page1 does) Why? What have I to write for doing that?

Categories

Resources