Including a file in code behind?

M

Mark Harris

I have an interesting situation that i could not find a resolution for on
google or msdn, it goes as follows:
The web app i have has about 35 pages, each requires the same code in the
page_load (about 62 lines). If i need to change a single part of that
code, is there a way to do it without find/replace over the whole project.
As the code i wish to change (and other sections i may wish to change in
the future) is also in some other parts of the project. Such as below:

if (helper.GetServiceRunning((string)Session["__SelectedService"]))
{
txtStartStop.Text = "Stop";
divRestartServer.Visible = true;
}
else
{
txtStartStop.Text = "Start";
divRestartServer.Visible = false;
}

I need to add a parameter to helper.GetServiceRunning, i was thinking it
would be mighty handy if i could just put this code in a .cs file and
include it at the right point on every page, then any changes/additions
would be dead easy later on.

I'm using Visual Studio .Net 2003 if it will pre-process anything to do
this.

Any ideas/insights would be very good :)

Its pretty frustrating when you need to go and change something =\


- Mark
 
M

Mythran

Sure, create a web user control and put all this code into it. Depending on what
exactly it is you are doing, you may need to put the code into the Initialize
event and not Page_Load. This way, your code is executed prior to the page's
Page_Load event handler.

Hope this helps :)

Mythran
 
M

Mark Harris

Thanks for the reply Mythran,

Not quite what i was after as the code hides/shows several parts of the
menu, binds a DropDownList, adds some javascript to the header, sets the
page title, etc. So something more advanced may be needed.

As much as a i now dislike php, one function from it comes to mind: the
Include() function, or C's ability to define a symbol that is then
replaced with the code by the pre-processor.

Again, thankyou for the reply,
- Mark Harris

Sure, create a web user control and put all this code into it.
Depending on what
exactly it is you are doing, you may need to put the code into the
Initialize
event and not Page_Load. This way, your code is executed prior to the
page's
Page_Load event handler.

Hope this helps :)

Mythran


Mark Harris said:
I have an interesting situation that i could not find a resolution for
on
google or msdn, it goes as follows:
The web app i have has about 35 pages, each requires the same code in
the
page_load (about 62 lines). If i need to change a single part of that
code, is there a way to do it without find/replace over the whole
project.
As the code i wish to change (and other sections i may wish to change in
the future) is also in some other parts of the project. Such as below:

if (helper.GetServiceRunning((string)Session["__SelectedService"]))
{
txtStartStop.Text = "Stop";
divRestartServer.Visible = true;
}
else
{
txtStartStop.Text = "Start";
divRestartServer.Visible = false;
}

I need to add a parameter to helper.GetServiceRunning, i was thinking it
would be mighty handy if i could just put this code in a .cs file and
include it at the right point on every page, then any changes/additions
would be dead easy later on.

I'm using Visual Studio .Net 2003 if it will pre-process anything to do
this.

Any ideas/insights would be very good :)

Its pretty frustrating when you need to go and change something =\


- Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top