Re-using another page section - c#

I have created a web page that I use as a small dashboard to hold issue or no issue. It works great. The page uses an .aspx and .aspx.cs. I would like to be able to reuse the information on this page on other pages. My site already uses master pages and I have not been able to find an easy way to include this information.
How can I use an include from a page that has coding in the code behind easily?

Typically you use Web User Controls for this.
Web User Controls allow you to package up other controls into one that you can drop onto multiple pages. They are great for common UI items such as address entries, dashboards, etc. Basically anything that needs to be the same across multiple pages.

At the risk of seeming very obvious - do you mean usercontrols. These will allow you to reuse chunks of functionality across your site.

I guess this question falls into two categories: User Controls, and Code Reuse. Not sure which one you are after.
User Controls
If you are talking about the controls on your page you will want to create a common user control.
Code Reuse
You need to create a common class (whether it is static or not depends on how you intend to use it) and define functions within that class.
For instance, lets say you have a page that you want to print "Hello World!" on any aspx/.cs page.
You could do this
public static class MyClass
{
public string PrintHelloWorld()
{
return "Hello World!";
}
}
Then you call it from any of your pages like so:
MyClass.PrintHelloWorld();

Right click on the project > Add New Item...
Select User Control (.ascx)
Put your markup & code behind there.
Then you add that control in any other page (includding other controls [although I wouldn't recommend that])

It sounds like you may want to create an ascx User Control.
http://msdn.microsoft.com/en-us/library/ie/2x6sx01c.aspx

Related

ASP.Net Class References a Control from ASPX Form

So, I've seen many discussions that are in this area, but it seems like they are mostly discussing Windows Forms, or they don't get around to answering this specific scenario enough to point me in the right direction.
Exactly what I need to do (Generic Example):
HTML (fragment.aspx)
<div id="html_fragment_1" runat="server">Contents</div>
<div id="html_fragment_2" runat="server">Contents</div>
...
Code Behind (fragment.aspx.cs)
Fragments fragment = new Fragments();
fragment.return_fragment( AN INSTANCE THAT REFERS TO html_fragment_N );
Class (Fragments.cs)
public void return_fragment ( AN INSTANCE THAT REFERS TO html_fragment_N ) {
string html = INSTANCE.html_fragment_N;
// DO SOMETHING WITH html
HttpContext.Current.Response.Write(html);
}
The reason I need to do this is that every aspx form on my site needs to be manipulated in the same way by Fragments.return_fragment(), where the content for several DIVs need to be read from the Form and arranged into an XElement to be returned.
Instead of doing the manipulation in the CodeBehind for every page, I'd rather have each Form use Fragments.return_fragment() so that it saves effort implementing a new Form page, and the code can be changed easily without having to change it in each Form.
Looks like you want your app's Global.asax file:
http://msdn.microsoft.com/en-us/library/1xaas8a2(v=vs.71).aspx
Look at your app's page events here as a place to specify things that should happen on every page request.

User-control based program

I am trying to create a new Windows Store-app, but before creating too many user-controls, I will like to know something that I have no luck googling
If my program only have one page, and the code thereby is based by user-controls, could it then be possible to use GoBack, or should I implement my own way to do that?
And if I need my own way to override GoBack, can I then override GoBack in the Page, or should I override it in general for the whole app?
If you're going to keep the same instance of your page and just switch the user control that is displayed then you'll need to reimplement the existing navigation framework. For it to work you need to be calling Frame.Navigate() to switch between different page instances.
To do that in your case you could still only have a single page class but instead of just replacing the user control inside the same instance you could call Frame.Navigate() with the same page class and then inside it display the correct user control based on the parameter that you pass in. In this case you can use the existing navigation framework to navigate between the page instances.

How to use a web user control in code only (no rendering)?

I have a user control which has panels and other gui controls. It's used like any user control in rendering a web page. However I have a need to use a new instance of it and use it in code only without rendering it as it has business logic which I want to use. However it gets object null reference errors during run whenever a gui control is referenced in code, for example: pnlSomePanel.Visible = true;
How do I use a user control in code only (without it being rendered)? Using .NET 4.0.
Technically, you should just be able to create a new instance of the code-behind class (assuming an asp.net web application and not a web site).
However, if there is business logic embedded in that user control, I would suggest moving it into a separate class or exposing the business logic as a static method with an appropriate set of parameters.
How about this...
Add a reference to your module on the page.
<%# Reference Control="~/modules/MyModule.ascx" %>
Then, you can create an instance of that module in your code behind using the class name of the module.
modules_MyModule ctrl = LoadControl("~/modules/MyModule.ascx") as modules_MyModule;
ctrl.SomeMethod();
The next step would be to add it to a parent container, but if you skip that step, you should still have a functional object in the code behind. Not totally sure if it will work, but in theory it should.

Navgation between XAML pages

I have a simple Silverlight application that consists of four pages (XAMLs).
Navigation is done by calling:
//from XamlPageA
this.Content = new XamlPageB();
Is this the right way. I need to have the entries in Browser history so that users can go page to the previous page(s). How can I do it.
You are bypassing the navigation system completely by setting content manually. You would have to implement updating the browser history yourself if you do it that way (certainly possible, but quite tedious).
A simpler approach is to generate a "Silverlight Business Application" project and see how the page navigation is simply handled with hyperlink buttons. All the browser history plumbing is done for you as is the mapping from URL to views.
e.g. A button with NavigateUri="/Home" will cause a view named Home.xaml to load into the navigation:Frame of the MainPage window.
if you look into the navigation:Frame element of MainPage.xaml, you will see a number of UriMapping entries like this:
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
They provide the pattern matching to convert from URLs to views.
Hope this helps your project.

ASP: Extend control (ASCX) and access base markup file from subclass code

I'm building form validation controls for our C# ASP application. The bulk of the work is handled by a BaseValidator control (subclassing System.Web.UI.UserControl), which also has the markup for the validation output. This is then extended by subcontrols like PasswordValidator, that provides the Validate method and any extra fields needed by that validator control.
(The end goal is to have controls like <uc1:PasswordValidator ControlId="txtPassword" /> which we can plop into any form with minimum duplication.)
However, PasswordValidator.ascx.cs cannot access the form elements defined in BaseValidator.ascx; the only way I've found to do so is to duplicate the markup in each subcontrol's *.ascx file. How can I extend BaseValidator.ascx.cs and access BaseValidator.ascx's markup in the subclass?
I'm pretty sure you'll have to create Server Controls to accomplish this. Meaning, you'll need to generate the outputted Markup from code in the control rather than in the .ascx file.
If you have a true baseclass for your BaseValidator control which your PasswordValidator extends, then any markup/controls in the baseclass should be available through protected properties. I would even go so far as to argue that a true base should not have an ascx portion. The base class should provide methods and properties that expose controls to built on the fly (probably during Page_Init to maintain viewstate).
If you have 2 separate controls on the same page, your parent page can be modified to provide brokerage methods to allow such communication.
If you have the PasswordValidator and you just need the controls/markup, you can use the LoadControl method to create an instance of the BaseControl in memory, access its controls/markup programmatically and either add it or destroy it depending on what you want to do with it.
Barring any of that, it would be just as #Shawn said. Server controls.
Thanks for the suggestions.
I solved this using a service and the strategy pattern. There is a central FieldValidator class which provides validation for any user interface. For the web interface, these methods are exposed through a WebService and (through the WebService) a UserControl. There are no issues with accessing the page elements, because there is now only one UserControl class that stands between the form and the WebService.
For example, <uc1:FieldValidator ControlType="Password" ControlToValidate="txtPassword" runat="server" > will plop down fully-functional clientside and serverside validation, with no need for code behind. Great! :)
(I mentioned resolution a while back in a reply to my question, but I can't mark that as answered.)

Categories