开发者

Why are my ASP.NET webforms controls not rendering?

开发者 https://www.devze.com 2023-04-01 14:10 出处:网络
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

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 i开发者_开发技巧n 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消