C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET WebForms »

Function to Populate ASP.NET Dropdown Combobox dynamically with XML Configuration file.


Posted Date: 13 Jun 2007    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: Brij MohanMember Level: Bronze    
Rating: 1 out of 5Points: 10



This code explains how to populate ASP.NET drop down list dynamically with XML configuration file.
This code reads XML file using XPATH and Populates the Values in ASP.NET Dropdown list control.

ASPX Page code, In page load event

if(!Page.IsPostBack)
{
this.PopulateControls(Server.MapPath("RegistrationConfig.xml"));
}


Code to Populate the controls, function called from Page Load event,
RegistrationConfig Class will have GetXMLConfig() method, which will read the XML configuration file and stores into CustomerRegistrationDetail Class as Detail object

public void PopulateControls(string fileName)
{
try
{
CustomerRegistrationDetail custDet=new CustomerRegistrationDetail();
RegistrationConfig getConfig=new RegistrationConfig();
custDet = getConfig.GetXMLConfig(fileName);
cblRegions.DataSource=custDet.ArlCustomerRegions;
cblRegions.DataBind();
cblTypes.DataSource=custDet.ArlCustomerType;
cblTypes.DataBind();
}
catch(Exception ex)
{
lblMessage.Text=ex.Message;
}
}


Code to ReadXML file using XPATH and populate ArrayList which is a member of CustomerDetail Class


public CustomerRegistrationDetail GetXMLConfig(string fileName)
{
CustomerRegistrationDetail det=new CustomerRegistrationDetail ();
XmlDocument doc = new XmlDocument();
XPathDocument xpathDoc = new XPathDocument(fileName);
XPathNavigator nav=xpathDoc.CreateNavigator();

// Compile a standard XPath expression
XPathExpression expr;

//Get Custoer Regions
expr = nav.Compile("//CustomerRegions/Value");
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
det.ArlCustomerRegions =GetArrayList(nav2.Value);
}

//Get Customer Types
expr = nav.Compile("//CustomerType/Value");
iterator = nav.Select(expr);
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
det.ArlCustomerType =GetArrayList(nav2.Value);
}


//return the detail object containing all the configuration paramaters
return det;
}

//Utility Function used in above function Function
private ArrayList GetArrayList(string strCustParam)
{
string[] arCust=strCustParam.Split(';');
ArrayList arlRetVal=new ArrayList();
for(int i=0;i {
arlRetVal.Add(arCust[i]);
}
return arlRetVal;
}


CustomerRegistrationDetail Class

public class CustomerRegistrationDetail
{
//variables used to populate Combo boxes in UI
public ArrayList ArlCustomerRegions=null;
public ArrayList ArlCustomerType=null;
}


XML Configuration File




North West;North East;Yorkshire and HumberSide;West Midlands;East Midlands;London;East of England;South West;South East;Scotland;Wales;Northern Ireland



Corporate end-users;Small Business end-users;Residential end-users;Via resellers and dealers






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Automatically generated autonumber in datagarid
Previous Resource: Net DataGrid DateTimePicker column Style.
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use