xml| Menu | The next year did not use. NET so much that he began to suspect that he had turned into a Delphi programmer. The project received at the beginning of the year, busy to September, sparse to the New Year's Day. A year's time, change too much. Vs.2005 released, the. NET framework also upgraded to 2.0, change too much, Microsoft again, when can stabilize? This speed, like our small coder how to follow AH.
Adp.net 2.0 provides several distinct data source objects that are used the to construct a common the framework for data -bound controls. Accessdatasource,sqldatasource,objectdatasource,datasetdatasource,xmldatasource.
And Delphi is almost the same, a few controls dragged in, the attribute set, data access on the OK. Data-driven WEB application development and so on, so, hehe.
Don't say much nonsense, back to the topic. Used to write a multilevel associated Drop-down menu, such as large categories, small categories, select provinces, display cities, and then to use JavaScript to write a function, the efficiency should be higher than XmlDataSource, the page does not have to return to the server. Today, the provinces of the city of the town written in XML, with XmlDataSource binding asp:dropdownlist in order to taste fresh.
I have an episode in the middle. Looking for a long time on the internet, did not find. Unexpectedly did not have the Chinese province city the XML document, I dizzy, did I write Ah, the province 31, the following city, county?? Finally find the National Bureau of Statistics website, there are 2005 of the latest administrative division, only HTML format, with XSL to turn? Not yet (don't despise, the Web page is also very non-standard). Save as text, one line to change? Too tired. Find a tool, huh? Txt2xml? Open source community has one, finally found the link down, found that the JDK must be installed. MD, there is no EXE program AH! In the face of that more than 3,000 lines of text, how do I organize into XML ah, depressed. At that time, I thought, if I develop a simple txt2xml may also be able to fire up, haha, good tools. It may be difficult to say. We'll talk about this later!
1. have their own XML. The format is as follows:
<?xml version= "1.0" encoding= "Utf-8" standalone= "yes"?>
<China>
<province name= "Beijing" postalcode= "110000" >
<city name= "Municipal District" postalcode= "110100" >
<county name= "Dongcheng District" postalcode= "110101"/>
<county name= "Xicheng District" postalcode= "110102"/>
<county name= "Chongwen District" postalcode= "110103"/>
..........
<county name= "Huairou" postalcode= "110116"/>
<county name= "Pinggu District" postalcode= "110117"/>
</City>
<city name= "County" postalcode= "110200" >
<county name= "Miyun County" postalcode= "110228"/>
<county name= "Yanqing County" postalcode= "110229"/>
</City>
</Province>
<province name= "Tianjin" postalcode= "120000" >
<city name= "Municipal District" postalcode= "120100" >
<county name= "Zone of Peace" postalcode= "120101"/>
...........
<county name= "Xiqing District" postalcode= "120111"/>
<county name= "Jinnan District" postalcode= "120112"/>
<county name= "Beichen District" postalcode= "120113"/>
<county name= "Wuqing District" postalcode= "120114"/>
<county name= "Baochi District" postalcode= "120115"/>
</City>
<city name= "County" postalcode= "120200" >
<county name= "Ninghe County" postalcode= "120221"/>
<county name= "Jinghai County" postalcode= "120223"/>
<county name= "Jixian" postalcode= "120225"/>
</City>
</Province>
</China>
2. Establish three DropDownList (AutoPostBack is true) and three XmlDataSource, the province list is directly bound.
<form id= "Form1" runat= "Server" >
<div>
<label> Provinces </label>
<asp:dropdownlist id= "Provinces" runat= "Server" datasourceid= "provincelist" datatextfield= "Name" DataValueField= "PostalCode" onselectedindexchanged= "provinces_selectedindexchanged" autopostback= "True" >
</asp:dropdownlist><asp:xmldatasource id= "provincelist" runat= "Server" datafile= "~/incxml/State.xml"
Xpath= "China/province" ></asp:XmlDataSource>
<label> City </label>
<asp:dropdownlist id= "Cities" runat= "Server" autopostback= "True" onselectedindexchanged= "Cities_" SelectedIndexChanged ">
</asp:dropdownlist><asp:xmldatasource id= "CityList" runat= "Server" datafile= "~/incxml/state.xml"
Xpath= "China/province/city" ></asp:XmlDataSource>
<label> Counties </label>
<asp:dropdownlist id= "County" runat= "Server" >
</asp:dropdownlist><asp:xmldatasource id= "countylist" runat= "Server" datafile= "~/incxml/state.xml"
Xpath= "China/province/city/county" ></asp:XmlDataSource>
</div>
</form>
3. OnSelectedIndexChanged event for the provinces and Cities list, specify the DataSourceID attribute of the corresponding list, and change the corresponding XmlDataSource XPath property
protected void Provinces_selectedindexchanged (object sender, EventArgs e)
{
Citylist.xpath = "china/province[@PostalCode =" + Provinces.selectedvalue + "]/city";
Cities.datasourceid = "CityList";
Cities.datatextfield = "Name";
Cities.datavaluefield = "PostalCode";
}
protected void Cities_selectedindexchanged (object sender, EventArgs e)
{
Countylist.xpath = "china/province[@PostalCode =" + Provinces.selectedvalue + "]/city[@PostalCode =" + Cities.selectedvalue + "]/county";
County.datasourceid = "Countylist";
County.datatextfield = "Name";
County.datavaluefield = "PostalCode";
}