Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 1 December 2015

How to call .cs file from .aspx.cs file in asp.net c#?

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 

imagination hunt to see latest Blogs

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">
    <form id="form1" runat="server">
            <fieldset style="width:30%;">
                <legend>Application Form</legend>
                        <th colspan="2"><asp:Label ID="lblmsg" runat="server" Text="" Visible="false" ForeColor="Red"></asp:Label></th>
                        <th style="width:50%;">
                        <td style="width:50%;">
                            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                        <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></td>

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:

using System;        //--------------> Namespace
/// <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
B) 2
C) 0
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();

Q-2 What is the output for selective code?

public static void Main(string[] args)
     void p()

A) hi infinite times
B) hi
C) None of the mentioned
D) Compile time error
Ans. Option (D)
Explanation: Invalid definition of function p() inside main(). 

Click imagination hunt to see latest Blogs

Keep learning and sharing...

No comments:

Post a comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...