how to use codes in c# classes?
i am developing web application using C# and asp.net. i want to put some codes in classes so that i can call these when necessary. example let's i have to files, called register.aspx and register.cs
now i want to store the register.cs codes in classes, so that i can call it when i use it? how to do that?
please redirect me to good sources and give sample code of this.
regards
Re: how to use codes in c# classes?
You need this line at the top of the web page
asp.net Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorChangeOnError.aspx.cs" Inherits="ColorChangeOnError" %>
CodeFile points to the *.cs you wish to use with the page and Inherits points to the method, see the following example
webpage
asp.net Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ColorChangeOnError.aspx.cs" Inherits="ColorChangeOnError" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblFirstName"
Text="First Name"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<asp:RequiredFieldValidator
id="reqFirstName"
ControlToValidate="txtFirstName"
Text="(Required)"
EnableClientScript="false"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastname"
Runat="server" />
<asp:RequiredFieldValidator
id="reqLastName"
ControlToValidate="txtLastName"
Text="(Required)"
EnableClientScript="false"
Runat="server" />
<br /><br />
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" OnClick="btnSubmit_Click" />
</div>
</form>
</body>
</html>
cs file
C# Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
public partial class ColorChangeOnError : System.Web.UI.Page
{
protected void Page_PreRender()
{
foreach (BaseValidator valControl in Page.Validators)
{
WebControl ctrl = (WebControl)Page.FindControl(valControl.ControlToValidate);
if (!valControl.IsValid)
ctrl.BackColor = System.Drawing.Color.Yellow;
else
ctrl.BackColor = System.Drawing.Color.White;
}
}
protected void btnSubmit_Click(object sender, EventArgs e)