下拉列表没有响应第一个和最后一个项目 [英] Dropdownlist not responding to first and last item

查看:60
本文介绍了下拉列表没有响应第一个和最后一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net/vb.net和sql server express 2008中有一个应用程序。在表格上我有两个下拉列表,一个用于选择年份(从2006年到2013年),另一个是选择成绩(从1- 8)。其他控件是列表框和按钮。我希望在从下拉列表中选择年份和等级以及单击按钮时,结果将显示在列表框中。 select查询在sql server中运行良好。但是在应用程序中它除了2006年和2013年之外它正在做我想做的事情,但这些年的数据就在那里。我使用下拉选择值。为什么查询没有为下拉列表中的第一个和最后一个项目选择记录。



下面的代码



I have an application in asp.net/vb.net and sql server express 2008. On the form i have two dropdownlists one for selection of years(from 2006-2013), the other is selection of grades(from 1-8). The other controls are listbox and a button. I want the results to be displayed in listbox upon selecting year and grade from dropdownlists and on click of a button. The select query works great in sql server. But in the application it is doing what i want except for year 2006 and 2013 but data for these years is there. Iam using dropdown selected value. Why the query is not selecting records for the first and last item in a dropdownlist.

the code below

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim queryString As String = _
                "SELECT DISTINCT Code FROM enrolnment WHERE grade ='" & DropDownList2.Selectedvalue & "' AND Year ='" & DropDownList3.Selectedvalue & "'AND iActive = '1';"
        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(queryString, connection)
            connection.Open()
            Dim reader As SqlDataReader = command.ExecuteReader()
            Dim i As Integer = 0
            command.CommandTimeout = 300

            While reader.Read()
                'i < Listbox1.Items.Count
                ListBox1.Items.Add(reader.GetValue(i).ToString())
                command.CommandTimeout = 300
            End While
            reader.Close()

            'End Try
        End Using

    End Sub

推荐答案

对于任何仍有问题的人;我以一种不同但更简单的方式解决了它:只需将一个虚拟ListItem添加到DropDownList的开头,并将该项的Enabled属性设置为false。即





< asp:dropdownlist id =dropdown1runat =serverautopostback =trueonselectedindexchanged = dropdown1_SelectedIndexChangedviewstatemode =已启用xmlns:asp =#unknown>

< asp:listitem value =text =enabled =false/>

< asp:listitem value =1text =Item 1/>

< asp:listitem value =2text =Item 2/> ;

< asp:listitem value =3text =Item 3/>

< asp:listitem value =4text =Item 4/>

< asp:listitem value =5text =Item 5/>

For Anyone still having the problem; I solved it in a different, yet easier way: Just add a dummy ListItem to the start of the DropDownList and set that item's Enabled property to false. i.e


<asp:dropdownlist id="dropdown1" runat="server" autopostback="true" onselectedindexchanged="dropdown1_SelectedIndexChanged" viewstatemode="Enabled" xmlns:asp="#unknown">
<asp:listitem value="" text="" enabled="false" />
<asp:listitem value="1" text="Item 1" />
<asp:listitem value="2" text="Item 2" />
<asp:listitem value="3" text="Item 3" />
<asp:listitem value="4" text="Item 4" />
<asp:listitem value="5" text="Item 5" />


这篇关于下拉列表没有响应第一个和最后一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆