ASP.Net控件显示页面上,但不是在该网页上用户控件 [英] ASP.Net controls appear on page, but not in a user control on that page

查看:181
本文介绍了ASP.Net控件显示页面上,但不是在该网页上用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ASP.Net Web应用程序项目。我有中有一个asp.net按钮的用户控件。当我在页面上使用该用户的控制,按钮没有出现,但如果我直接把页面上的按钮,这些按钮显示出来。任何想法是什么问题?

此外,该用户控件内,我可以重写Render方法,并传递给渲染方法工作的考验,但我仍然没有得到一个按钮

的组件被登记在web.config

编辑:Dave的文章后,我发现任何东西放在.ascx文件不起作用,而覆盖的用户控件的方式呈现作品

 <%@页标题=家LANGUAGE =C#的MasterPageFile =〜/ Master.MasterAutoEventWireup =真codeBehind =Default.aspx的的.cs继承=Site.Default%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =头=服务器>
< / ASP:内容>
< ASP:内容ID =内容2ContentPlaceHolderID =ContentPlaceHolder1=服务器>
    < D​​IV>
    < UC:SomeCustomControl ID =显示myControl=服务器/>
        < ASP:按钮=服务器文本=OUTCONTROL/>
    < / DIV>
< / ASP:内容>

控制.ascx文件

 <%@控制语言=C#AutoEventWireup =真codeBehind =TestControl.ascx.cs继承=Site.Controls.TestControl%GT ;
    < ASP:按钮=服务器文本=InControl/>< - 无法看到这个按钮 - >
    < P>我看不到该< / P><! - 无法看到这段文字 - >

背后的code为.ascx文件

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;命名空间Site.Controls
{
    公共部分类系统testControl:System.Web.UI.UserControl
    {
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
    保护覆盖无效渲染(HtmlTextWriter的作家)
    {
    writer.Write(我可以看到这个);
    base.Render(作家);
    }    }
}

和页面的源代码

 我可以看到这个
    <输入类型=提交名称=$ ctl00 $ ContentPlaceHolder1 Button1的价值=OUTCONTROLID =ctl00_ContentPlaceHolder1_Button1/>


解决方案

如果您在注册页面像这样

顶部的控制

 <%@注册标签preFIX =UC变量名=SomeCustomControlSRC =〜/ PathTo / TestControl.ascx%GT;

(这里的关键点是SRC =...部分)

然后在TestControl.ascx的标记和控件是可见的和可用的。如果只指定例如在web.config中&LT控制的命名空间;网页>指令(或在页面的顶部),则只有codebehind被使用,并且在codefront的任何标记(的.ascx)被忽略

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" />

解决方案

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

这篇关于ASP.Net控件显示页面上,但不是在该网页上用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆