Today
in Engineer's World, we are discussing a C# query faced
by a developer in very easy way. Posted By- +Manish Kumar Gautam +LIVE VIAR +ASP.NET SOLUTIONS
Calling .cs (class file) from .aspx.cs (webform code behind file) in Asp.net C#
Practical Implementation:
A) hi infinite times
Calling .cs (class file) from .aspx.cs (webform code behind file) in Asp.net C#
Up
till now, I have created a simple website in which I have added a web form file
and a class file. But, why did I added these two files in the very beginning of
the project. What's the need behind adding these two file's?
Let's
understand this fact. If you quick watch the extension for the class file, its
.cs (dot cs). And if you press F7 on the web form you will notice that the
extension of web form file is .aspx.cs (dot aspx dot cs). Very much
similar to the blog title. .cs and .aspx.cs. Now, as per the title of the blog
we have to call the class (.cs) file from the web form code behind
(.aspx.cs) file.
Practical Implementation:
So,
let's first create a simple form in which we have a textbox control,
a button control and a label control.
Source code
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="calling_cs_page_from_aspxcs.aspx.cs"
Inherits="calling_cs_page_from_aspxcs" %>
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset style="width:30%;">
<legend>Application Form</legend>
<table>
<tr>
<th colspan="2"><asp:Label ID="lblmsg" runat="server" Text="" Visible="false" ForeColor="Red"></asp:Label></th>
</tr>
<tr>
<th style="width:50%;">
Name:
</th>
<td style="width:50%;">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>
</tr>
</table>
</fieldset>
</div>
</form>
</body>
</html>
Press
Ctrl+F5. Our form will look like this
What
next, we have to do is? We have to validate the values entered in the
textbox.
So,
for that we have to write the validation code in our Validate class file that
we had created in "App_Code" folder.
Class code:
/// <summary>
/// Summary description for Validation
/// </summary>
public class Validation
{
public Validation()
{
//
// TODO: Add
constructor logic here
//
}
//checkName function that we have created
public string checkName(string txtId)
{
string msg = string.Empty;
if (txtId.Trim() == "") //statement check if textbox is empty
{
return msg = "Enter Name";
}
else if (txtId.Trim().Length >=
15) //statement check if textbox contains
more than 15 characters
{
return msg = "Name length cannot be greater than 15 characters";
}
return msg;
}
}
This
will create business logic to our web form.
Related Questions:
Q-1 How many values does a
function return?
A) any number of values
A) any number of values
B) 2
C) 0
D) 1
D) 1
Ans. Option (D)
Explanation- A method can return only either single value or no value if no then it’s declared as void method();
Explanation- A method can return only either single value or no value if no then it’s declared as void method();
Q-2 What
is the output for selective code?
public static void Main(string[] args)
{
p();
void p()
{
Console.WriteLine("hi");
}
}
A) hi infinite times
B) hi
C) None of the mentioned
C) None of the mentioned
D) Compile time error
Ans. Option (D)
Explanation: Invalid definition of function p() inside main().
Explanation: Invalid definition of function p() inside main().
Keep learning and sharing...
No comments:
Post a Comment