1 Replies - 1787 Views - Last Post: 23 November 2012 - 09:30 AM Rate Topic: -----

#1 SuperSam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-November 12

Help understanding object oriented ASP.Net using C#

Posted 22 November 2012 - 11:12 AM

I am having some trouble understanding how to create websites using visual web developer in a way that would make them object oriented.
Let's say you want to make a store that sells bikes. You create a bikes.aspx page to handle the HTML code for your page, a bikes.aspx.cs page for all the SQL database interaction and a bikes.cs file which contains getter and setter methods for variables such as bikeModel, price, and topSpeed.
Is it possible to link all these files so that you can assign whatever is in your database to the variables in the C# file and use this in the webpage to display all items?

I know there are quicker ways of doing this but this way would make the code much cleaner. I cant seem to find any useful tutorials on this problem and I have been spending a long time trying different ways to link these files.

All help and pointers would be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Help understanding object oriented ASP.Net using C#

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Re: Help understanding object oriented ASP.Net using C#

Posted 23 November 2012 - 09:29 AM

Since you are using ASP.NET WebForms you could look at using the MVP pattern. This is a variation of the more well known MVC pattern. MVP stands for Model-View-Presenter. The general idea is to create an interface that will define the properties that your page will implement. You then have your page inherit from that interface. The presenter class is basically responsible for initializing and handling any changes to the properties defined by the interface. The presenter itself is usually not aware of the database, it instead passes the values to whatever classes you have defined to handle the database operations, such as a repository class or something similar.

Here's a simple "Hello World" example. It will simply allow a user to enter their first and last names then it will return a message to them using their full name.

First let's define a simple interface to represent the view.

    public interface IHomeView
    {
        string FirstName { get; set; }
        string LastName { get; set; }
        string FullName { get; set; }
    }



The aspx page is pretty simple, just a couple of text box controls and a label.

    <div>
        First Name:
        <asp:TextBox runat="server" ID="txtFirstName" />
        <br />
        Last Name:
        <asp:TextBox runat="server" ID="txtLastName" />
    </div>
    <br />
    <div>
        Full Name: 
        <asp:Label Text="" runat="server" ID=lblFullName" />
    </div>
    <br />
    <asp:Button ID="btnGetMessage" runat="server" onclick="btnGetMessage_Click" Text="Submit" />



In the code-behind, the page needs to implement the view interface and the controls need to be tied to the view. This is easily done by creating a couple of properties for the page. The code-behind will also contain a private field for the presenter. The presenter gets initialized in the Init event for the page and the presenter's Display method gets called during the first page load event. In the button's click event the presenter's GetMessage method is called to update the page's FullName property.

    public partial class _Default : System.Web.UI.Page, IHomeView
    {
        private HomePagePresenter presenter;

        public string FirstName
        {
            get { return txtFirstName.Text; }
            set { txtFirstName.Text = value; }
        }

        public string LastName
        {
            get { return txtLastName.Text; }
            set { txtLastName.Text = value; }
        }

        public string FullName
        {
            get { return lblFullName.Text; }
            set { lblFullName.Text = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            // The page passes itself into the presenter
            presenter = new HomePagePresenter(this);           
            base.OnInit(e);
        }

        protected override void onload(EventArgs e)
        {
            if (!IsPostBack)
            {
                presenter.Display();
            }
        }

        protected void btnGetMessage_Click(object sender, EventArgs e)
        {
            presenter.SetMessage();
        }
    }



And here's the presenter.

    public class HomePagePresenter : SampleApplication.Presenter.IPresenter
    {
        private IHomeView view;

        public HomePagePresenter(IHomeView homeView)
        {
            this.view = homeView;
        }

        public void Display()
        {
            this.view.FullName = "Enter your first and last name for a special message!";
            this.view.FirstName = string.Empty;
            this.view.LastName = string.Empty;
        }

        public void SetMessage()
        {
            this.view.FullName = string.Format("Hello there, {0} {1}", this.view.FirstName, this.view.LastName);

            // Reset input fields
            this.view.FirstName = string.Empty;
            this.view.LastName = string.Empty;
        }
    }



This is a very simplified implementation and doesn't have any sort of database interaction, but hopefully it helps some. Also, if you google "Model View Presenter" you should get back quite a bit of info on this pattern.

This post has been edited by Nakor: 23 November 2012 - 09:32 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1