Tuesday 13 May 2014

Generics in asp.net

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace Test_n.G
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List names = new List();
names.Add("A");
names.Add("B");
names.Add("C");
lboxStrings.DataSource = names;
lboxStrings.DataBind();
string temp1 = names[2];
Customer c1 = new Customer("1", "A", "AA", "123-4567");
Customer c2 = new Customer("2", "B", "BB", "123-7823");
Customer c3 = new Customer("3", "C", "CC", "123-6383");
List customers = new List();
customers.Add(c1);
customers.Add(c2);
customers.Add(c3);
Customer c = customers[2];
customers[1] = c;
lboxCustomers.DataSource = customers;
lboxCustomers.DataBind();
Dictionary dict = new Dictionary();
dict.Add(c1.Id, c1);
dict.Add(c2.Id, c2);
lboxDictionary.DataSource = dict;
lboxDictionary.DataBind();
}
}
public abstract class AbstractEntity
{
private string _id;
public AbstractEntity(string id)
{
_id = id;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public abstract bool IsValid
{
get;
}
}
public class Customer : AbstractEntity
{
private string _firstName;
private string _lastName;
private string _phone;
public Customer(string id, string firstName, string lastName, string phone)
: base(id)
{
_firstName = firstName;
_lastName = lastName;
_phone = phone;
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
public string Name
{
get { return LastName + ", " + FirstName; }
}
public override bool IsValid
{
get
{
if (Id.Length > 0 && LastName.Length > 0)
return true;
else
return false;
}
}
public override string ToString()
{
return Id + "," + Name + "," + Phone;
}
}
}
--------------------
asp:ListBox ID="lboxStrings" runat="server"
asp:ListBox ID="lboxCustomers" runat="server"
DataTextField="Name"
DataValueField="Id"
asp:ListBox ID="lboxDictionary" runat="server"
DataTextField="Value"
DataValueField="Key"
----------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace Test_narayana.G
{
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Author A1 = new Author(TextBox1.Text, TextBox2.Text, TextBox3.Text, "123-4567");
Author A2 = new Author("TTT", "B", "BB", "123-7823");
Author A3 = new Author("MS", "C", "CC", "123-6383");
Dictionary dict = new Dictionary();
dict.Add(A1.Id, A1);
dict.Add(A2.Id, A2);
dict.Add(A3.Id, A3);
lboxDictionary.DataSource = dict;
lboxDictionary.DataBind();
ReadDic(dict);
}
protected void ReadDic(Dictionary dict)
{
//Read Dictionary
foreach (KeyValuePair kv in dict)
{
Response.Write(kv.Key + ":" + kv.Value);
Author au = kv.Value;
Response.Write(au.Id);
Response.Write(au.ItemURL);
Response.Write(au.AuthorName);
Response.Write(au.searchKeyWord);
}
}
}
public abstract class AAbstractEntity
{
private string _id;
public AAbstractEntity(string id)
{
_id = id;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public abstract bool IsValid
{
get;
}
}
public class Author : AAbstractEntity
{
private string _ItemURL;
private string _AuthorName;
private string _searchKeyWord;
public Author(string id, string ItemURL, string AuthorName, string searchKeyWord)
: base(id)
{
_ItemURL = ItemURL;
_AuthorName = AuthorName;
_searchKeyWord = searchKeyWord;
}
public string ItemURL
{
get { return _ItemURL; }
set { _ItemURL = value; }
}
public string AuthorName
{
get { return _AuthorName; }
set { _AuthorName = value; }
}
public string searchKeyWord
{
get { return _searchKeyWord; }
set { _searchKeyWord = value; }
}
//public string Name
//{
// get { return AuthorName + ", " + ItemURL; }
//}
public override bool IsValid
{
get
{
if (Id.Length > 0 && AuthorName.Length > 0)
return true;
else
return false;
}
}
//public override string ToString()
//{
// return Id + "," + Name + "," + searchKeyWord;
//}
public override string ToString()
{
return Id + "," + AuthorName + ", " + ItemURL + "," + searchKeyWord;
}
}
}