extending asp.net control in the website project - c#

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" />

Related

Custom Control Class Unknown In My Project

I made a custom control in asp.net web form.
I have a master page, my custom control is in the master page. In the aspx.cs file I tried to get the control from the master page.
This is the code:
if (Master.FindControl("Navbar1") != null)
((Controls_Navbar)Master.FindControl("Navbar1")).NavTitle = "Example";
The class:
public partial class Controls_Navbar : System.Web.UI.UserControl
It gave me an error: Compilation Error, Why?
Check the namespace of the control class, master class and page class and make sure they match.

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.

Can I use parent(web page) property in web user control?

How can I use parent(web page) property in web user control?
One more question:- How can I access the shared class(i.e. class in App_Code folder) property in web user control.
Thanks
You can use this.Page in asp.net user control to refer to page and it will always give you page in which that control is added.
You can access any public class declared in App_code folder directly in any user control without problem. Be careful of namespace and make sure to compile your project if you are having issues to access the class.
You could but I am not sure it would be a clean / full OOP approach, how about setting a public property or calling a public method of your user control from the page passing to it the value you need to use in the control?
this because the page hosting the control should be generic and is the page which contains the control not the other way round.
If this does not fit you, then you can take the control's Page property and cast it to the class of your page then you will be able to access its property but this will make your control specific instead of generic and it will only work when the control is hosted in pages of that exact type/class.
You have to mark the property as Public.
var myVar = ((ParentPageClass)this.Page).YourProperty;
To access the shared class you have to specify the namespace of that class:
YourProject.SomeNamespace.YourClass
or to include the namespace in your .ascx.cs file
using YourProject.SomeNamespace;
It's a cleaner aproach to pass the parameter to the user control from the parent page.

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.

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

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>

Categories