做了一个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>
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>
解决方案 »
- 为什么要点击两次按钮才会执行 javascript的代码?
- 关于url中可不可以隐藏传的参数的问题【请前辈,指点】
- 各位大神们帮忙解决一下吧 急啊 实在是看不懂啊!
- repeater显示方式求教。
- if (HttpContext.Current.Session["Id"]==null)都报错?
- 100 分求解 两个网站改成 一个网站二级目录
- 新手上路
- 两个表和在一起显示,怎么处理?
- 页面打开时如何最大化?
- 关于在Datalist等数据控件的模板中使用含有多选性质的控件的疑惑
- 怎么样让一个asp.net页面只在后台运行,前面不跳出窗口来?
- 经济情况 明明 是有实例的datagrid 他说没有 不知道是怎么一回事
,还是FormView-Panel-DropDownList的顺序??
根本不存在一个ID为"a"的控件,只存在一个代码中名字为a的控件,而且还是动态创建的。你是误解了ControlID这个属性的用法吧?其实真正需要你提供的不是一个代码名为"a"的控件,而是ID为"a"的控件。将ControlID改为"DropDownList1"就是你所需要做的。
参考
http://tag.csdn.net/Article/73a1088a-e0d3-47e7-82c0-5366a03497c9.html
http://topic.csdn.net/t/20050514/20/4007122.html
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();
}