ASP.NET DropDownList + SQL Query没有显示任何内容

时间:2012-05-26 20:14:36

标签: asp.net sql database sqldatasource

我有5个下拉列表,我试图这样做,如果我在某个下拉列表中选择某个值,它就不会在下面的下拉列表中显示该特定项目。

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

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

有人能帮我一把吗?

谢谢!

来自PASTE BIN的密码

<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!

1 个答案:

答案 0 :(得分:0)

对于您的情况,您可以使用或不使用Ajax,但基本上可以创建所有列表     的AutoPostBack = “真” 和enabled =“false”除了第一个,然后做类似以下的事情: 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