FormView控件是可及显示、修改、添加、删除为一体的控件,感觉很好用,可是昨天发现了一个可以说是它的一个Bug吧,我是想要实现下拉框的联动效果,比如在A下拉框选择了省对应B的下拉框会把对应A中省的市显示在B下拉框中,我想要实现的是校区和对应校区建筑的联动效果,单纯的这种效果很好实现比如下面的代码
<
asp:DropDownList
ID
="DropDownList2"
runat
="server"
DataSourceID
="SqlDataSource2"
DataTextField ="校区简称" DataValueField ="校区代码" AutoPostBack="true" >
</ asp:DropDownList >
< asp:DropDownList ID ="DropDownList3" runat ="server" DataSourceID ="SqlDataSource3"
DataTextField ="楼名称" DataValueField ="楼代码" >
</ asp:DropDownList >< asp:SqlDataSource ID ="SqlDataSource3" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [楼名称], [楼代码] FROM [C_楼名代码表] WHERE ([校区] = @校区)" >
< SelectParameters >
< asp:ControlParameter ControlID ="DropDownList2" Name ="校区" PropertyName ="SelectedValue"
Type ="Int32" />
</ SelectParameters >
</ asp:SqlDataSource >
< asp:SqlDataSource ID ="SqlDataSource2" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [校区代码], [校区简称] FROM [C_校区代码表]" ></ asp:SqlDataSource >
只需要这样就可以了,不用再CS中编写代码
DataTextField ="校区简称" DataValueField ="校区代码" AutoPostBack="true" >
</ asp:DropDownList >
< asp:DropDownList ID ="DropDownList3" runat ="server" DataSourceID ="SqlDataSource3"
DataTextField ="楼名称" DataValueField ="楼代码" >
</ asp:DropDownList >< asp:SqlDataSource ID ="SqlDataSource3" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [楼名称], [楼代码] FROM [C_楼名代码表] WHERE ([校区] = @校区)" >
< SelectParameters >
< asp:ControlParameter ControlID ="DropDownList2" Name ="校区" PropertyName ="SelectedValue"
Type ="Int32" />
</ SelectParameters >
</ asp:SqlDataSource >
< asp:SqlDataSource ID ="SqlDataSource2" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [校区代码], [校区简称] FROM [C_校区代码表]" ></ asp:SqlDataSource >
但是把这个代码转移到FormView中就会出错,一般页面第一次载入时时不会出错的,我也不知道为什么,但是如果将校区换了以后就会出现下面的错误
Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用。
源错误:
|
而且我还不知道是哪行错了,上网查了查,也有好多人问,但是没有人能给人正确或者说比较好的答案,我找了半天也没有找到解决办法,看来只能自己试一试了,差不多用了半天时间吧我已经能够实现想要实现的效果了,下面是我的代码,还请大家指点,哪如果有不足的地方请多指点
在ASPX文件中进行添加一个GridView然后对插入模板进行编辑实现校区下拉菜单和楼名的连动效果,这里需要注意以下几点:
1要实现连动要把校区下拉框的AutoPostBack属性设置为true;
2校区下拉框所对应的数据源孔件必须放在FormView里,否则回报错,找不到数据源
3对应校区的楼的下拉框的数据源空间要放到FormView的外面,实际上数据源和楼名下拉框基本上没有什么关系了,只是通过数据源获取数据然后把数据传递给DropDownList
<
asp:FormView
ID
="fv_ShiYanShi"
runat
="server"
DefaultMode
="Insert"
Width
="437px"
>
< InsertItemTemplate >
< asp:DropDownList ID ="dpl_xiaoqu" runat ="server" AutoPostBack ="True" DataSourceID ="sqd_xiaoqu"
DataTextField ="校区简称" DataValueField ="校区代码" SelectedValue ='<%# Bind("校区") % > '
Width="200px">
</ asp:DropDownList >< asp:SqlDataSource ID ="sqd_xiaoqu" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [校区代码], [校区简称] FROM [C_校区代码表]" ></ asp:SqlDataSource >
< asp:DropDownList ID ="dpl_lou" runat ="server" SelectedValue ='<%# Bind("所在楼名") % > ' Width="200px">
</ asp:DropDownList >
</ InsertItemTemplate >
</ asp:FormView >
< asp:SqlDataSource ID ="sqd_lou" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [楼代码], [楼名称], [校区] FROM [C_楼名代码表] WHERE ([校区] = @校区)" DataSourceMode ="DataSet" >
< SelectParameters >
< asp:Parameter Name ="校区" Type ="Int32" />
</ SelectParameters >
</ asp:SqlDataSource >
< asp:SqlDataSource ID ="SqlDataSource1" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [楼代码], [楼名称] FROM [C_楼名代码表] WHERE ([校区] = @校区)" DataSourceMode ="DataSet" >
< SelectParameters >
< asp:Parameter Name ="校区" Type ="Int32" />
</ SelectParameters >
</ asp:SqlDataSource >
< asp:DropDownList ID ="DropDownList1" runat ="server" >
</ asp:DropDownList >
< InsertItemTemplate >
< asp:DropDownList ID ="dpl_xiaoqu" runat ="server" AutoPostBack ="True" DataSourceID ="sqd_xiaoqu"
DataTextField ="校区简称" DataValueField ="校区代码" SelectedValue ='<%# Bind("校区") % > '
Width="200px">
</ asp:DropDownList >< asp:SqlDataSource ID ="sqd_xiaoqu" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [校区代码], [校区简称] FROM [C_校区代码表]" ></ asp:SqlDataSource >
< asp:DropDownList ID ="dpl_lou" runat ="server" SelectedValue ='<%# Bind("所在楼名") % > ' Width="200px">
</ asp:DropDownList >
</ InsertItemTemplate >
</ asp:FormView >
< asp:SqlDataSource ID ="sqd_lou" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [楼代码], [楼名称], [校区] FROM [C_楼名代码表] WHERE ([校区] = @校区)" DataSourceMode ="DataSet" >
< SelectParameters >
< asp:Parameter Name ="校区" Type ="Int32" />
</ SelectParameters >
</ asp:SqlDataSource >
< asp:SqlDataSource ID ="SqlDataSource1" runat ="server" ConnectionString ="<%$ ConnectionStrings:SBGLConnectionString %>"
SelectCommand ="SELECT [楼代码], [楼名称] FROM [C_楼名代码表] WHERE ([校区] = @校区)" DataSourceMode ="DataSet" >
< SelectParameters >
< asp:Parameter Name ="校区" Type ="Int32" />
</ SelectParameters >
</ asp:SqlDataSource >
< asp:DropDownList ID ="DropDownList1" runat ="server" >
</ asp:DropDownList >
在对应的CS文件中这样进行处理
private
DropDownList dpl_xiaoqu;
private DropDownList dpl_lou;
protected void Page_Load( object sender, EventArgs e)
{
dpl_xiaoqu = (DropDownList)this.fv_ShiYanShi.FindControl("dpl_xiaoqu");
dpl_lou = (DropDownList)this.fv_ShiYanShi.FindControl("dpl_lou");
dpl_xiaoqu_SelectedIndexChanged();
}
protected void dpl_xiaoqu_SelectedIndexChanged()
{
dpl_lou.Items.Clear();
this.sqd_lou.SelectParameters["校区"].DefaultValue = this.dpl_xiaoqu.SelectedValue;
DataView datav = (DataView)this.sqd_lou.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView dr in datav)
{
ListItem li = new ListItem();
li.Value = dr.Row["楼代码"].ToString();
li.Text = dr.Row["楼名称"].ToString();
dpl_lou.Items.Add(li);
}
}
private DropDownList dpl_lou;
protected void Page_Load( object sender, EventArgs e)
{
dpl_xiaoqu = (DropDownList)this.fv_ShiYanShi.FindControl("dpl_xiaoqu");
dpl_lou = (DropDownList)this.fv_ShiYanShi.FindControl("dpl_lou");
dpl_xiaoqu_SelectedIndexChanged();
}
protected void dpl_xiaoqu_SelectedIndexChanged()
{
dpl_lou.Items.Clear();
this.sqd_lou.SelectParameters["校区"].DefaultValue = this.dpl_xiaoqu.SelectedValue;
DataView datav = (DataView)this.sqd_lou.Select(DataSourceSelectArguments.Empty);
foreach (DataRowView dr in datav)
{
ListItem li = new ListItem();
li.Value = dr.Row["楼代码"].ToString();
li.Text = dr.Row["楼名称"].ToString();
dpl_lou.Items.Add(li);
}
}