Tuesday, March 23, 2010

Generic list Example

GenericSampleClass.aspx.cs:
---------------------------
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;

public partial class wfGenericSampleWithArg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GenericSampleWithArg objGenericSampleWithArg = new GenericSampleWithArg();
objGenericSampleWithArg.Push(9);
int Resultint = objGenericSampleWithArg.pop();

GenericSampleWithArg objGenericSampleWithArgstring = new GenericSampleWithArg();
objGenericSampleWithArgstring.Push("sri");
string Resultstring = objGenericSampleWithArgstring.pop();

drpCollection.DataSource = GetUserWithArg();
drpCollection.DataBind();

drpCollectionwithCalss.DataTextField = "userName";
drpCollectionwithCalss.DataSource = GetUserswithClass();
drpCollectionwithCalss.DataBind();
}

private object GetUserWithArg()
{
ListItemCollection addtCollection = new ListItemCollection();
for (int i = 0; i <= 5; i++)
{
addtCollection.Add(new ListItem("sri" + i.ToString()));
}
this.lstCollection = addtCollection;
return this.lstCollection;
}

private List GetUserswithClass()
{
List objListGenericSampleWithArg = new List();
for (int j = 0; j <= 9; j++)
{
objListGenericSampleWithArg.Add(new genericSamplewithClass(j, "sri" + j.ToString()));
}
this.users = objListGenericSampleWithArg;
return this.users;
}

private ListItemCollection _lstCollection;
public ListItemCollection lstCollection
{
get
{
return _lstCollection;
}
set
{
_lstCollection = value;
}
}

private List _users;
public List users
{
get
{
return _users;
}
set
{
_users = value;
}
}
}

genericSamplewithClass.cs:
--------------------------
using System;
using System.Data;
using System.Configuration;
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;

///
/// Summary description for genericSamplewithClass
///

public class genericSamplewithClass
{
public genericSamplewithClass()
{
//
// TODO: Add constructor logic here
//
}

public genericSamplewithClass(int userId, string userName)
{
this.userId = userId;
this.userName = userName;
}

public genericSamplewithClass(int userId, string userName, string address)
{
this.userId = userId;
this.userName = userName;
this.address = address;
}

private string _userName;
private int _userId;
private string _address;
public int userId
{
get
{
return _userId;
}
set
{
_userId = value;
}
}
public string userName
{
get
{
return _userName;
}
set
{
_userName = value;
}
}

public string address
{
get
{
return _address;
}
set
{
_address = value;
}
}
}

No comments:

Post a Comment