Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 29 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 

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…
Read More »

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 

Click 
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">
    <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:

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)
 {
     p();
     void p()
     {
         Console.WriteLine("hi");
     }
 }

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...
Read More »

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#

This question is very general. And is faced by almost everyone including me. So, I have tried something which will be very helpful to everyone and beginners.

To understand this let's create a new website.



Select Asp.net Empty website. After writing the file name click OK button.



Right Click on the website title in the Solution Explorer -> Add -> Choose Web Form (or Add New Item) -> Write the appropriate web form name and click OK button.







Similarly, to add a class file. Right click on project title -> Add new item.



Choose Class file. Write the file name and click the Add button.



An alert box will appear, that will ask that all the class file will be added in 'App_Code' folder. Click 'Yes' button. It is not necessary; you can add your own folder. But, it's good to keep all your class file in 'App_Code' as it will keep your files well organized and easily managed.



You can see that only your class file is added in the 'App_Code' folder.




Related Questions:

Q-1 Why class files need to add in 'App_Code' folder?
Ans. Files need to be add in 'App_Code' folder because:
A) It not only store class file, but it can also store various other the files, such as typed data set, text files, and reports. And it will automatically compile at run-time.
B) Files can be easily accessible through the project.
C) One of the important features of the App_Code folder is that only one .dll is created for the complete folder, irrespective of how many files it contains when we publish our project.

Q-2 Can we access a class file that is declared in the 'App_Code' within our entire application?
Ans. Yes. Files declared can be easily accessible through the project.

Click imagination hunt to see latest Blogs

Keep learning and sharing...
Read More »

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...