做了一个FormView提交一些信息,其中包括两个DropDownList,第一个DropDownList是省份,另一个DropDownList是城市,分别绑定于两个SqlDataSource,想选择第一个DropDownList后,自动回送,然后页面在第二个DropDownList显示对应省份的城市,试了N遍各种方法仍不得成功,主要代码如下,vs2005系统提示在第二个数据源里死活找不到控件a,希望高手指点:<script>
DropDownList a = new DropDownList();protected void OnSelectedIndexChanged_FirstClass(object sender, EventArgs e)
{
  a = (DropDownList)FormView1.FindControl("DropDownList1"); //这一段代码放到其他方法,如                                Page_Load等中也没有用
}</script>   <asp:DropDownList ID="DropDownList1" runat="server" Width="220px"
    SelectedValue='<%# Bind("province") %>' DataSourceID="SqlDataSource1"
    DataTextField="province" DataValueField="provinceId" 
    AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged_FirstClass" >
    </asp:DropDownList>
  
    <asp:DropDownList ID="DropDownList2" runat="server" Width="220px"
    SelectedValue='<%# Bind("city") %>' DataSourceID="SqlDataSource2"
    DataTextField="city" DataValueField="city" > 
    </asp:DropDownList>    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" 
    SelectCommand="SELECT DISTINCT [province],[provinceId] FROM [place]">
    </asp:SqlDataSource>
    
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" 
    SelectCommand="SELECT DISTINCT [cityId], [city], [provinceId] 
    FROM [place] WHERE ([provinceid] = @provinceId)">
        <SelectParameters>
          <asp:ControlParameter ControlID="a" DefaultValue="1" 
             PropertyName="SelectedValue" Name="provinceId" Type="string" />              
        </SelectParameters>
    </asp:SqlDataSource>

解决方案 »

  1.   

    顶一个,用过FormView.Row.FindControl,没作用
      

  2.   

    在第一个DropDownList控件中的SelectedIndexChanged事件中来绑定第二个DropDownList控件数据,并显示,不就OK了
      

  3.   

    照kbxj406(羽儿)的办法,系统显示如下:,说白了就是找不到DropDownList控件,如果是TextBox却可以找到系统显示错误:Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidOperationException: Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。
      

  4.   

    你把DropDownList1加到一个Panel控件中后,用a = (DropDownList)Panel1.FindControl("DropDownList1");看能否找到DropDownList1控件.
      

  5.   

    请问楼上,你说的是在最外层放Panel,然后是FormView,再里面是DropDownList
    ,还是FormView-Panel-DropDownList的顺序??
      

  6.   

    试过了,也不行,提示找不到Panel1
      

  7.   

    顶一个,诺大个csdn竟没人解答这么简单的问题?
      

  8.   

    <asp:ControlParameter ControlID="a" DefaultValue="1" PropertyName="SelectedValue" Name="provinceId" Type="string" />
    根本不存在一个ID为"a"的控件,只存在一个代码中名字为a的控件,而且还是动态创建的。你是误解了ControlID这个属性的用法吧?其实真正需要你提供的不是一个代码名为"a"的控件,而是ID为"a"的控件。将ControlID改为"DropDownList1"就是你所需要做的。
      

  9.   

    问题是不是找不到控件了?
    参考
    http://tag.csdn.net/Article/73a1088a-e0d3-47e7-82c0-5366a03497c9.html
    http://topic.csdn.net/t/20050514/20/4007122.html
      

  10.   

    难道就没人在FormView用两个DropDownList吗?
      

  11.   

    你的dropdownlist是从数据库取的数么?如果是,就在库里面选择值=第一个selectedvalue的然后重新data绑定就可以了。
      

  12.   

    //市
    private void DropDownList2_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    dqdata_load3();
    }
    //县
    private void DropDownList3_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    dqdata_load4();
    }
    #region 地区和人员数据初始化
    //提取省的纪录
    private void dqdata_load1()

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    string cmdText = "select distinct 省名,省代码,id from SYS_省市行政区划  order by id";
    SqlCommand myCommand = new SqlCommand (cmdText,myConnection);
    myConnection.Open();
    SqlDataReader recm = myCommand.ExecuteReader();  
    DropDownList1.DataSource = recm;
    DropDownList1.DataTextField ="省名";
    DropDownList1.DataValueField ="省名";
    DropDownList1.DataBind();
    recm.Close();
    myConnection.Close();
    }
    //提取市的纪录
    private void dqdata_load2()

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    string cmdText = "select distinct rtrim(市名) as 市名,行政代码,id from SYS_行政区划表 where substring(convert(char,行政代码),3,4)<>'0000' and substring(convert(char,行政代码),3,2)<>'00' and substring(convert(char,行政代码),1,3)<>'429' and substring(convert(char,行政代码),5,2)='00' order by id";
    SqlCommand myCommand = new SqlCommand (cmdText,myConnection);
    myConnection.Open();
    SqlDataReader recm = myCommand.ExecuteReader();  
    DropDownList2.DataSource = recm;
    DropDownList2.DataTextField ="市名";
    DropDownList2.DataValueField ="行政代码";
    DropDownList2.DataBind();
    recm.Close();
    myConnection.Close();
    DropDownList2.SelectedIndex =-1;
    } //页面初始化时提取县的纪录
    private void dqdata_load31()

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    string cmdText = "select distinct rtrim(县名) as 县名,行政代码,id from SYS_行政区划表 where substring(convert(char,行政代码),3,4)<>'0000' and substring(convert(char,行政代码),5,2)<>'00' and substring(convert(char,行政代码),3,2)<>'00' and substring(convert(char,行政代码),1,3)<>'429' order by id";
    SqlCommand myCommand = new SqlCommand (cmdText,myConnection);
    myConnection.Open();
    SqlDataReader recm = myCommand.ExecuteReader();  
    DropDownList3.DataSource = recm;
    DropDownList3.DataTextField ="县名";
    DropDownList3.DataValueField ="行政代码";
    DropDownList3.DataBind();
    recm.Close();
    myConnection.Close();
    }
    //提取县的纪录
    private void dqdata_load3()

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    string cmdText = "select distinct rtrim(县名) as 县名,行政代码,id from SYS_行政区划表 where substring(convert(char,行政代码),3,4)<>'0000' and substring(convert(char,行政代码),5,2)<>'00' and substring(convert(char,行政代码),3,2)<>'00' and substring(convert(char,行政代码),1,3)<>'429' and 市名='"
    +DropDownList2.SelectedItem.ToString().Trim()+"' order by id";
    SqlCommand myCommand = new SqlCommand (cmdText,myConnection);
    myConnection.Open();
    SqlDataReader recm = myCommand.ExecuteReader();  
    DropDownList3.DataSource = recm;
    DropDownList3.DataTextField ="县名";
    DropDownList3.DataValueField ="行政代码";
    DropDownList3.DataBind();
    recm.Close();
    myConnection.Close();
    }
    //提取乡镇的纪录
    private void dqdata_load4()

    SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    string cmdText = "select distinct rtrim(乡镇名称) as 乡镇名称,行政代码,id from qcqf_乡镇名称表 where 县级行政代码='"+DropDownList3.SelectedValue.ToString().Trim() + "' order by id";
    SqlCommand myCommand = new SqlCommand (cmdText,myConnection);
    myConnection.Open();
    SqlDataReader recm = myCommand.ExecuteReader();  
    DropDownList4.DataSource = recm;
    DropDownList4.DataTextField ="乡镇名称";
    DropDownList4.DataValueField ="行政代码";
    DropDownList4.DataBind();
    recm.Close();
    myConnection.Close();
    }
      

  13.   

    谢谢楼上的好心相助,关键是找不到DropDown控件,不是别的