本人超笨,所以向各位求教了。
两个Dropdownlist:dl1和dl2,其中dl1直接在页面中,dl2在DataGrid1的footer中。
我的设想是,在aspx中不做任何绑定,当Page_Load时绑定dl1到“省份”字段;当dl1发生OnSelectedIndexChanged时在后台的dl1_SelectedIndexChanged事件中绑定dl2到“地区”字段,并实现联动。
问题出在后面的dl1_SelectedIndexChanged事件中——当dl1的值changed时,dl2没任何反应。下面是我的代码片段,请不吝赐教,争取下班之前散分完毕。注意两个dropdownlist控件的位置!        string strmysql2 = "select * from area where arProv = '" + dl1.Text + "'";
        SqlDataAdapter da2 = new SqlDataAdapter(strmysql2, cnn);
        DataSet ds2 = new DataSet();
        foreach (DataGridItem item in dgIn.Controls[0].Controls)
        {
            if (item.ItemType == ListItemType.Footer)
            {
                Dropdownlist t2 =    (Dropdownlist)item.FindControl("dl2");
                da2.Fill(ds2, "area");
                t2.DataSource = ds2;
                t2.DataTextField = "arName";
                t2.DataValueField = "arName";
                t2.DataBind();
                DataGrid1.DataBind();
            }
        }

解决方案 »

  1.   

    试下把D1的Autopost(自动回发服务器)的值设为TRUE
      

  2.   

    你说的没反映是页面没回发触发事件还是触发了事件dl2里没有Item
      

  3.   

    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 = "地市信息文件丢失!";
                    }
                }
            }        Web 窗体设计器生成的代码#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 = "地市信息文件丢失!";
                }
            }    }
    }
      

  4.   

    Re:qiongl(路人甲)
    已经设为TRUE了。Re:pontus(pontus)
    触发了事件,但dl2里面是空的。Re: q_po_o(两个人)
    你的代码好像很复杂耶,是不是XML?我看不懂。好像跟我的问题不大一致,有空研究研究吧,感谢!!
      

  5.   

    da2.Fill(ds2, "area");
    t2.DataSource = ds2;
    改成:t2.DataSource = ds2.Tables["area"];
      

  6.   

    Re:pontus(pontus) 
    是不是将我上面的那段代码复制到DataGrid1_ItemDataBound事件中??
    试过了,好象不行哦。Re:bitliuyang(昊天)
    不行!
      

  7.   

    是不是因为数据绑定的是t2而不是dl2,所以dl2当然是空的???
    要这样子的话,是不是要把t2的Item集再传回dl2???怎么样传呢???
    不知道这样理解是否合理???
    下班了,回家继续等待高手们的帮助哦!!!
      

  8.   

    地区联动干嘛不用javascript在客户端完成呢
      

  9.   

    http://singlepine.cnblogs.com/articles/266538.html
      

  10.   

    如果你把  DataGrid1.DataBind();注释掉,看看行不行。
      

  11.   

    Re:kevin_gao(困了!累了!睡觉了!) 
    我的程序是要绑定到后台数据库的,用客户端与服务器端有什么不同?况且我不会JavaScript。Re:singlepine(小山)
    中间那段ddlpovince_SelectedIndexChanged事件代码跟我的代码应该是一样的吧?不知道为什么我的不行呢?
    另外我想请教:btndelete.Attributes.Add("onclick","Delete();");这条语句后而的双引号中引用的事件是不是只能为客户端事件?如果要引用服务器端事件(即后台cs或vb文件中定义的事件)要怎么做?Re:bitliuyang(昊天)
    为何要去掉那句?DataGrid中的控件绑定了之后不用对DataGrid进行重新绑定吗?我去掉了之后并没有影响程序执行,只是还是不行呢。
      

  12.   

    呼呼,在前台对控件进行数据绑定后,终于达到我要的效果了。不过问题好像还没有解决:(不管了,以后有空再研究研究吧。开始散分了,感谢各位的大力支持。TO:singlepine(小山)
    关于btndelete.Attributes.Add("onclick","Delete();");对服务器端事件的调用的问题希望你知道的话能帮忙解答一下,谢谢哦。/
      

  13.   

    btndelete.Attributes.Add("onclick","Delete();");
    是后台调用前台方法,主要实现删除前判断和无刷新删除,直接在后台删除也是可以的,如
    foreach(DataGridItem thisitem in DataGrid1.Items) 

     if(((CheckBox)thisitem.Cells[0].Controls[1]).Checked) 
     { 
      string strloginid= DataGrid1.DataKeys[thisitem.ItemIndex].ToString(); 
      Del (strloginid); //删除函数 
     }