I have multiple usercontrols in my asp.net website ,I define for every user control a single name space like below:
Add document user control:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="AddDocument.ascx.cs" Inherits="DMS.UI.Users.Controls.AddDocument" %>
C# code behind:
namespace DMS.UI.Users.Controls
{
public partial class AddDocument : UserControl
{
}
}
AddSubversion Usercontrol:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="AddSubversion.ascx.cs" Inherits="DMS.UI.Users.Controls.AddSubversion" %>
c# code behind:
namespace DMS.UI.Users.Controls
{
public partial class AddSubversion : UserControl
{
}
}
now I need to work with other usercontrols in one user control, like this:
AddSubversion ucAddSubversion = (AddSubversion)this.Parent.Parent.FindControl("ucAddSubversion");
but program not recognize that all usercontrols are in one namespace.Why and how to solve this ?
thanks a lot
Define namespaces for usercontrols are different from namespaces for classes. if we want to use one usercontrol in another , only way is to Register that user control in another user control.even if two user controls were in one namespace ,without Register visual studio (2010) we can not access that user controls and its public properties...
<%# Register Src="AddSubversion.ascx" TagName="AddSubversion" TagPrefix="uc5" %>
Related
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 :)
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.
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
...
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 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.