Web Forms

This technology is still available under the Web project templates of Visual Studio 2017.

image

A simple sample could consists of a dropbox, a button and a table.

 
<%@ Page Title="My Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" 
    CodeFile="MyPage.aspx.cs" Inherits="MyPage" Async="true" AsyncTimeout="3" %>

<asp:Content ID="BodyContent"  ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %>.</h2>
    <h3>This is my page.</h3>
        <div>
            <br />
            <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"  AutoPostBack="true" >
            </asp:DropDownList>
            <asp:TextBox ReadOnly="true" ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="BtnAdd" runat="server" Text="Add to table" OnClick="BtnAdd_Click"/>
            <br />
            <br />
            <asp:GridView ID="GvPersons" runat="server">
            </asp:GridView>
            <asp:Button ID="Button1" runat="server" Text="Refresh table" OnClick="Refresh_Click"/>
            <br />
            <br />
        </div>
</asp:Content>

 

In this simplified sample the events fire a  postback in the code behind.

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI;

public class Person
{
    private string name;
    private string surname;
    private string id;

    public string Name { get => name; set => name = value; }
    public string Surname { get => surname; set => surname = value; }
    public string FullName { get => name + " " + surname; }
    public string Id { get => id; set => id = value; }
}

public partial class MyPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataTextField = "FullName";
            DropDownList1.DataValueField = "Id";
            DropDownList1.DataSource = MyComboList;
            DropDownList1.DataBind();
            TextBox1.Text = DropDownList1.SelectedItem.Text;
            //RegisterAsyncTask(new PageAsyncTask(RefreshPersonTable));
        }
    }

    private async Task RefreshPersonTable(CancellationToken cancellationToken)
    {
        await Task.Delay(2000, cancellationToken);
        {
            GvPersons.DataSource = Persons;
            GvPersons.DataBind();
        }
    }

    private Person[] myComboList = new Person[2] 
    {
            new Person() { Id = "1", Name = "Johnny", Surname = "English" },
            new Person() { Id = "2", Name = "Eva", Surname = "Summer" }
    };
    public Person[] MyComboList{ get => myComboList; set => myComboList = value; }

    private static List persons = new List();
    public static List Persons { get => persons; set => persons = value; }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = DropDownList1.SelectedItem.Text;
    }

    static int counter = 0;
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        var selected = myComboList.FirstOrDefault(p => p.Id == DropDownList1.SelectedValue);
        if (selected != null)
            Persons.Add(new Person() { Name = selected.Name, Surname = selected.Surname, Id = (counter++).ToString() });
        RegisterAsyncTask(new PageAsyncTask(RefreshPersonTable));
    }
    protected void Refresh_Click(object sender, EventArgs e)
    {
        GvPersons.DataSource = null;
        GvPersons.DataBind();
        RegisterAsyncTask(new PageAsyncTask(RefreshPersonTable)); 
    }
}

 

In the next post we will see how to implement Ajax and Web Method instead.

 

 

 

Advertisements

One thought on “Web Forms

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s