Code-Behind Model:
Code Behind refers to the code for an ASP.NET Web page that is
written in a separate class file that can have the extension of .aspx.cs or
.aspx.vb depending on the language used. One major point of Code Behind is that
the code for all the Web pages is compiled into a DLL file that allows the web
pages to be hosted free from any Inline Server Code.
Code-behind Model refers to code for our ASP.NET page that is
contained within a separate class file. This allows a clean separation of our
UI from our Code. To create a new page in our ASP.NET solution that uses the
code - behind model, to build a page that uses the code - behind model, we
first select the page in the Add New Item dialog and make sure the
Place Code in Separate File check box is selected.
Code-Behind
File Extensions:
Web
Form- .aspx file &.aspx.cs file or .aspx.vb file
Master
Page- .master.cs file or .master.vb file
Web
User Control - .ascx file .ascx.cs file or .ascx.vb file
Web
Services - .asmx file & cs file or .vb file
By using the code - behind
model is to separate the business logic
and presentation logic into separate
files. Code Behind approach is a better way to develop and design the .aspx
page having basic layout of a web page containing all the necessary controls
required for the GUI of the web page. Then include the C# or VB code behind
class file for handling the events of controls. This mechanism separates the
web page from design layout from the coding part.
Example:
Codebehind.aspx:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Codebehind Model</title>
<script type="text/javascript">
function validate(v) {
if (v != "") {
document.getElementById("lblError").innerHTML = "";
var value1 = document.getElementById("txtValue1").value;
var value2 = document.getElementById("txtValue2").value;
var error = "";
if (value1 == "") error += "Please Enter
Value1" + "<br />";
if (value2 == "") error += "Please Enter
Value2" + "<br />";
if (error != "") {
document.getElementById("lblError").innerHTML = error;
return false;
}
else {
__doPostBack(event,
"");
return true;
}
}
else {
document.getElementById("lblError").innerHTML = "Please Select Any
Option!!!";
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div align="center" style="background-color: #6984bb; padding: 5px;">
<h3 style="text-align: center; color: maroon">Code Behind Model Example
</h3>
<asp:Label ID="lblError" runat="server" ForeColor="Red" />
<b>Enter Value1:</b><asp:TextBox ID="txtValue1" runat="server" />
<br />
<br />
<b>Enter Value2:</b><asp:TextBox ID="txtValue2" runat="server" />
<br />
<br />
<b>Select Option:</b>
<asp:DropDownList ID="ddlCalc" runat="server" AutoPostBack="true" onchange="return
validate(this.value);" OnSelectedIndexChanged="ddlCalc_SelectedIndexChanged">
<asp:ListItem Text="Select" Value="" />
<asp:ListItem Text="Add" Value="+" />
<asp:ListItem Text="Sub" Value="-" />
<asp:ListItem Text="Mul" Value="*" />
<asp:ListItem Text="Div" Value="/" />
</asp:DropDownList>
<br />
<b>Result:</b><asp:TextBox ID="txtResult" runat="server" ReadOnly="true" />
<br />
<br />
</div>
</form>
</body>
</html>
Design view:
Codebehind.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class codebehind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtValue1.Focus();
}
protected void ddlCalc_SelectedIndexChanged(object sender, EventArgs e)
{
lblError.Text = string.Empty;
txtResult.Text = string.Empty;
try
{
float value1 = float.Parse(txtValue1.Text.Trim());
float value2 = float.Parse(txtValue2.Text.Trim());
float result = 0;
switch (ddlCalc.SelectedItem.Value)
{
case "+": result = value1
+ value2;
break;
case "-": result = value1
- value2;
break;
case "*": result = value1
* value2;
break;
case "/": if (value2 == 0)
throw new DivideByZeroException("You can't divide by
zero!!!");
result = value1
/ value2; break;
}
txtResult.Text = result.ToString();
}
catch (FormatException ex1)
{
lblError.Text = ex1.Message;
}
catch (OverflowException ex2)
{
lblError.Text = ex2.Message;
}
catch (DivideByZeroException ex3)
{
lblError.Text = ex3.Message;
}
}
}
Output:
To run the page
on server press f5 or cntrl +f5 and see the below output.
Step1: entert value1
and value2 select add from the dropdown list, result will be displayed in below
textbox.
Step2: entert value1
and value2 select Sub from the dropdown list, result will be displayed
in below textbox.
Step3: entert value1
and value2 select Mul from the dropdown list, result will be displayed
in below textbox.
Step4: entert value1
and value2 select Div from the dropdown list, result will be displayed
in below textbox.
Thank you for visiting my blog ,don't forget to comment on post or else if you want daily updates please follow in Google Plus.
No comments:
Post a Comment