I am using a base page and an inherited page. The base page is working fine.
In the inherited page I have the following #Page-directive:
<%# Page Title="" Language="C#" AutoEventWireup="True" Inherits="SubPage" CodeFileBaseClass="BasePage" CodeFile="SubPage.aspx.cs" %>
and the following code-behind file
public partial class SubPage: BasePage
The problem is that in Page_Load of the base class, all Controls are null.
A master-page is no solution for me in this case.
Inhert your code-behind as you would normally using:
public partial class SubPage: BasePage
and in markup do something like:
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="SubPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
Main content.
</asp:Content>
http://msdn.microsoft.com/en-us/library/vstudio/wtxbf3hh(v=vs.100).aspx
EDIT:
If masterpage is not what you want you'll have to do:
<%# Page Title="" Language="C#" AutoEventWireup="True" Inherits="BasePage" CodeFileBaseClass="BasePage" CodeFile="SubPage.aspx.cs" %>
and copy whole markup of BasePage below. Code-Behind is inherited same way again:
public partial class SubPage: BasePage
Related
I have following project structure where web application may contain user controls from the same web project or from a separate class library. The issue is in Default.aspx.cs, when I execute usercontrol2.SetValues(); I receive NullReferenceException since for some reason textbox2 is null. I've tried using EnsureChildControls(); but it does not help either. Should I register or invoke these usercontrols in some other way? Why does not it work out-of-box?
User control in web project
WebUserControl1.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplication1.WebUserControl1" %>
<asp:TextBox runat="server" ID="textbox1"></asp:TextBox>
WebUserControl1.ascx.cs
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public void SetValues()
{
textbox1.Text = "Hello";
}
}
}
User control in class library
WebUserControl2.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="ClassLibrary1.WebUserControl2" %>
<asp:TextBox runat="server" ID="textbox2"></asp:TextBox>
WebUserControl2.ascx.cs
namespace ClassLibrary1
{
public partial class WebUserControl2 : System.Web.UI.UserControl
{
public void SetValues()
{
textbox2.Text = "world";
}
}
}
Main page
Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%# Register tagPrefix="web" tagName="WebUserControl1" src="WebUserControl1.ascx" %>
<%# Register TagPrefix="web" Namespace="ClassLibrary1" Assembly="ClassLibrary1" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<web:WebUserControl1 runat="server" ID="usercontrol1"></web:WebUserControl1>
<web:WebUserControl2 runat="server" ID="usercontrol2"></web:WebUserControl2>
</asp:Content>
Default.aspx.cs
using System;
using System.Web.UI;
namespace WebApplication1
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
usercontrol1.SetValues(); // OK
usercontrol2.SetValues(); // NullReferenceException
}
}
}
Using ascx usercontrols from a different assembly is not possible.
I think you should look at this problem this way. Your classlibrary is compiled into a dll-file. This is the only thing that is available for your webapplication. The ‘ascx’ doesn’t get compiled into the dll, and is not available.
If you really want to keep some controls separated from your web-project, I think there are a few options:
Convert your usercontrol to a customcontrol. Custom controls are a bit more difficult to create, but it is certainly not impossible. This way you really get a re-usable control.
See also this question; it addresses your problem and there’s also a link to an algorithm which ‘converts’ an ascx usercontrol to a custom control. (I didn’t test that)
Asp.NET user control referenced from another project/dll has NULL properties
Make sure your ascx files are available to the web-project, by publishing to a path in your output. Then use LoadControl to load them serverside. This could work, but I also think some of the reusability from your classlibrary is lost.
Line 1: <%# Page Language="vb" AutoEventWireup="false" CodeBehind="Berechtigungen.aspx.vb" Inherits="Berechtigungen.Berechtigungen" %>
My ASPX Page: Berechtigungen.aspx & Berechtigungen.aspx.vb
The .aspx.vb file:
<Serializable()>
Public Class Berechtigungen
Why is my Inherit not working?
My web control doesn't appear in my web page in a different folder,
However it works in another web page in the same location.
My source code is like this :
..
<%Register src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1"
TagName="HumanUserControl"/>
...
<uc1:HumanUserControl runat="Server" id="HumanUserControl"/>
...
Error:
Request is not available in this context
The syntax on your Register tag is incorrect. And I'm not sure why you're using a ".wuc" extension instead of the default ".ascx".
You need matching <% %> tags, like so:
<%# Register Src="~/UI/Human/HumanUserControl.wuc" TagPrefix="uc1" TagName="HumanUserControl" %>
If you delete the <%Register .../> and <uc1 .../> tags from your page completely, then in Visual Studio, drag-and-drop the user control from the Solution Explorer directly onto your page wherever you want the control to appear, it will automatically add the correct Register and user control tags for you.
If this doesn't clear things up, then provide more detailed error information, including the code where the exception is thrown.
The following works just fine:
~/UI/Human/HumanUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="HumanUserControl.ascx.cs" Inherits="UserControlTest.UI.Human.HumanUserControl" %>
<asp:Label ID="lblTest" runat="server" />
~/UI/Human/HumanUserControl.ascx.cs
namespace UserControlTest.UI.Human
{
public partial class HumanUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = Request.Browser.Browser;
}
}
}
~/Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UserControlTest._Default" %>
<%# Register Src="~/UI/Human/HumanUserControl.ascx" TagPrefix="uc1" TagName="HumanUserControl" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<uc1:HumanUserControl runat="server" id="HumanUserControl" />
</asp:Content>
i designed web page and put controls in it , then i added name space to code behind file and add
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.master" CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<%# Import Namespace="RegisterName" %>
to asp file and
namespace RegisterName
{
public partial class _Default : System.Web.UI.Page
{
now i have some Error :
Error 4 'ASP.default_aspx' does not implement interface member 'System.Web.IHttpHandler.IsReusable'
You need both the page and the code-behind to be parts of the same namespace.
By adding the namespace declaration to the code-behind, it and the web page are now in different namespaces.
Additionally, the #Import page directive simply means that you can access types declared in the imported namespace, not that the page belongs to it.
You need to ensure that the #Page directive matches the fully qualified name of the code-behind class.
I have the following hierarchy:
Site.Master
Site2.Master
Page1.aspx
Page2.aspx
Page3.aspx
Page4.aspx
On Page3 and Page4 I can access an object on Site.Master using <%# MasterType VirtualPath="~/Site.Master" %> on aspx file.
How can I access same object on Page1 and Page2?
I tried:
1) Put <%# MasterType VirtualPath="~/Site.Master" %> on Page1.aspx but I receive an error.
2) Put <%# MasterType VirtualPath="~/Site.Master" %> on Site2.Master and <%# MasterType VirtualPath="~/Site2.Master" %> on Page1.aspx. Page1.aspx open but I can't access object using Master property.
Thank you.
public Site TopMasterPage
{
get
{
return (this.Master as Site) ?? this.Master.Master as Site;
}
}