How to avoid build errors with aspx include files? - asp.net

I have an aspx file that will contain a lot of sections and I would prefer to have each section in its own include file. I'm okay to put all of the code behind into the main file. I'm using: <!-- #include file ="section1.aspx" --> in the body. When I build (I'm still only including the first section with code), I get "The name 'lbl_phone' does not exist in the current context" because the code behind has a routine that references this screen field which IS in the include file. What is a better way to go about this?
Edit (as it is now, hope I cut it down properly here, builds good now but error in browser):
file ola.aspx
<%# Page Language="C#" AutoEventWireup="true" Inherits="_ola" Codebehind="ola.aspx.cs" %>
<%# Register TagPrefix="section" TagName="account" Src="account.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><body><form id="ola_form1" runat="server">
<section:account id="section_account" runat="server" />
</form></body></html>
file: ola.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
public partial class _ola : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lbl_phone.Text = "000-000-1234";
}
}
file: account.ascx
<%# Control Language="C#" ClassName="_ola" CodeBehind="account.ascx.cs" %>
Call us at <asp:Label ID="lbl_phone" runat="server" Text="lbl_phone"></asp:Label>
At runtime in the browser, I get: Object reference not set to an instance of an object. (for the line with lbl_phone.Text = ...)

Don't use server side includes. They don't work well in the ASP.NET architecture.
Instead, embed reusable stuff into .ASCX user controls. MSDN How To article.
ASCX File MyControl.ascx
<% # Control Language="C#" ClassName="Spinner" %>
<table>
<tr><th>Column 1</th><th>Column 2</th></tr>
<tr><td>Some content</td><td>Some more content</td></tr>
</table>
Then this can be embedded on as many ASPX pages as you like.
<%# Register TagPrefix="uc" TagName="MyControlName" Src="~\Controls\MyControl.ascx" %>
<uc:MyControlName ID="MyControl1" runat="server" />
Edit- You should make these ASCX files as portable as possible in your application so that they're self contained units. There shouldn't be much of a need for your ASPX code behind to reference a control contained in the ASCX file. You can do code behinds for user controls (MyControl.ascx.cs or MyControl.ascx.vb) if necessary, or you can use a script block to embed the code directly in the ASCX page.
.NET Framework will not follow server side includes and so it won't compile properly if you try to reference a control that is being included via SSI. But you could potentially reference a control in an ASCX file. For example...
MyAdvancedControl.ascx
<% # Control Language="C#" ClassName="Spinner" %>
<asp:Label runat="server" id="lbl_phone" />
<script runat="server">
public Label Lbl_phone {get {return lbl_phone;}}
</script>
MyPage.aspx
<%# Register TagPrefix="uc" TagName="MyAdvancedControlName" Src="~\Controls\MyAdvancedControl.ascx" %>
<uc:MyAdvancedControlName ID="MyControl2" runat="server" />
MyPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
MyControl2.Lbl_phone.Text="Galaxy S4";
}

Controls defined as part of a Page or Master Page or User Control are private by default. To get around that, add a public property that exposes it. Put this property in account.ascx.cs
public Label lbl_phone_public_version{
get {
return lbl_phone;
}
}
When you try to reference lbl_phone from code on ola.aspxcs, you have to do it through the parent control, like this:
protected void Page_Load(object sender, EventArgs e)
{
section_account.lbl_phone_public_version.Text = "000-000-1234";
}

Related

NullReferenceException in usercontrol from class library, but not from web project

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.

Accessing Properties of the code-behind from aspx page in web application project

I have a property x in my code-behind file in a web application project. It gives me compile time error while it does not give any error in website project
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="test.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%= x2 %>
<%# x2 %>
</div>
</form>
</body>
</html>
Codebehind file :
using System;
namespace test
{public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
It is working fine when I use the same technique in my website project
Kindly help
Thanks
Check if there are any other errors in your code behind WebForm1.aspx.cs.
like missing reference or any other compile error.
This works for me. Are you sure there's a value in ViewState? On form load add a value to test.
ViewState["FormMode"] = "ViewApplicant";
lblMessage.DataBind();
Also make sure the label has some text to view. It could be showing but is empty.
<asp:Label ID="lblMessage" runat="server"
Text="some text"
Visible='<%# FormMode == "View" || FormMode == "ViewApplicant" %>'>
</asp:Label>
Your code is perfectly okay. While accessing the variable/property initillay, the red underline may appear under the expression indicating the compile error, but when you build the solution, there should be no error.
Your original post has changed so here's a new answer.
Try removing the namespace from the code behind and change 'inherits' on the page to Inherits="WebForm1".
page directive:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="WebForm1" %>
code behind:
using System;
public partial class WebForm1 : System.Web.UI.Page
{
public int x2 = 2;
protected void Page_Load(object sender, EventArgs e)
{
}
}
'Inherits' must match the partial class name.

How to create multiple code behind file for .aspx page

I have a situation where I need to create multiple code behind files for a single .aspx page in c# asp.net. Actually I have a web form on that huge coding is done and I need multiple developer's working on it simultaneously. How can I achieve the same?
Here is the code snippet I tried
Class 1 MyPartialClass.cs
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void PrintText(string pt)
{
Response.Write(pt);
//lblTest.Text = pt; //Can not access this label in partial class.
}
}
}
Class 2 which is Default.aspx.cs
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PrintText("Hello World");
}
}
}
and my HTML source
<%# Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblTest" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
ASP.NET will always generate your code behind files as partial classes
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
You can therefor separate the code behind in different files, if you pertain the definition as partial and keep the class under the same namespace.
Edit : The Web Project
//Default.aspx
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Content>
//Default.aspx.cs
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PrintText("Hello World");
}
}
}
//MyPartialClass.cs
namespace WebApplication1
{
public partial class Default
{
protected void PrintText(string pt)
{
Response.Write(pt);
lblTest.Text = pt; //lblTest is accessible here
}
}
}
I haven't modified any other generated files. One thing I like to mention is that the Default.aspx.cs file that has been generated was generated with the class name "_Default". I have changed it to Default and Visual Studio refactored the change across all files that contain a definition for that class, except the Default.aspx file, where I had to manually modifiy from Inherits="WebApplication1._Default" to Inherits="WebApplication1.Default".
Edit 2:
I kept searching the internet, and according to http://codeverge.com/asp.net.web-forms/partial-classes-for-code-behind/371053, what you are trying to do is impossible. Same idea is detailed at http://codeverge.com/asp.net.web-forms/using-partial-classes-to-have-multiple-code/377575
If possible, consider converting from Web Site to Web Application, which supports what you are trying to achieve. Here is a walkthrough on how to perform this conversion: http://msdn.microsoft.com/en-us/library/vstudio/aa983476(v=vs.100).aspx
You need to set up a source safe server in order to that, like Team Foundation Server.
You can easily just add partial classes to your code.
http://www.codeproject.com/Articles/709273/Partial-Classes-in-Csharp-With-Real-Example

ASP.NET Web Application not allowing Option for Controls in Default.aspx.cs file, why?

Using Microsoft Visual Web Developer 2008 here with a ASP.NET Web Application created by default.
I create a label control with ID="Label1" control in designer view within the Default.aspx file, than I go to Default.aspx.cs file and type in Label1.Visible = False; I immediately get Label1 underlined in red, and hovering over this code says that the Control does not exist in the Current Context.
So, how am I supposed to manipulate properties of a control?
Ok, so in the Default.aspx file, there is this:
<asp:Label ID="Label1" runat="server"></asp:Label>
In that same file very first line says this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
In Default.aspx.cs, I have this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IDSS
{
public partial class _Default : System.Web.UI.Page
{
Label1.Visible = False;
}
}
Label1.Visible is not defined in the current context? When I start typing the word Label, I see only "Label" in there, Label1 is not in there at all. If I selected Label and place a dot after it, I get only 2 options: Equals and ReferenceEquals
In Default.aspx.designer.cs there is this:
public partial class _Default {
protected global::System.Web.UI.WebControls.Label Label1;
}
What is the problem here?
Define something like:
<asp:Label ID="Label1" runat="server" />
in you .aspx page. It should automatically create the realated definition in the .designer file. You do not need to manually create the definition in the designer file. If you do, then make sure it is a partial class...for your page.

ASP Hyperlink not found in Site.master.cs?

I have this code in my Site.master :
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!-- ... -->
<AnonymousTemplate>
[ <asp:HyperLink ID="LoginHyperLink" runat="server" EnableViewState="false">Log In</asp:HyperLink> |
<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink> ]
</AnonymousTemplate>
I have this code in my Site.master.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// ...
protected void Page_Load(object sender, EventArgs e)
{
// this is just placeholder for now.
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
}
I'm getting a Compile-Time error in Visual Studio 2010 at the RegisterHyperLink.NavigateUrl saying :
"The name 'RegisterHyperLink' does not exist in the current context."
Not really sure what's up. I've seen this work in non-Master pages, so does this just not work in Masters?
I'd think it would...
It´s a bug, add a new HyperLink in Desing/Source save and try again. Delete the new hyperlink to finish
Ensure that your code-behind is inheriting from System.Web.UI.MasterPage.
Ensure the aspx has the appropriate directive added and that it is spelt right with correct case:
<%# Master Language="C#" CodeFile="Site.master.cs" Inherits="MasterPage" %>
It is because of this AnonymousTemplate. It probably creates new naming container thus is not directly accessible from Page_Load.

Resources