CodeVerge.Net Beta


   Explore    Item Entry   Register  Login  
Microsoft News
Asp.Net Forums
IBM Software
Borland Forums
Adobe Forums
Novell Forums

MS SQL 2008 on ASP.NET Hosting



Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 11/7/2006 8:01:41 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 29 Views: 64 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
30 Items, 2 Pages 1 2 |< << Go >> >|
Screamer
Asp.Net User
Shared code behind and functions11/7/2006 8:01:41 PM

0/0

Hi all,

Can someone give me a C# example of how i'd use a single code behind file for both a master page and a content page please? And on top of that, how would I create a set of shared functions that can then be used in codebehind of both the master and content pages?

 A very very very big-to-huge thanks in advance!

(word of warning: i'm a complete and utter .Net beginner)

codeasp
Asp.Net User
Re: Shared code behind and functions11/7/2006 10:45:22 PM

0/0

Hi it is not possible to have a single code-behind file for master page as well as content page.  This is OOP.  All the common functionalities are done in master page and then content pages are derived from this page.  Example: Vehicle class is the master and Car is derived from the Vehicle as it provides more functionality particular to the car.

You can create shared\protected functions in the master page so that it can be accessed from the content pages.  Check the Master Page, master type property in the SDK.

Screamer
Asp.Net User
Re: Shared code behind and functions11/8/2006 12:18:13 PM

0/0

Thanks for the reply codeasp. I'm really sorry, but i've no idea where i'm supposed to be looking in the SDK. If you could tell me where to find the article(s) i'd appreciate it, or even better, where I could find some C# examples on the web.

Thanks again for your help so far.

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/8/2006 12:53:23 PM

0/0

If you have some shared functions you want each to use, you could create a new class, call it BaseFunctions. Create a new interface, IBaseFunctions that exposes those function signatures, then... in your Page and MasterPage, include references to those interfaces. For instance:

  

// Base class functions
public class BaseFunctions
{
     public string getUserName()
     {
          return Request.Cookies["Username"].Value;
     }
}

// Interface
public interface IBaseFunctions
{
     string getUserName();
}

// Webpage
public partial class _Default : Page, IBaseFunctions
{
     Response.Write("My username is: " + getUserName());
}

// Masterpage
public partial class _Default : MasterPage, IBaseFunctions
{
     Response.Write("My username is: " + getUserName());
}
 
 
Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/8/2006 1:54:41 PM

0/0

Thanks Mike - that's brilliant! Am I right in saying that the BaseFunctions and IBaseFunctions interface go inside the Master page's code behind? Or do they go in a new file (if so, how is the new file 'linked-in' to the master and content pages)?

A very big thanks for that so far!

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/8/2006 2:27:21 PM

0/0

Ack.. I've got some foul-ups written in there. I'm working you another example. Hang on...

Mike Bell
My Code Blog
Mike Bell
Asp.Net User
Re: Shared code behind and functions11/8/2006 2:41:53 PM

0/0

Ok.. this is a little mixed. In the end you will still need to use two classes. PageBase and MasterBase. Each can implement it's own interface, or the same one. IBaseFunctions can still be used. When you reference an interface like that in the declaration, that means you will have to have functions in the class to support all methods that are defined in the interface. In other words, in our interface, we have one method... string getUserName. You will need that function to exist in bot PageBase and MasterBase, if you want to use the interface in them.

To keep everything encapsulated in one base class, such as BaseFunctions, you can use this model:

  

// Interface
public interface IBaseFunctions
{
     string getUserName();
}

public class BaseFunctions : IBaseFunctions
{
      public getUsername(){
        return "blah";
      }
}

then, in your page base class, and masterpage base class, you do

// Base class functions
public class PageBase : Page, IBaseFunctions
{
     BaseFunctions bs = new BaseFunctions();

     string IBaseFunctions.getUserName()
     {
	return this.getUserName();
     }

     public virtual getUserName()
     {
	return bs.getUserName();	
     }
}

// Base class functions
public class MasterBase : MasterPage, IBaseFunctions
{
     BaseFunctions bs = new BaseFunctions();

     string IBaseFunctions.getUserName()
     {
	return this.getUserName();
     }

     public virtual getUserName()
     {
	return bs.getUserName();	
     }
}
 

This will keep your base function code from having to be written in 2 diff places, and the functionality will be within reach of anything that derives from your base classes.
 
Mike Bell
My Code Blog
Mike Bell
Asp.Net User
Re: Shared code behind and functions11/8/2006 2:50:25 PM

0/0

To add on to what I said above; With that code in place, you can declare your pages like:

public partial class _default : PageBase

 and your master pages like:

public partial class _default : MasterBase

 
Doing so will give you access to all of you BaseFunctions methods.
 


Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/8/2006 7:09:10 PM

0/0

Whoa - thanks for taking the time to do all that Mike.

 1 last question: Where do I put my shard base class and interface? I'm guessing that the rest goes in the master page's code behind and the content page's codebehind as appropriate. Does the shared stuff go in with the master page-specific code?

 Thanks again (again!).

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/8/2006 7:13:15 PM

0/0

N/P. The BaseFunctions.cs would just go in you App_Code directory, along with PageBase.cs, MasterBase.cs, and IBaseFunctions.cs.

Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/10/2006 9:51:50 AM

0/0

Hi - still having trouble with this unfortunately.

I've added the various bits of code into .cs files, stuck them in the App_Code directory, and uploaded it. The problem is that the moment I change the line:

public

partial class Master : System.Web.UI.MasterPage

to:

public

partial class Master : MasterBase

I just get a "The type or namespace name 'MasterBase' could not be found (are you missing a using directive or an assembly reference?)" message. Right now, i'm not 'publishing' my site. I'm just uploading the original files. Do I need to publish before the App_Code directory can be accessed by the other files as required?

 

Thanks again for any help!

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/10/2006 12:31:21 PM

0/0

Doesn't look like youe created this:

 

 

public class MasterBase : MasterPage, IBaseFunctions
{
     BaseFunctions bs = new BaseFunctions();

     string IBaseFunctions.getUserName()
     {
	return this.getUserName();
     }

     public virtual getUserName()
     {
	return bs.getUserName();	
     }
}
  
Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/10/2006 1:35:34 PM

0/0

Right. Actually, the problem was all my fault - I was using the App_Data folder rather than App_Code! Doh!

New problem though. I get an error in BaseFunctions.cs:

"CS1520: Class, struct, or interface method must have a return type"

It points to this line (line 3) in the file:

public class BaseFunctions : IBaseFunctions
Line 2:  {
Line 3:  public getUsername(){
Line 4:  return "blah";
Line 5:  }

Any ideas? Thanks again again again (!)

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/10/2006 2:27:22 PM

0/0

woops.. should be:

public string getUsername(){


Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/10/2006 3:00:00 PM

0/0

Thanks Mike - new error now:

"CS1520: Class, struct, or interface method must have a return type"

Line 8:  }
Line 9: 
Line 10: public virtual getUserName()
Line 11: {
Line 12: return bs.getUserName();

 

That occurs in MasterBase.cs

 

Really, a big thanks for all this help. I can understand what all this is doing, but i'm not even close to being able to understand what's not working (yet!).

Taking a guess, should the red line be "public virtual string getUserName()"? When I make that change, I get this new error in BaseFunctions.cs:

"CS0535: 'BaseFunctions' does not implement interface member 'IBaseFunctions.getUserName()'"

Line 1:  public class BaseFunctions : IBaseFunctions
Line 2:  {
Line 3:  public string getUsername(){

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/10/2006 3:11:33 PM

0/0

c# is case sensative. You need to correct the getUsername function names to match exactly.

Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/10/2006 3:20:00 PM

0/0

Right - got it working - thanks Mike! There was a getUsername and a getUserName. Looks like .net thinks they're different because of the difference in case (is that right/true?).

Final problem: how do I actually use the function. Right now, i've just got my master page using ": MasterBase" and it loads fine. When I insert "string myvar = getUserName();" it all breaks. I've also tried "getUserName myvar = new getUserName();" with no luck. To be honest, the eventual intention is to replace the 'return "blah";' fucntion with one that accepts a string and formats it for 'true' html (e.g. replace "&" with "&amp;"), so "string myvar = getUserName();" isn;t really what I want to do with the final thing - I want to do this: "string myvar = myNewFunction("this is my string to be formatted");". If I do that right now with getUserName, I just get a "can't fint getUserName()" message...

 Any (final) ideas?

Thanks!

Mike Bell
Asp.Net User
Re: Shared code behind and functions11/10/2006 3:32:37 PM

0/0

When you try:
string myvar = getUserName();

What is the error you get? See if you get intellisense when typing out:
string myvar = base.

The 'base' keyword in this case, points at the MasterBase class.

If you would, paste the full source of your masterpage cs file. Not the MasterBase....
 


Mike Bell
My Code Blog
Screamer
Asp.Net User
Re: Shared code behind and functions11/10/2006 4:46:14 PM

0/0

Right: the error when using "string myvar = getUserName();" is "CS0120: An object reference is required for the nonstatic field, method, or property 'MasterBase.getUserName()'"

And here's the code snippet it gives:

Line 12: public partial class Master : MasterBase
Line 13: {
Line 14: string myvar = getUserName();
Line 15: OleDbConnection DBConnection1;
Line 16: OleDbCommand DBCommand1;

Right, and here's my entire Master page code behind:

using

System;

using

System.Data;

using

System.Configuration;

using

System.Web;

using

System.Web.Security;

using

System.Web.UI;

using

System.Web.UI.WebControls;

using

System.Web.UI.WebControls.WebParts;

using

System.Web.UI.HtmlControls;

using

System.Data.OleDb;

public

partial class Master : MasterBase

{

string myvar = getUserName();

OleDbConnection DBConnection1;

OleDbCommand DBCommand1;

OleDbDataReader DBDataReader1;

 

protected void Page_Load(object sender, EventArgs e)

{

try

{

DBConnection1 =

new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

DBConnection1.Open();

}

catch (Exception err) {

Response.Write(

"Error: " + err.ToString() + "<br />");

}

 

string BackgroundImage = "";

string BackgroundTagText = "";

string BackgroundTagUrl = "";

string ColourMain = "#990000";

string ColourSecondary = "#FF0000";

DBCommand1 =

new OleDbCommand("SELECT TOP 1 * FROM SiteTheme ORDER BY RND((1000*ID))", DBConnection1);

DBDataReader1 = DBCommand1.ExecuteReader();

if (DBDataReader1.Read())

{

BackgroundImage = DBDataReader1[

"BackgroundImage"].ToString();

BackgroundTagText = FormatString(DBDataReader1[

"BackgroundTagText"].ToString());

BackgroundTagUrl = FormatStringUrlIn(DBDataReader1[

"BackgroundTagUrl"].ToString());

ColourMain = DBDataReader1[

"ColourMain"].ToString();

ColourSecondary = DBDataReader1[

"ColourSecondary"].ToString();

}

DBCommand1.Dispose();

DBDataReader1.Close();

Style1.Text = ColourMain;

Style2.Text = ColourSecondary;

Style3.Text = BackgroundImage;

Style4.Text = ColourMain;

Style5.Text = ColourMain;

Style6.Text = ColourMain;

Style7.Text = ColourMain;

Style8.Text = ColourSecondary;

ContentBackTagA.InnerText = BackgroundTagText;

ContentBackTagA.HRef = BackgroundTagUrl;

}

 

public string FormatString(object TheString)

{

string TheStringOutput = (string)TheString;

TheStringOutput = TheStringOutput.Trim();

TheStringOutput = TheStringOutput.Replace(

"&","&amp;");

TheStringOutput = TheStringOutput.Replace(

"&amp;amp;","&amp;");

TheStringOutput = TheStringOutput.Replace(

" "," ");

return TheStringOutput;

}

 

public string FormatStringUrlIn(object TheString)

{

string TheStringOutput = (string)TheString;

TheStringOutput = TheStringOutput.Replace(

"&","-amp-");

TheStringOutput = TheStringOutput.Replace(

"-amp-amp;","-amp-");

TheStringOutput = TheStringOutput.Replace(

"/","-fdsl-");

TheStringOutput = TheStringOutput.Replace(

"\\","-bksl-");

TheStringOutput = TheStringOutput.Replace(

"\"","-dbqu-");

TheStringOutput = TheStringOutput.Replace(

"'","-siqu-");

return TheStringOutput;

}

 

public string FormatStringUrlOut(object TheString)

{

string TheStringOutput = (string)TheString;

TheStringOutput = TheStringOutput.Replace(

"-amp-","&amp;");

TheStringOutput = TheStringOutput.Replace(

"-fdsl-","/");

TheStringOutput = TheStringOutput.Replace(

"-bksl-","\\");

TheStringOutput = TheStringOutput.Replace(

"-dbqu-","\"");

TheStringOutput = TheStringOutput.Replace(

"-siqu-","'");

return TheStringOutput;

}

 

protected void Page_Unload()

{

if (DBConnection1.State.ToString() == "Open")

DBConnection1.Close();

}

}

 

As you can probably tell, the intention is to move all of the 'functions' at the bottom into the App_Code folder...

Screamer
Asp.Net User
Re: Shared code behind and functions11/10/2006 4:54:20 PM

0/0

Erm... i'll try the code again...

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class Master : MasterBase
{
	//string myvar = getUserName();
	OleDbConnection DBConnection1;
	OleDbCommand DBCommand1;
	OleDbDataReader DBDataReader1;
	
	protected void Page_Load(object sender, EventArgs e)
	{
		try
		{
			DBConnection1 = new OleDbConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
			DBConnection1.Open();
		} catch (Exception err) {
			Response.Write("Error: " + err.ToString() + "&lt;br />");
		}
		
		string BackgroundImage = "";
		string BackgroundTagText = "";
		string BackgroundTagUrl = "";
		string ColourMain = "#990000";
		string ColourSecondary = "#FF0000";
		DBCommand1 = new OleDbCommand("SELECT TOP 1 * FROM SiteTheme ORDER BY RND((1000*ID))", DBConnection1);
		DBDataReader1 = DBCommand1.ExecuteReader();
		if (DBDataReader1.Read())
		{
			BackgroundImage = DBDataReader1["BackgroundImage"].ToString();
			BackgroundTagText = FormatString(DBDataReader1["BackgroundTagText"].ToString());
			BackgroundTagUrl = FormatStringUrlIn(DBDataReader1["BackgroundTagUrl"].ToString());
			ColourMain = DBDataReader1["ColourMain"].ToString();
			ColourSecondary = DBDataReader1["ColourSecondary"].ToString();
		}
		DBCommand1.Dispose();
		DBDataReader1.Close();
		Style1.Text = ColourMain;
		Style2.Text = ColourSecondary;
		Style3.Text = BackgroundImage;
		Style4.Text = ColourMain;
		Style5.Text = ColourMain;
		Style6.Text = ColourMain;
		Style7.Text = ColourMain;
		Style8.Text = ColourSecondary;
		ContentBackTagA.InnerText = BackgroundTagText;
		ContentBackTagA.HRef = BackgroundTagUrl;
	}
	
	public string FormatString(object TheString)
	{
		string TheStringOutput = (string)TheString;
		TheStringOutput = TheStringOutput.Trim();
		TheStringOutput = TheStringOutput.Replace("&","&amp;");
		TheStringOutput = TheStringOutput.Replace("&amp;amp;","&amp;");
		TheStringOutput = TheStringOutput.Replace("  "," ");
		return TheStringOutput;
	}
	
	public string FormatStringUrlIn(object TheString)
	{
		string TheStringOutput = (string)TheString;
		TheStringOutput = TheStringOutput.Replace("&","-amp-");
		TheStringOutput = TheStringOutput.Replace("-amp-amp;","-amp-");
		TheStringOutput = TheStringOutput.Replace("/","-fdsl-");
		TheStringOutput = TheStringOutput.Replace("\\","-bksl-");
		TheStringOutput = TheStringOutput.Replace("\"","-dbqu-");
		TheStringOutput = TheStringOutput.Replace("'","-siqu-");
		return TheStringOutput;
	}
	
	public string FormatStringUrlOut(object TheString)
	{
		string TheStringOutput = (string)TheString;
		TheStringOutput = TheStringOutput.Replace("-amp-","&amp;");
		TheStringOutput = TheStringOutput.Replace("-fdsl-","/");
		TheStringOutput = TheStringOutput.Replace("-bksl-","\\");
		TheStringOutput = TheStringOutput.Replace("-dbqu-","\"");
		TheStringOutput = TheStringOutput.Replace("-siqu-","'");
		return TheStringOutput;
	}
	
	protected void Page_Unload()
	{
		if (DBConnection1.State.ToString() == "Open")
		DBConnection1.Close();
	}
}
 
30 Items, 2 Pages 1 2 |< << Go >> >|


Free Download:

Books:
Professional ASP.NET 3.5 in C and VB: In C# and VB Authors: Bill Evjen, Scott Hanselman, Devin Rader, Pages: 1704, Published: 2008
Visual Basic .NET Unleashed Authors: Paul Kimmel, Pages: 757, Published: 2002
Professional ASP.NET 1.1 Authors: Alex Homer, Dave Sussman, Rob Howard, Brian Francis, Karli Watson, Richard Anderson, Pages: 1337, Published: 2004
Professional DotNetNuke ASP.Net Portals Authors: Shaun Walker, Patrick J. Santry, Joe Brinkman, Daniel Caron, Scott McCulloch, Scott Willhite, Bruce Hopkins, Pages: 421, Published: 2005
Programming Windows Presentation Foundation Authors: Chris Sells, Ian Griffiths, Pages: 430, Published: 2005
ASP.NET 2.0 Website Programming: Problem-design-solution Authors: Marco Bellinaso, Pages: 576, Published: 2006
Beginning ASP.NET 3.5: In C# and VB Authors: Imar Spaanjaars, Pages: 734, Published: 2008
Pro LINQ Object Relational Mapping in C# 2008 Authors: Vijay P. Mehta, Pages: 408, Published: 2008
Mastering Visual Basic.NET Authors: Evangelos Petroutsos, Pages: 1153, Published: 2002
Component-Based Development with Visual C# Authors: Ted Faison, Edmund W. Faison, Pages: 1008, Published: 2002

Web:
Adobe - Flex Quick Start Basics: Building components by using code ... Flex cookbook (share code); Adobe AIR cookbook (share code) ... The main application file also makes use of code behind and the example also features the .... Panel; public class PaddedPanel extends Panel { public function PaddedPanel () ...
Getting a page's URL in code-behind - Imar.Spaanjaars.Com Aug 6, 2005 ... Getting a page's URL in code-behind ... Public Shared Function GetUrl(itemID As Integer, _ page As System.Web.UI.Page) As String Return page ...
call a javascript function within code behind - bytes Join one and ask questions, share your expertise. Application Development ... i want to call a java script function from code behind. ...
How to call code in aspx webform from functions defined in code ... I find it necessary to mix code-behind and non-code behind ... html elements) or even call public shared functions from other code behind ...
How to share arrays between code behind and javascript with ... How to share arrays between code behind and javascript with RegisterStartupScript and ..... Function to check whether string is a valid path and file name ...
Can you share a code behind file with a page and usercontrol ... Can you share a code behind file with a page and usercontrol? ASP.NET General. ... Function sql_scalar(ByVal str As String) As String ...
Call Atlas ws from both Javascript and VB code behind? - ASP.NET ... By changing the function's scope to shared and seperating the code into the ... and the vb code behind?... both returning the same string. ...
How can I call js function from code behind? - DevX.com Forums Michael Sync http://michaelsync.net. The more you share,The more you get ... You can call Js functions as a script registered on the codebehind itself ...
Call a javascript function from codebehind in Asp.net using ajax ... ... of code snippets, categorize them with tags / keywords, and share them with the world. Call a javascript function from codebehind in Asp.net using ajax (See ... to javascript asp.net ajax codebehind clientside by shukri.adams on Tue ...
Calling a function in codebehind from the html side of the aspx ... Join one and ask questions, share your expertise. ... a onClick or something and assign it with the sub/function name that is in the codebehind ... i declare it in the codebehind and i can only access only a few of them? ...

Videos:
The Secret behind the Hebrew alphabet Detailed description behind all the Hebrew Bible letters. Code of the Hebrew letters. Kabbalah , English Subtitles , Bible codes , mysticism ...
¥SANSSOLEIL¥1¥ Chris Marker - Sans Soleil (1) http://www.youtube.com/user/existentialistcat http://www.youtube.com/user/truthinliez
www.moldytoaster.com ow the incline would not conduct him to his goal. If he were to reach another outlet, he would find it obstructed by a plug or a grating. Every ...
Authors@Google: Michael Chorost Author Michael Chorost visits Google's headquarters in Mountain View, CA, to discuss his book "Rebuilt: How Becoming Part Computer Made Me More ...
GeoServer and Architectures of Participation for Geospatial ... Google TechTalks August 23, 2006 Chris Holmes ABSTRACT This talk will introduce GeoServer, an open source server to publish and edit ...
Powered by YouTube - Design Your Own YouTube Player Speaker: Geoff Stearns The YouTube player APIs allow you to take nearly full control over the YouTube embedded video players. Controls like ...
Charlie Rose - Road to Innovation, Part Two: Eric Schmidt ... Google CEO Eric Schmidt and Verizon CEO Ivan Seidenberg are the featured guests on this second installment of the four-part series, "Road to ...
BeGeistert 014 Interview of yellowTAB During BeGeistert 014 (April 2-3, 2005) Daniel "daat" Teixeira from IsComputerOn and Chris Simmons from Haiku News spent 40 minutes interviewing ...
A Googly MySQL Cluster Talk Google TechTalks April 28, 2006 Stewart Smith Stewart Smith works for MySQL AB as a software engineer working on MySQL Cluster. He is an ...
Web Applications and the Ubiquitous Web Google TechTalks February 1, 2006 Dave Raggett Dave Raggett is currently a W3C Fellow from Canon, and W3C Activity Lead for Multimodal ...




Search This Site:










dynamically styling a database generated menu

master page class type error

inconsistent use of sitemap?

menu not paying attention to font size

sitemapnode - anonymous users only?

sitemapnode url with two keys

help: applying a skin to a dynamic control.

need help with including a link button on a master page.

treeview and postback on parent node

how to change the header falsh programmatically?

web.sitemap

applying a different master page to a different part of the site

masterpage/menu - content page getting cut off in firefox

newbie question re: contentplaceholder

menu control on master pages

find menuitem without a sitemap

preventing staticbottomseparatorimage appearing after last menuitem

intellisense for .skin files

masterpages and user controls

multiple sitemaps, switching providers

cannot change dynamically the title of my page when using a master page

re there dynamic tabs or dynamic menus in asp.net (like the tab control and mainmenu control in vb.net)

master page compilation

wizard bug on masterpage architecture

help me (multicolumn treeview)

how to change data from content1 page to content2

problem displaying a menu control in iis5.0, but ok in iis6.0

postback on embedded page

master page viewing problem.

how to dynammically discover available themes ?

  Privacy | Contact Us
All Times Are GMT