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 

Click imagination hunt to see latest Blogs


Calling .cs (class file) from .aspx.cs (webform code behind file) in Asp.net C#

We have already prepared our web form and validation class logic in our previous two blogs.
Inference from our previous blogs, all we need is to call our validation class method into our web form.

For that, we have to call the validate.cs file method in 
calling_cs_page_from_aspxcs.aspx.cs web form file. And this calling need to be done on click of button event.

Practical Implementation:

Code Behind code:

using System;    -----> Namespace

public partial class calling_cs_page_from_aspxcs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //using and creating the object of validation.cs class
        Validation isValid = new Validation(); //isValid-> object of Validation class

/* Here, isValid (object of Validation class) has a method checkName which is taking the name. So, we need to pass that name (entered from textbox) onto the method parameter. And also this method (checkName) is returning output in string. This means, we need to store the output in string. So, we take string variable 'msg' to store the result. */
      
        string msg = isValid.checkName(txtName.Text);

        if (msg.Trim().Length != 0)
        {
            lblmsg.Text = msg;
            lblmsg.Visible = true;
        }
        else
        {
            lblmsg.Text = "Name validate successfully.";
            lblmsg.ForeColor = System.Drawing.Color.Green;
            lblmsg.Visible = true;
        }
    }
}


Press Ctrl+Shift+B. As the output, message shows build succeeded. Press Ctrl+F5. It will Run the website locally.
  
You can check all the three cases:

A) Case 1: When we do not enter anything in the name text box. And click on the submit button. We got a label message - which says - "Enter Name". One more thing to note down is the message is in red color, implies it is a validation alert message, means this field is required, we cannot submit a blank name in  the 'Name' field.



B) Case 2: When we input more than 15 characters in the name text box. And click on the submit button. We again got a label message - which says - "Name length cannot be greater than 15 characters" in Red color. Again an alert message.



C) Case 3: When we input the appropriate name with less than 15 characters. It is accepted. Hence, giving a success message in green color.



This demo shows - how we can call a function from class (.cs) file present in some different location into a web form (.aspx.cs) which is also present in some different location.


Related Questions:


Q-1 What will be the correct output for the given code snippet?
  
class maths
 {
     int fact(int n)
     {
         int result;
         if (n == 1)
         return 1;
         result = fact(n - 1) * n;
         return result;
     }
 }
 class Output
 {
     static void main(String args[])
     {
         maths obj = new maths() ;
         Console.WriteLine(obj.fact(4)*obj.fact(2));
     }
 }

A) 60
B) 64
C) 120
D) 48

Ans. Option(D)
Explanation- 4! = 4*3*2*1 & 2! = 2*1 .So, 24*2 = 48.
Output- 48

Q-2 What is output of the code?

static void Main(string[] args)
{
    m();
    Mul();
    Console.ReadLine();
}
static void Mul()
{
    Console.WriteLine("4");
}
static void m()
{
    Console.WriteLine("3");
    Mul();
}

A) 4 3 4
B) 4 4 3
C) 4 3 3
D) 3 4 4

Ans. Option(D)
Explanation- First m() will be executed to print the number ‘3’ and at last mentioned function Mul() will be executed to print the statement 4. Second, Mul() will be executed to print the number ‘4’ to return the output as 3 4 4.
Output- 4 3 4.

Click imagination hunt to see latest Blogs

Keep learning and sharing…

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions