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>
|
<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>
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);
}
}
如何解决FormView中实现DropDownList连动选择时出现 "Eval()、XPath() 和 Bind() 这类数据绑定方法只能在数据绑定控件的上下文中使用" 的错误
原文:http://www.cnblogs.com/lonelyxmas/p/3715988.html