Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include File Equivalent in ASP.NET

Status
Not open for further replies.

starbuckinfinity

Programmer
Jan 31, 2005
8
US
hi. In classic ASP, include files were used for content that would allow you to store content in one file that could simply be "included" in other files that needed the same content. An example would be navigation bars, shared components, etc. I know there are these "server controls" in ASP.NET which are supposed to be similar to include files, but what about files which hold just mainly content things and components that are not functional? Is there anything in ASP.NET that does the equivalent without being for the purpose of building functional components? Or do you just have to put the same code on every page in ASP.NET? With classic ASP, whenever a change needed to be made to, say, a navigation, it was so easy just to open one file and do it in that file. Now, with ASP.NET, if you have to make a change, you would have to open every file that common component was on and make the change if there is no equivalent. Will someone please help me out on this? By the way, I am using VB.NET. Thank you.
 
Look into User Controls and Custom Controls. A simple example of a User Control would be your navigation bar example. You would build your navigation bar as a User Control (.ascx file). Once you're done all you need to do is drag and drop it onto your page (if you're using VSNET). If you need to change it, you just make the change in one place. Here's a very simple example of a login form User Control:
Code:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="login.ascx.cs" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"%>[/URL]
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
	<TR>
		<TD>UserName</TD>
		<TD>
			<asp:TextBox id="txtUserName" runat="server"></asp:TextBox></TD>
	</TR>
	<TR>
		<TD>Password</TD>
		<TD>
			<asp:TextBox id="txtPassword" runat="server" TextMode="Password"></asp:TextBox></TD>
	</TR>
	<TR>
		<TD align="center" colSpan="2">
			<P>
				<asp:Button id="btnSubmit" runat="server" Text="Button"></asp:Button></P>
			<P>
				<asp:Label id="Label1" runat="server" Width="142px"></asp:Label></P>
		</TD>
	</TR>
</TABLE>
Notice that there are no <HTML><HEAD><BODY> etc. tags.
Here's the code-behind:
Code:
using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for login.
	/// </summary>
	public abstract class login : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txtUserName;
		protected System.Web.UI.WebControls.TextBox txtPassword;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Button btnSubmit;
		public string UserName
		{
			get{return txtUserName.Text;}
			set{txtUserName.Text = value;}
		}
		public string Password
		{
			get{return txtPassword.Text;}
			set{txtPassword.Text = value;}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSubmit_Click(object sender, System.EventArgs e)
		{
			Label1.Text = "UserName = " + UserName + "\n" + "Password = " + Password;
		}
	}
 
Web User Control, Web Custom Control and System.Web.Page inheritance are few answers to your problems.

[morning]
 
Just realised your post was submitted twice - I had posted a reply in the other thread - thread855-998705

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top