Question

I've got a aspx and aspx.cs file with some components. Now I want to reuse parts of that page in another page. My approach would be to pull out the duplicate part into a WebServerControl.

So before I waste more time yahoogling, is that even the right idea and if so, is there a way to use parts of the aspx file rather than doing it tediously in RenderContents with the HtmlTextWriter, WriteBeginTag, WriteAttribute and so on. That looks like a mess for complicated layout and sizeable amounts of controls.

What's the standard?

Was it helpful?

Solution

Depends.

The main driving factor is that if you need to reuse your control in multiple web applications, you should go with a Custom Control (.cs in C#).

Else, if you only intend to reuse your control in one web application, choose a User Control (.ascx).

This MSDN article is a good starting point.

UPDATE (since OP asked further details):

To embed JavaScript for a custom control, a common approach is

var initializeScript = string.Format("MyNamespace.initialize('{0}', {1});", ClientID, myScriptString);
Attributes.Add("onmouseover", initializeScript);

Suggest to write JavaScript code in a js file and not in .cs since the latter is a nightmare to maintain and debug. Hope this helps.

OTHER TIPS

It sounds like what you want to do is bundle the items into a User Control. This will allow you to design the control by using existing .NET controls rather than rendering everything out from scratch.

All you need is to create an ASP.NET Web User Control

Taken from MSDN:

An ASP.NET Web user control is similar to a complete ASP.NET Web page (.aspx file), with both a user interface page and code. You create the user control in much the same way you create an ASP.NET page and then add the markup and child controls that you need. A user control can include code to manipulate its contents like a page can, including performing tasks such as data binding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top