简体   繁体   中英

Classes and object in C# Asp.Net

i am trying to learn classes and objects in C#, i want to get the textbox value and show it in a label using classes and get set property. i try the below process, but i will not showing/output anything.

index.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="classes.Index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="fname" runat="server" /><br />
        <asp:TextBox ID="lname" runat="server" /><br />
        <asp:TextBox ID="password" runat="server" /><br />
        <asp:Button Text="Submit" ID="submit" runat="server" OnClick="submit_Click" />
        <br />
        <br />
        <br />
        <br />
        <asp:Label ID="FirstName" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

Button click code

 protected void submit_Click(object sender, EventArgs e)
        {
            basicinfo bn = new basicinfo();
            FirstName.Text = bn.fname;
        }

class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace classes
{
    public class basicinfo
    {
        public string fname;
        public string last_name;
        public string password;
        public string Name
        {
            get{return fname;}
            set{fname=value;}
        }

    }

    }

Can someone tell me is this the wrong way? Also please provide reference of any helping material/Links/video tutorials course through which i can clear my basic classes, get set, objects, methods idea, i am heaving trouble to understand it.

Update if this how it works

protected void submit_Click(object sender, EventArgs e)
        {
            basicinfo bn = new basicinfo();
            bn.Name = fname.Text;
            FirstName.Text =bn.Name;
        }

then why should we use classes and get, set properties?

we can simply do like

protected void submit_Click(object sender, EventArgs e)
        {

            FirstName.Text = fname.Text;
        }

You are trying to save the object value in the Label but Your object is empty it does not contain textbox value. Your code should be

 protected void submit_Click(object sender, EventArgs e)
    {
        basicinfo bn = new basicinfo();
        bn.fname= fname.Text; //textbox value to object
        FirstName.Text = bn.fname; //object value to label
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM