473,411 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Common functionality across pages, what's the best solution?

Hi
I've created a asp.net 2.0 page with an Page_Init eventhandler that I want
to share among multiple pages, what's the best approach for this? Should I
inherit from the page, or is it better to create a library with the
eventhandler code and insert a call to the library on Page_Init on all pages
that should implement this feature?
I would also like to be able to create maste pages that support the same
functionality.

Kind Regards,
Allan Ebdrup
Jan 13 '06 #1
3 1146
Dan
Its difficult to advise without knowing what your pageinit is doing.

For example if you are just setting a load of variables to starting values
then there are a few ways, you could store these contants in an xml file and
read those vars in on application start?

Can you provide some more info? Maybe some code of the pageinit method.

--
Dan
"Allan Ebdrup" <co****@ofir.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
Hi
I've created a asp.net 2.0 page with an Page_Init eventhandler that I want
to share among multiple pages, what's the best approach for this? Should I
inherit from the page, or is it better to create a library with the
eventhandler code and insert a call to the library on Page_Init on all
pages that should implement this feature?
I would also like to be able to create maste pages that support the same
functionality.

Kind Regards,
Allan Ebdrup

Jan 13 '06 #2
I would put the handler in a class in the app_code folder and use a
delegate in the page_init handlers on the page to call the shared
handler function

HTH

-----------------------------------------
David Gray
ASP.NET/C# Developer/Architect (MCAD.NET)

On Fri, 13 Jan 2006 11:03:18 +0100, "Allan Ebdrup" <co****@ofir.com>
wrote:
Hi
I've created a asp.net 2.0 page with an Page_Init eventhandler that I want
to share among multiple pages, what's the best approach for this? Should I
inherit from the page, or is it better to create a library with the
eventhandler code and insert a call to the library on Page_Init on all pages
that should implement this feature?
I would also like to be able to create maste pages that support the same
functionality.

Kind Regards,
Allan Ebdrup

Jan 13 '06 #3
The page Init swaps some controls if there is a special control defined
instead:
---
protected void Page_Init(object sender, EventArgs e)
{
int intPartner = System.Convert.ToInt32(Request["partner"]);
if (intPartner > 0)
{
CheckControlsForSwap(intPartner, Page.Controls);
}
}
protected void CheckControlsForSwap(int intPartner, ControlCollection
controlCollection)
{
foreach (Control c in controlCollection)
{
//Response.Write("c.GetType:" + c.GetType().ToString() + "<br>");
if (c.GetType().ToString().Substring(0, 13) == "ASP.controls_")
{
/* convert a string like "ASP.controls_default_dynamicload_ascx" to:
* "controls/partner/[partnerid]/dynamicload.ascx"
*/
string strControlPath = c.GetType().ToString();
//check that this is not already a partner specific control
if (strControlPath.IndexOf(intPartner.ToString()) == -1)
{
strControlPath = strControlPath.Replace("ASP.", "");
strControlPath = strControlPath.Replace("_ascx", ".ascx");
strControlPath = strControlPath.Replace("default", "partner/" +
intPartner.ToString());
strControlPath = strControlPath.Replace("_", "/");
//load partner control
Control partnerControl = null;
bool blnLoaded = true;
try
{
partnerControl = Page.LoadControl(strControlPath);
}
catch (System.Web.HttpException)
{
blnLoaded = false;
}
if (blnLoaded)
{
//we have loaded a partner specific control and want to swap it with
hte default control
int intControlIndex = controlCollection.IndexOf(c);
//remove the default control
controlCollection.Remove(c);
//add partner control
controlCollection.AddAt(intControlIndex, partnerControl);
//call CheckControlsForSwap on the same collection again to swap other
controls in this collection
CheckControlsForSwap(intPartner, controlCollection);
//return because the enumerater can't continue when we've added and
removed controls from the collection
return;
}
}
}
if (c.HasControls()) CheckControlsForSwap(intPartner, c.Controls);
}
}
---

This works fine when I run it directly in a page, but when using a maste
page the Request["partner"] is empty even though the master page contains a
dropdownlist with the ID "partner" that isn't empty.

Kind Regards,
Allan Ebdrup
Jan 13 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Al Franz | last post by:
Want to create a template that will have Top, Side and Bottom common HTML code for all pages in a directory. What is the best way to do this, using CSS, Frames or something else. Any chance...
7
by: J Smithers | last post by:
I have several ASPX pages (with code-behind logic) that I reuse amongst many Web sites on the same production server. Currently each Web site has its own copy of these aspx pages. I was thinking...
1
by: ngodugu | last post by:
Hi All, Is there any way to use a common login page across multiple web applications? I have tried couple of things that were suggested on MSDN but they did not work. ...
3
by: Garth17 | last post by:
I'm trying to figure out a solution for sharing common properties and methods in all me .aspx and .ascx pages. In classic ASP I would use include directives. So far I have made 2 base classes...
5
by: wrecker | last post by:
Hi all, I have a few common methods that I need to use at different points in my web application. I'm wondering where the best place would be to put these? I think that I have three options. ...
6
by: Joseph Geretz | last post by:
I have the following class which I am serializing and passing back and forth between my Web Service application and the client. public class Token : SoapHeader { public string SID; public...
4
by: | last post by:
I have learned about compartmentalizing my code base using Class Libraries. I have my common code such as my ORM framework broken out into their own Class Libraries, which are referenced as...
3
by: clintonG | last post by:
Why don't we see more WebParts being used on the web? SharePoint gets all the fun? And what are the prevailing opinions be they what they may about the use of WebParts rather than WPF when the...
6
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.