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.
Related
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.
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
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" />
public partial class MasterPages_Main : System.Web.UI.MasterPage
{
public LoggedInUser ThisUser;
This is in my master page, and my user control is running on the page. In my user control however, I can't seem to reference ThisUser, do I have to pass it in to the control as a parameter? Is there any way to directly reference it?
Absolutely! Take a look at the methods and properties of an asp.net page. I've done similiar using:
HtmlForm mainform = (HtmlForm)Master.FindControl("form1");
Where the important part is the Master.FindControl();
This is a part of the Page class. Page.Master will get you to the master page of the current page.
I'm sure that there's some way to avoid passing an instance of LoggedInUser to your control, however it's probably best to pass it as a parameter as doing so promotes reuse of the control because it will be more loosely coupled.
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>