Here is the code to add countries in your DropDownList using WebService.
Here I am using a webservice from http://www.webservicex.net and a link to the webservice used is http://www.webservicex.net/country.asmx.
In the .aspx page put a DropDownList as in the following:
Here I am using a webservice from http://www.webservicex.net and a link to the webservice used is http://www.webservicex.net/country.asmx.
In the .aspx page put a DropDownList as in the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:DropDownList ID="drpCNT" runat="server"></asp:DropDownList>
</div>
</form>
</body>
</html>
Now to add the reference of your webservice, do following procedure:
Now to add the reference of your webservice, do following procedure:
- Open Solution Explorer, select your website and right-click and select "Add Web Reference"
- Now the following window will be opened, enter "http://www.webservicex.net/country.asmx" in the URL area that is circled in the image and press Enter.
- Now change Web reference name (optional) and click the "Add Reference" Button (circled in the image).
After that it will create the following structure in Solution Explorer.
- Now put the following code in the .aspx.cs file (explained in code):
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;using System.Text;public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){if (IsPostBack == false){//get reference of your webservicemyservice.country ct = new myservice.country();// str is an XML String which will hold all the countries in xml formatstring str = ct.GetCountries();// add first item in dropdownlistdrpCNT.Items.Add("-Select-");//Create an XML Document and load your XMLXmlDocument doc = new XmlDocument();doc.LoadXml(str);//Get your nodes, here our node in Table because webservice will give us following format// http://www.webservicex.net/country.asmx/GetCountriesXmlNodeList nodes = doc.DocumentElement.SelectNodes("//Table");//Iterates for xml nodes and add them in dropdownlistforeach (XmlNode node in nodes){drpCNT.Items.Add(node["Name"].InnerText);}}}}
- Finally you will get all the countries in your DropDownList.
No comments :
Post a Comment