ASP.Net - Can I Access My BasePage Properties From Within Markup? - c#

I want to be able to extend the System.Web.UI.Page class and then easily access those properties from the Markup. I know I can do it from the codebehind, but is it possible from the Markup? Take the following class.
public class MyBasePage : System.Web.UI.Page {
public bool DoesThisWork { get; set; }
}
Then I want to be able to access it from the html markup, perhaps in the #Page directive.
<%#Page Language="C#" DoesThisWork="False" ... %>
Of course the above Page is using the MyBasePage class instead of System.Web.UI.Page.

I think the question is focused on how to use custom properties in the page directive - and the answer depends on whether you're using a Web Application project or Web Site.
For a Web Site, you need to assign CodeFileBaseClass to a non-partial class (under App_Code or in an external assembly). For Web Application, setting the Inherits directive should suffice.

Yes. You can access the base page's properties from markup, just as you can any other property on the page.
For example:
this is a link

Not directly from the HTML, as in your example, but you can do this:
<td>Result: <%=DoesThisWork.ToString()%></td>

Related

Make sure that the class defined in this code file matches the 'inherits' attribute

I have a page in which page class is inherited with BasePage class like this
public partial class Content_Document_DocumentGuideline : BasePage
so I am able to access BasePage class information like below
int accessPermission = this.AccessPermission;
Now I have a usercontrol in this page.
I wanted to access this base page information in that user control also.
So if I try to inherit this BasePage class in my usercontrol like this
public partial class Content_Document_GuideLinesList : BasePage
I am getting following error:
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).
So if I want to user BasePage class information from usercontrol, what would be the approach?
You should be able to access the members of BasePage from within the user control so long as you only ever use it on pages that inherit from BasePage. You may want to add a property to your user control for this.
public BasePage BasePage
{
get
{
return Page as BasePage;
}
}
Be aware this property will return null if you use the control on a page that does not inherit from BasePage. You may need to make some members of BasePage public if they are not already.
Inheritance is an link between a class that is another class. In your example, A Control is not a BasePage. It is contained in a Page. Think of a Page as the main outside Control container. This page could be a good start to learn. It deals with the difference between inheritance an composition.
About your specific question, there is, in Control class, a property that lets you access the Page that is currently containing it: look at the msdn documentation.

renaming code behind class in VS2010, does not change inherits attribute in .aspx form

when using auto-rename functionality of vs2010 for renaming a code behind class, this does not change automatically the inherits attribute in the in the .aspx form? at least not in vs2010).
Example: if you rename "Error" class to "ErrorLs" this would lead to errors not caught at compile time, due to that Inherits attribute in page tag was not automatically changed.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Error.aspx.cs" Inherits="ABC.Error" %>
The Error.aspx.cs after renaming Error class:
namespace ABC
{
public partial class ErrorLs : Page
{
...
}
}
Since this a common task performed by using VS IDE, does anybody knows the reason why is not by default set to change the Inherits attribute either, I am expecting too much :)?
here I found a link with the same question but no answer from Microsoft Team:
https://connect.microsoft.com/VisualStudio/feedback/details/664505/renaming-partial-classes-via-refactor-rename-should-change-inherits-directive
You will have to change inherits attribute manually unfortunately.

Access Web Methods from multiple aspx pages

I have a user control which is used in multiple(4 to be exact) aspx pages. This usercontrol have a couple of Get/Save webmethods. Right now, I placed all the webmethods in one aspx page and kept calling the same ones from my javascript. I would like to place them in a central location that all the aspx pages can see, but not sure how/where. Any suggestions please?
Edit:
I know the WebMethods should be a part of a class inherited from 'System.Web.UI.Page'. Is there a better place that I can move these methods to, where js can call from.
try to creating Generic Handler (.ahx) and put all your code there.
or try to creating base page, where the base page inherited with the all the aspx pages
in your aspx page :
public partial class RekapDocumentView : based.PageBase
{
}
in your new class :
public class PageBase : System.Web.UI.Page
{
//your webmethods
}
perhaps this can help
If you try to create a class, VS will ask you if you wont to create a folder for it. All common classes should go in App_code folder. Then you can move your method in that class and reference them from the pages.
How about creating a web service which implements web methods that your user control needs to work properly.
Here is a MSDN article on this topic: http://msdn.microsoft.com/en-us/library/bb515101(v=vs.90).aspx
Hope this helps!
Regards,
Uros

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.

extending asp.net control in the website project

when I extend an asp.net control and place the extended control class in, say, Applicaton_code
(without specifying the namespace) how do i register the control to use it on a webpage?
what assembly name and namespace should be specified?
use :
<%#Register TagPrefix="local" Assembly="App_Code" Namespace="Controls" %>
Also, you HAVE to defines a namespace where to put your controls (from memory, when adding class to App_code, no namespace is generated by default).
namespace Controls {
public class control1 : WebControl {
}
}
and then , in the aspx file
<local:control1 runat="server", id="youreluckyitworks" />

Categories