简体   繁体   中英

Re-using another page section

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM