reusable code - Including code in the code behind file

 I have a page of code that instead of putting it in each pags code behind, I would like to be able to jsut include it in the code behind of each page so that it gets executed each time. This way I could jsut edit the one file and th changes woudl be site wide in thepages that have this as an include in the code behind file.

What is the bet way to do that? Any examples?

Thanks

0
belcherman
3/8/2009 7:28:10 AM
asp.net.getting-started 91979 articles. 3 followers. Follow

2 Replies
1079 Views

Similar Articles

[PageSpeed] 4

Hi,

You could create a base class for the code-behind classes (put every code-behind class to derive from this common base class), when changes to the base class would apply to the derived classes.


Thanks,

Teemu Keiski
Finland, EU
0
joteke
3/8/2009 8:29:07 AM

 Hi there, do you have an example of this? LIke say for instance this code below needs to run in each pages code behind on load:



struct Site
{
    public string site_id;
    public string group_id;
    public string consortia_id;
    public string name;
    public string publisher;
};


void LoadGlobals(Object sender, EventArgs e)
{
    SqlConnection conAIP;
    SqlCommand cmdSelect;
    SqlDataReader dtrAccessControl;
    SqlDataReader dtrConsortia;
    SqlDataReader dtrSite;
    SqlDataReader dtrGroup;
    SqlDataReader dtrPublisher;

    G_site.site_id = "";
    G_site.name = "";
    G_site.group_id = "";
    G_site.publisher = "";

    conAIP = new SqlConnection(G_conn_string);

    try
    {
        conAIP.Open();

      stuff here.....................


        conAIP.Close();
    }
 
}

Thanks

0
belcherman
3/8/2009 10:00:57 AM
Reply:

Similar Artilces:

what about the code-behind (code behind)
One MAJOR difference between AJAX.NET and Atlas.NET seems to be that AJAX.NET is able to expose methods in your codebehind to the client. Why doesn't Atlas do this? Or am I mistaken? With AJAX.NET it's just a simple line or two:  Ajax.Utility.RegisterTypeForAjax(GetType(myClass)) <Ajax.AjaxMethod()>Public Function myFunction PLEASE tell me Atlas has something comparable to this. I don't have the time to rewrite all my code-behind methods in javascript or atlas script. All you need to do in Atlas to expose a method on your page (or its code behind class) is to put a [WebMet...

Code Behind or not Code Behind
Ok i am confuse now.... A lot of books are showing pages without code behind (the code is integrated in the .aspx) Is it better to use the code behind or not. Is there any difference between using VB.NET or C#.NET except for the code synthax. Thank you and come again. A lot of samples show inline code just for simplicity.. i personally use code-behind because I like to seperate my code from presentation. As for VB.NET or C#, its your preference.. there is no difference really when its all said and done. From a purely mathematical sense, having everything in a single file would s...

To code or not to code?
The rise of Open Source software adoption has brought with it increased awareness of non-proprietary programming technologies. http://www.itnews.com.au/Feature/4593,to-code-or-not-to-code.aspx -- "Never drive faster than your ANGEL can fly" ...

difference between code inside a <script> tag and code in the code-behind file
What is the difference between code that exists inside the <html><script runat="server" > tag and code that exists in the code-behind file? Thanks, Keith There really isn't much of a difference other than the code inside the <script runat="server"> tag is part of the web form while code in a class file is part of the class file and inherited in the web form. HTH,RyanRyan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question...

Code-behind file code is not working
when i am trying to run application after writing code in code-behind file, it is not working.if i do same work with aspx file it is running properly in browser.like,This aspx file code is running properly.<script language="vb" runat="server">Sub submit_click(sender as object,e as Eventargs)textbox1.text="User Name"End Sub</script><asp:Button id="submit" runat="server" Onclick="submit_click></asp:Button>This code behind file is not runing at all.Any code (not only this programme)is not working when i am trying to write in code-behind file.private sub submit_click(By V...

How do you code a LinkButton from the code-behind file?
Hi, I need to code LinkButtons from the code behind but  myLinkButton.PostBackUrl = "mySite\\subfolder\\thePage.aspx";  Shows up in the broswer as path/mySite//subfolder//thePage.aspx Why is it adding the extra /?  I need a simple fix, thank you.    Hi, try this: myLinkButton.PostBackUrl = "~/subfolder/thePage.aspx"; Any doubt, post your comment.Segundo Serrano P.Ing. Sistemashttp://www.4workgroup.com/Blog: http://dotnet-peru.blogspot.com Add the link button to your page like this... LinkButton link = new LinkButton(); link.T...

Seperating Admin code in code-behind from general code
Hi guys, I just want to know if it would improve the functioning of my site to create two seperate dlls: One of the dll's would contain all the code for the general functioning of the site, and the other dll would contain all of the code for the administration elements of the site. Thanks GP...

Code-Behind versus Code-Inside and Code-Beside
Can someone explain to me how the code gets compiled when you do code-inside (like in webmatrix) as oposed to code-behind? WHen you do the code-behind I know all the code gets compiled at compile-time into a dll assembly. And I think that code-inside gets compiled at run-time. But what if you have a mixture of the two and you have a dll assembly with code-beside modules, how do they get compiled into the assembly? Maybe I'm wrong or confused, can someone clarify? I'm also aware of the Visual Studio 2005 code-beside also, will that change anything? thanks, Frank For a general...

How to register a CSS file to the page from code in the code-behind file?
Hello, all! How to register a CSS file to the page from code in the code-behind file? Thank you.                                                                                  Marcos I a...

Code behind vrs. non code behind in VS.NET 2003
Does anyone know what the advantages and disadvantages are for using code behind vrs. non code behind in Visual Studio.Net 2003? One important difference is that you are able to separate the design of a web site from the code. This makes for ease of maintainablity and much more structured applications. If you ever worked with classic ASP the code-behind is a great invention. sivilianMy Weblogs...

To Code Behind or Not Code Behind, That is the Question...
I have been working in ASP.Net 1.0 for several years.  I have just begun using 2.0 and I am really liking it. I understand how to create pages with code in the page and code behind the page.  I'm just not clear on what is considered the preferred method. I am about to start a new application and I would like to do it the recommended way. Are there any opinions on this already, something I should be reading? Thanks a lot,Chris Hi,  Generally, code behind, or code behind files are recommended, as they make applications easier to debug and rework.  I guess that the thi...

get source code of an iframe in code behind
Hello,I'd like to get the entire source of an iframe (= same as right click-view source) in the code behind.I have tried several ways without success.. So instead of showing 10 pages of non working codes, I'll just hope someone has a good suggestion for me...For info, don't know if it matters for the problem, in my case, the source is displayed fine in the iframe and it is XML. Regards,NNM Validation Complete========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========I'll always mark your post(s) as answer when it is! The problem you are running int...

code behind vs one file code
hi, I realized that there are two ways to create a web page in asp.net . either put allthe code in one page and use the tags to separate the server code from the client code. or use what vs.net offers, code behind aspx and aspx.vb The question is what is better to develop with and what if I have an asp page that was created in vs.net with code behind and I need to make it one file. thanks for your help AlNew to Asp.net depends what you mean by 'better'. that's a personal preference thing. Personally I like to split content from code, but some others find it strange. the codebehin...

Registering Javascript Code file from code-behind???
Hi, Environment of web application is: Windows 2000/VB.NET/ASP.NET/SQL 2000 For small javascripts, we can use RegisterClientScriptBlock method for script registration. But, when it comes bigger scripts and we want to use them across the pages, then this approach is not so neat. I am trying to register the entire javascript code file by RegisterClientScriptBlock(). I know it is possible, but I am not to work it out. Does anybody knows, how to make it work? Some working examples would be nice :) Please advice, Thanks in advance. Pankaj Hoping below example helps you! pleas...

Using App_Code shared code in page code-behind AND code-front
Hi all, I've only just come across what seems to be the 'correct' way of making use of shared code in my .Net sites: I code some classes in a .cs (in my case - i'm using c#) file. I then put them inside namespaces. To make use of the classes in the code-behind of my web pages, I simply add a "using MyNamespace;" line at the top, and then I can make use of a class' contents like so: "MyClass1.MyString1(SomeVariablesToSend)". Now that works fine when working in the codebehind, but what happens when I want to call the class/string from my code-fron...

Web resources about - reusable code - Including code in the code behind file - asp.net.getting-started

70M Facebook Shares To Hit The Market, Including 41.35M From Mark Zuckerberg
... public offering of class-A common stock, with 27,004,761 coming from the company itself and another 42,995,239 from certain stockholders, including ...

Latino Youth Interrupt President Obama Speech to Protest His One Million Deportations, Including Many ...
Presente.org hat eine Notiz mit dem Titel Latino Youth Interrupt President Obama Speech to Protest His One Million Deportations, Including Many ...

Data visualization company SumAll raises $6M, including $500K for charity foundation
Data visualization company SumAll today announced it raised $6 million in a Series A round led by Battery Ventures. The funding agreement includes ...

Twitter Introduces Custom Timelines, Including Developer API
Like Storify, built right into Twitter itself.

Thailand Travel Guide with Offline Maps – tripwolf (including Bangkok, Phi Phi Islands, Chiang Mai, Koh ...
Get Thailand Travel Guide with Offline Maps – tripwolf (including Bangkok, Phi Phi Islands, Chiang Mai, Koh Phangan, Koh Samui) on the App Store. ...

Image taken from page 332 of 'Adventures in the far interior of South Africa; including a journey to ...
Image taken from: Title: "Adventures in the far interior of South Africa; including a journey to Lake Ngami, and rambles in Honduras. To which ...

Zellers Everything Must GO Including Zeddy
http://goo.gl/E0gDo Zellers stores are closing and everything must go, even Zeddy. The Zellers Executive Managing Director has taken it upon ...

30 sites including Inca trails, ancient French cave vie for World Heritage status
The World Heritage Committee gathers in Doha to decide which natural and cultural sites should get their names included on its 981-strong list. ...

Dodo releases NBN plans including unlimited data plan
Dodo has today announced its new range of consumer NBN plans, including a plan offering unlimited data per month.

Tony Abbott updates interests register, including gifts to wife and daughters
Prime Minister Tony Abbott has updated his pecuniary interest register, listing gifts and hospitality for his wife and daughters, including a ...

Resources last updated: 12/9/2015 10:12:55 PM