Let’s say you have an ASP.Net Web Application in which you have multiple Web Forms with different languages / interfaces. By default, each Web Form will have separate code file. If you make changes in one code file then you have to modify all the respective code files. This is a time consuming process. To reduce the work load and to maintain consistency in code file, we can maintain single code file for multiple Web Forms.
In this tutorial we'll take the following files:
Web Forms
CodeBehind File (C#)
We’ll take one CodeBehind file, “interface_english.aspx.cs” for multiple Web Forms
Now the question is how to inherit a single CodeBehind file for multiple Web Forms. The code below will explain you how to achieve this.
<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_english" CodeFile="interface_english.aspx.cs" %>
Description
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="interface_english.aspx.cs" Inherits="interface_english" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <p>English <a href="interface_arabic.aspx">Arabic</a> <a href="interface_hindi.aspx">Hindi</a> <a href="interface_telugu.aspx">Telugu</a></p> <h3>English Interface</h3> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /> <asp:Label ID="lblMessage" runat="server"></asp:Label> </form> </body> </html>
You just need to change the "Inherits" attrubute for respective Web Forms as mentioned below
<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_arabic" CodeFile="interface_english.aspx.cs" %>
<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_hindi" CodeFile="interface_english.aspx.cs" %>
<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_telugu" CodeFile="interface_english.aspx.cs" %>
This CodeBehind file is common for all Web Forms.
using System; public partial class interface_english: System.Web.UI.Page { // Common logic that all the pages should have. protected void Page_Load(object sender, EventArgs e) { Response.Write("This is a common text for all Web Forms."); } protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "You are viewing English Interface."; } } public partial class interface_arabic: interface_english { // Specific logic for interface_arabic protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "تقوم بعرض واجهة اللغة العربية."; } } public partial class interface_hindi: interface_english { // Specific logic for interface_hindi protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "आप हिंदी इंटरफेस देख रहे हैं।"; } } public partial class interface_telugu: interface_english { // Specific logic for interface_telugu protected void btnSubmit_Click(object sender, EventArgs e) { lblMessage.Text = "మీరు తెలుగు ఇంటర్ఫేస్ చూస్తున్నారు"; } }