0

我有 5 个下拉列表,并且正在尝试制作它,所以如果我在某个下拉列表中选择某个值,它将不会在以下下拉列表中显示该特定项目。

我通过对每个下拉列表的数据源的特定 SQL 查询来执行此操作,代码位于以下 pastebin 链接中,因为 StackOverflow 文本框由于某种原因对我来说是错误的:http: //pastebin.com/gDm8MPrL

我不确定为什么,但它没有显示数据库中的任何内容。当我测试正常的“SELECT *”查询时,它可以工作,所以它不是连接问题。

谁能帮我一把?

谢谢!

粘贴箱中的粘贴代码

<asp:DropDownList ID="ddlHero1" runat="server" DataSourceID="ddlDataSource0"                        DataTextField="ChampName" DataValueField="ChamadpName" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="ddlDataSource0" runat="server" ConnectionString="<%$ ConnectionStrings:mobamanagerConnectionString%>" SelectCommand="SELECT [ChampName] FROM [Champions] WHERE Champname NOT IN (
SELECT [ChampName] FROM [Champions] WHERE Champname=@ddlHero2 
UNION 
SELECT [ChampName] FROM [Champions] WHERE Champname=@ddlHero3
UNION
SELECT [ChampName] FROM [Champions] WHERE Champname=@ddlHero4
UNION
SELECT [ChampName] FROM [Champions] WHERE Champname=@ddlHero5)">
   <SelectParameters>
        <asp:ControlParameter ControlID="ddlHero2" Name="ddlHero2" 
                                                PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="ddlHero3" Name="ddlHero3" 
                                                PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="ddlHero4" Name="ddlHero4" 
                                                PropertyName="SelectedValue" />
        <asp:ControlParameter ControlID="ddlHero5" Name="ddlHero5" 
                                                PropertyName="SelectedValue" />
   </SelectParameters>
</asp:SqlDataSource>

这个过程再重复 4 次,对每个 DDL 进行不同的查询,但过程是相同的。

编辑:感谢您将代码粘贴到 mod 中!

4

1 回答 1

0

对于您的情况,您可以使用或不使用 Ajax,但基本上将所有列表 AutoPostback="true" 和 enabled="false" 除了第一个列表,然后执行以下操作:this sample code in VB

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        'here load the first only
        'make the 2d enabled
    Else
        If ViewState("ddlHero1").ToString <> "" Then
            'get the value of the 1st from the viewstate
            'ddlHero1.selectedindex=cint(ViewState("ddlHero1").ToString)
            'make the 2nd enabled
            'bind the second
        End If
        If ViewState("ddlHero2").ToString <> "" Then
            'get the value from viewstate
            'ddlHero2.selectedindex=cint(ViewState("ddlHero2").ToString)
            'make the 3nd enabled
            'bind the 3rd
        End If
        If ViewState("ddlHero3").ToString <> "" Then
            'like the 2nd..etc
        End If
    End If
End Sub

在每个列表的 SelectedIndexChanged 事件上,将所选值保存在视图状态中

Protected Sub ddlHero1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlHero1.SelectedIndexChanged

    ViewState("ddlHero1") = ddlHero1.SelectedIndex.ToString
    'and the same for the other lists

End Sub
于 2012-05-26T21:18:45.970 回答