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
Related
I have an entity Profile that I need to access from all aspx and ascx pages in my website. I have created a class "Context" which has the profile property and inherits from System.Web.UI.Page. This worked very well for aspx pages.
I need to apply the same parent to the user controls in my application, my only choice now is to create another Context class "UserControlContext" that inherits from System.Web.UI.UserControls and let the user controls inherit from it.
My problem is that this way i'm duplicating the code for both context pages, how can I use one Context class to let both ascx and aspx inherit from ? What type will that Context class be ?
No, you can't. Your control is either a Page or a Control (or UserControl). They can't share the same base class.
I suggest to move the duplicate code into another class that is called by both Page and Control. You can use the base class Control if you want to in that class, so the code is as generic as possible.
You can inherit only from one class in C# so you can't put the functionality into a common base class for both (as they already inherit from System.Web.UI.Page and System.Web.UI.UserControl). What you can do is to move the common functionality out to some other class and then use that class inside your Context and UserControlContext classes. You would not derive directly from the new class, but you could encapsulate it inside both Context and UserControlContext classes and call its methods from those classes.
When it comes to the question to which extent you can move the code out to the other class, that depends on your code.
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.
Is it possible to make master page get called first instead of the called page - asp.net 4.0. I mean for example i have home page and i have master page. When i called the home page it goes to the home page code behind first then it goes to the master page code behind. Is it possible to call master page code behind first ?
ASP.net 4.0 , C# thank you
No, this is not possible.
The hierarchy is that the master page is actually nested within each content page.
The sequence of events is documented here.
If you have to write common function in each content page.You can avoid t by creating a base class and derive each content pages from that base class instead of the default base class System.Web.UI.Page
Other method as above mentioned write function in global.asax
We are developing a site inside a CMS that pushed files to our production server. If the .apsx pages created share the same code behind file, will this cause a problem?
Why don't you let both pages inherit from the same class?
public class MyPage : System.Web.UI.Page
{
// common logic that both pages should have
protected void Page_Load(object sender, EventArgs e)
{
}
}
public partial class PageA : MyPage
{
// specific logic for page A
}
public partial class PageB : MyPage
{
// specific logic for page B
}
Yes. It is technically possible, but it is not a supported way of using ASP.NET and there will likely be gnarly difficulties with it.
You should use User Controls or AppCode instead.
I would suggest to avoid such design, so each page should has own code behind file.
Also take a look at the following ASP.NET features whcih can simplify sharing of common layout and behaviour across the web pages:
Master pages (.master), basically multiple pages which has the same layout could use a shared master page which provides the common layout and supporting code behind logic. Moreover - You can switch master pages in runtime and master pages could be nested, so Master Page could be placed inside of an other Master Page, this gives you a much of freedom and flexibility to design your web application layout and separate responsibilities as well.
User Controls (.ascx), it worth to separate concerns and split page by a set of controls, for instance, it could be LoginControl.ascx,, AdControl.ascx, SearhcControl.ascx, so you keep a code of page clean and each control provides specific functionality and own layout.
Inheritance, so basically set of pages have the same base class which inherited from the Page class (I would not suggest use this approach massively, one base page it is enough)
You may use other common development techniques like dependency injection to share code across multiple pages, this depends on particular case and business goals which were considered under the hood.
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.