.NET_STAR

打造技术团队,愿与您共同开创事业!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace WebApplication2
{
    
/// <summary>
    
/// WebForm1 的摘要说明。
    
/// </summary>

    public class WebForm1 : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DropDownList DropDownListProvince;
        
protected System.Web.UI.WebControls.DropDownList DropDownListCity;
        
protected System.Web.UI.WebControls.Label LabelMessage;

        
public string City = "广州";
        
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            string CurrentPath = this.Server.MapPath(".");

            
if (!Page.IsPostBack)
            
{
                
if(System.IO.File.Exists(CurrentPath + "\\Province.xml"))
                
{
                    
this.DropDownListProvince.Items.Clear();

                    System.Xml.XmlDocument doc 
= new System.Xml.XmlDocument();
                    doc.Load(CurrentPath 
+ "\\Province.xml");

                    XmlNodeList nodes 
= doc.DocumentElement.ChildNodes;

                    XmlNode node1 
= doc.DocumentElement.SelectSingleNode(@"Province/City[@Name='" + this.City + "']");

                    
foreach(XmlNode node in nodes)
                    
{
                        
this.DropDownListProvince.Items.Add(node.Attributes["Name"].Value);
                        
int n = this.DropDownListProvince.Items.Count - 1;
                    
                        
if(node1 != null && node == node1.ParentNode)
                            
this.DropDownListProvince.SelectedIndex = n;
                    }

                    DropDownListProvince_SelectedIndexChanged(sender,e);
                }

                
else
                
{
                    
this.LabelMessage.Text = "地市信息文件丢失!";
                }

            }

        }


        
#region Web 窗体设计器生成的代码
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{    
            
this.DropDownListProvince.SelectedIndexChanged += new System.EventHandler(this.DropDownListProvince_SelectedIndexChanged);
            
this.Load += new System.EventHandler(this.Page_Load);

        }

        
#endregion


        
private void DropDownListProvince_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
string CurrentPath = this.Server.MapPath(".");

            
if(System.IO.File.Exists(CurrentPath + "\\Province.xml"))
            
{
                
this.DropDownListCity.Items.Clear();

                System.Xml.XmlDocument doc 
= new System.Xml.XmlDocument();
                doc.Load(CurrentPath 
+ "\\Province.xml");

                XmlNodeList nodes 
= doc.DocumentElement.ChildNodes[this.DropDownListProvince.SelectedIndex].ChildNodes;

                
foreach(XmlNode node in nodes)
                
{
                    
this.DropDownListCity.Items.Add(node.Attributes["Name"].Value);
                    
int n = this.DropDownListCity.Items.Count - 1;
                    
if(node.Attributes["Name"].Value == this.City)
                    
{
                        
this.DropDownListCity.SelectedIndex = n;
                    }

                }


                
if(this.DropDownListCity.SelectedIndex == -1)
                    
this.DropDownListCity.SelectedIndex = 0;
            }

            
else
            
{
                
this.LabelMessage.Text = "地市信息文件丢失!";
            }

        }


    }

}


Province.xml 文件:/Files/lmjob/Province.xml
posted on 2008-12-11 22:40  雷明  阅读(978)  评论(0编辑  收藏  举报