Monday, November 23, 2009

To add controls dynamically in Web Application

Steps:

i)To add ContentHolder control in Webpage

ii) Create an object for Label

Label l = new Label();
l.Text="Hai"

iii) PlaceHolder1.Controls.Add(l);

To Load All Country Names in DropDownList

private void PopulateCountryName(DropDownList dropDown)
{
Dictionary<string, string> objDic = new Dictionary<string, string>();

foreach (CultureInfo ObjCultureInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{

RegionInfo objRegionInfo = new RegionInfo(ObjCultureInfo.Name);

if (!objDic.ContainsKey(objRegionInfo.EnglishName))
{
objDic.Add(objRegionInfo.EnglishName, objRegionInfo.TwoLetterISORegionName.ToLower());
}
}
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(objDic);
myList.Sort(

delegate(KeyValuePair<string, string> firstPair,

KeyValuePair<string, string> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
foreach (KeyValuePair<string, string> val in myList)
{
dropDown.Items.Add(new ListItem(val.Key, val.Value));
}
}