Add codebehind file to asp.net page - c#

I got a file, let's call it MyPage.aspx. It has no codebehind and I need to add a codebehind file to it. Here's what I've done:
created MyPage.aspx.cs and included it in the corresponding namespace
Added the following code to the Page tag of MyPage.aspx: AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyApp.MyPage"
Still, it doesn't work. I cannot access runat=server elements from the codebehind. Strangely, I also noticed one more thing:
The definition of the class in MyPage.aspx.cs is as follows:
public partial class MyPage : System.Web.UI.Page
Normally, both words MyPage and Page in this line would be green. However, only the word MyPage is green and the word Page is still black.
I'm kinda stuck with this, any help would be appreciated.

in your aspx page you need to regrence the code behind page
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs" Inherits="MyPage" %>
after you do that, you make a new class and name it MyPage.aspx.cs, just make sure that its in the same directory as your aspx page.
also give the class name inside the code behind _MyPAge
it should look like this after you reference your components
public partial class _MyPage : System.Web.UI.Page
Hope this helps.

Related

Rename User Control in asp.net

I have created User control named MUSD.ASCX
And I changed the name of code-behind class to ctlAllBranchRouteMUSD.
When I try to use it, the IDE thinks that it doesn't exist and the reference doesn't have any impact in codes
faced with a similar problem recently, since I couldn't work with a designer file, I had to reopen the same solution in VS 2017 to complete the change in aspx file.
Instead of renaming it i suggest that you create a new ascx file and copy the code into there.
However if you rename it you need to change the following files
*.ascx
*.ascx.cs
*.ascx.designer.cs (this one is supposed to update automatically)
*.ascx:
from
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="A.ascx.cs" Inherits="ObscriptSearch.A" %>
to
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="B.ascx.cs" Inherits="ObscriptSearch.B" %>
*.ascx.cs:
from
public partial class A: System.Web.UI.UserControl
to
public partial class B: System.Web.UI.UserControl
*ascx.designer.cs:
from
public partial class A
to
public partial class B
You edited both the filename and class in code? If not, try to change.
Be aware you changed also the extension, might be a problem :)

Get and cast Masterpage UserControl from Content Page to access specific UC Property

I have a MasterPage (MyBoxx.Master) referencing 2 usercontrols :
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MyBoxx.master.cs" Inherits="MyBoxxMaster" %>
<%# Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>
<%# Register TagPrefix="uc1" TagName="Footer" Src="Footer.ascx" %>
My user control "Header" contains among other things a searchbox. I want to hide this searchbox when visiting some pages. Therefore I added a boolean property to my user control and use this property when rendering the usercontrol to determinate whether to display the search box or not :
public partial class uxHeader : System.Web.UI.UserControl
{
bool _showSearch = true;
public bool ShowSearch
{
get { return _showSearch; }
set { _showSearch = value; }
}
[...]
protected void Page_Load(object sender, EventArgs e)
{
[...]
searchBox.Visible = _showSearch;
}
}
I then try to access this "ShowSearch" Property from the content page :
((uxHeader)Page.Master.FindControl("Header1")).ShowSearch = false;
Problem is I get the following error when trying to compile :
Error 15 The type or namespace name 'uxHeader' could not be found (are you missing a using directive or an assembly reference?)
The thing is I'm sure I got it to work and compile at some point as it works on the previously released production version. But now I'm doing a change to something else in the same site, and can't compile anymore.
From various post on SO, I tried adding the following lines to my content page aspx :
<%# MasterType VirtualPath="~/MyBoxx.master"%>
<%# Reference VirtualPath="~/MyBoxx.master" %>
Without any success ! I saw also some answers about the page Lifecycle, but this can't be the problem here as I'm getting an error on compilation, not a bug upon execution.
If anyone has any advice on how I can fix this for good, I would grandly appreciate.
Thanks !
Well, I found several working solutions... and I think I understood how/why it worked earlier
1) it seems that compilation has a role to play in this. If I comment the line, compile the site, and then try to add the line again, the type uxHeader is "available" in VS and I can compile the site back again with the line uncommented...
2) As first solution is obviously not a long-term solution, I found that referencing the user-control (without actually using it of course) in the content page aspx would do the trick :
<%# Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>
3) I also tried this one, which I find the cleanest...
In the master page, expose a public property :
public uxHeader PageHeader
{
get
{
return Header1;//Header1 is the id of the userControl dropped in masterpage
}
}
In the content page aspx, I then put :
<%# MasterType VirtualPath="~/DBoxx.master"%>
then, still in the content page, but in the codebehind, and after a compilation of the site, I can use :
this.Master.PageHeader.ShowSearch = false;
Hope this will help the ones searching for help on the subject in the future. I see this is a recurent question
Depending on how you have your User Control coded you may or may not be able to access all it's properties/methods when exposing it to the master page as a master page property..
Here is a solution that works:
In your master page you need to register your user control (.ascx) and place it on the master within the form tag.
Register the User Control
<%# Register Src="~/Controls/MyUserControl.ascx" TagPrefix="uc" TagName="MyUserControl" %>
Add the User Control to the Master Page
<form id="frmMain" runat="server">
<uc:MyUserControl runat="server" ID="ucMyUserControl" />
<div id="main-wrapper">
<div id="main">...
Now for the content page, you must create a reference in each of the content pages that use the master page for which you want to use the control.
Add the Reference in the content Page
<%# Reference Control="~/Controls/MyUserControl.ascx" %>
Now you can setup a public variable at the page level and access it's properties/methods
Partial Class MyPage
Inherits System.Web.UI.Page
Public usrCtrl As MyUserControl
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Master.FindControl("ucMyUserControl") IsNot Nothing Then
usrCtrl = CType(Master.FindControl("ucMyUserControl"), MyUserControl)
usrCtrl.ExecMyMethod()
End If
...

How to put Code-Behind Class of UserControl.ascx in another Library

I have one UserControl.ascx and want this ascx use (reference) the code-behind that is in another library (project). So in the #control directive i do the following
<%# Control Language="C#" AutoEventWireup="true" CodeFile="..\CodeBehinds\ucBehind1.cs" %>
but when a run the page i see the error
Parser Error Message: Cannot use a leading .. to exit above the top directory.
Is there a way to achieve it?
GitHub example Project
In your example, remove the inherits from UserControl1.ascx
In usercontrol1.ascx.cs, uncomment all your code.
Change
public partial class UserControl1 : System.Web.UI.UserControl
To
public partial class UserControl1 : otherClassInAnotherProject
otherClassInAnotherProject should in the end inherit from System.Web.UI.UserControl

Can a single ascx page use multiple codefile which are partial classes

Can we link up two partial classes to a single ascx page.
I have gone through some of the links but could not find a suitable solution for the same.
Suppose for example I am having files such as
Collection.ascx.cs and Collection.Search.cs
with Collection.ascx
I am having events like button click event, could it be placed in the partial class, as doing so would make the method unavailable.
My intention is to handle button click events and the dropdown change events in the partial class.
Yes, In WebApplication, It is a default nature for a .aspx (web page) and .ascx (user control).
These are by default comes with two .cs files, separating code in partial classes (filename.aspx.designer.cs and filename.aspx.cs with same partial class)
You can also add more .cs files to extend these partial class definition.
But in website, it will not work, for aspx or ascx.
Yes, the C# compiler rules for partial classes don't change because its ASP.NET. Just make sure your namespace and class name matches, the source file doesn't have to be a child of the ascx file in the Solution Explorer, but if you really want it to you can edit the project file and add the appropriate depends-on xml segment.
The answer locates at default.aspx 1st line:
Language="C#" CodeBehind="Default.aspx.cs" ...
will set every partial class working.
Instead,
Language="C#" CodeFile="Default.aspx.cs" ...
will lead partial class to wrong.
However, in vs2017 old-style-webForm you can only use CodeFile, so you have to try another way: just create a file ..\WebF8\App_Code\Class3.cs with content :
public static class Class3 {
public static int gg3=33;
}
then you can see Class3.gg3 in default.aspx.cs correctly.
Be careful, this Class3.cs file must be in \App_Code folder.

Inherit problem in asp.net

in my web application i copy and paste the code from other site to in my page also the source code starting form
when i run the application it is giving the error like this
Server Error in '/DomainIV' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
Source Error:
Line 1: using System;
Line 2: using System.Data;
Line 3: using System.Web;
Source File: c:\Inetpub\wwwroot\DomainIV\WhoIs.aspx.cs Line: 1
this is my problem help me thank u.
In your aspx page there will be the following at the top:
<%# Page Language="C#" MasterPageFile="~/Test.Master" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TestApp.Test" Title="Untitled Page" %>
In your .cs file your class will be defined like:
namespace TestApp
{
public partial class Test : System.Web.UI.Page
{
Make sure the inherits property in the aspx page matches the class definition in the .cs file. In the example above it is 'TestApp.Test' in the inherits property and the class must have the same namespace and classname TestApp and Test.
You probably copied the whole contents of one of the files and now the two pieces no longer match up.
My advice, create the new page with the same file name as the source.
Then copy and paste the codes into the new pages (APSX and code-behind). Works for me everytime.
In aspx page, there will be
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
And in .cs page
public partial class _Default : System.Web.UI.Page{}
Inherits attribute and the class name both should be same.
Hope this resolves the issue.
In your ASPX file, make sure the #Page declaration has an Inherits attribute that matches the full class name of the class in your code behind file. This declaration is usually the first line of the ASPX file. Also make sure that the class in your code behind file inherits System.Web.UI.Page and that it is not a sealed class.

Categories