VB网站-DropDownList无法保留所选数据

时间:2018-10-04 10:03:45

标签: sql asp.net vb.net

我正在创建一个新的Web应用程序,该应用程序通过下拉菜单从SQL列获取数据,以用于SQL查询以在GridView中显示数据。

到目前为止,到目前为止,数据显示正常。但是我仍然无法使用下拉菜单保留我选择的数据。

每次我选择一个随机选择项后单击执行查询代码,它都会自动默认为列表中的第一个选择项。

这里是供参考的代码,也许有人知道出了什么问题。

Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing

Public Class WebForm1
Inherits System.Web.UI.Page
Public Div_Adapter As New OleDb.OleDbDataAdapter


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


    Call Inv_no()

End Sub

Public Sub Inv_no() 'Load Inv No data from SQL & place it in dropdown list

    Dim cmd As New SqlCommand("SELECT distinct [Inv No] from [SQLIOT].[dbo].[ZDIE_MAIN_HISTORY] order by [Inv No]", SQLCONN)
    cmd.Connection.Open()
    '-------------------
    Dim ddlValues As SqlDataReader
    ddlValues = cmd.ExecuteReader()

    Inv.DataSource = ddlValues
    Inv.DataValueField = "Inv No"
    Inv.DataTextField = "Inv No"
    Inv.DataBind()

    cmd.Connection.Close()

End Sub

Protected Sub exe_Click(ByVal sender As Object, ByVal e As EventArgs) Handles exe.Click

    Dim check1
    Dim cmd As New SqlCommand
    Dim dsGROUP As New DataSet()

    check1 = Inv.SelectedItem.ToString

    Dim strSelectCmd As String = "SELECT [Project No],[Inv No],[Description] ,[Entry Date], convert(varchar(10),[Date],121) " _
                                 & "as [Date], REPLACE(RIGHT('0' + LTRIM(RIGHT(CONVERT(VARCHAR, [Time], 131), 14)), 14), ':000', ' ') " _
                                 & "AS [Time],[Problem + Repair Details] ,[Status],[Remarks],[Location],[Measurement (OK/NG)]," _
                                 & "[Accumulative Stroke],[Preventive Stroke],[PIC],[Flag] FROM [SQLIOT].[dbo].[ZDIE_MAIN_HISTORY] where " _
                                 & "[Inv No] = '" & check1 & "' order by [Project No]"

    Dim da As New SqlDataAdapter(strSelectCmd, SQLCONN)

    cmd.Connection = SQLCONN

    SQLCONN.Open()
    da.Fill(dsGROUP, "Die_Records")

    Dim dvGroup As DataView = dsGROUP.Tables("Die_Records").DefaultView

    WebDieView.DataSource = dvGroup
    WebDieView.DataBind()

    SQLCONN.Close()

End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub
End Class

2 个答案:

答案 0 :(得分:3)

问题出在您的页面加载事件中。每次页面加载时,您都在运行

Call Inv_no()

因此,当您的click事件触发时,它还会加载页面并重置下拉菜单

在页面加载中尝试一下

If Not IsPostBack Then
    Call Inv_no()
End if

答案 1 :(得分:0)

Dim sqlConnection1 As New SqlConnection("Data Source= ***;Initial Catalog=**;Integrated Security=SSPI;")//your connection string,or you can fetch from web config
        Dim cmd As New SqlCommand
        Dim reader As SqlDataReader

        cmd.CommandText = "SELECT distinct [Inv No] from [SQLIOT].[dbo].[ZDIE_MAIN_HISTORY] order by [Inv No]" //check once query retriving values or not

        cmd.Connection = sqlConnection1

        sqlConnection1.Open()
        reader = cmd.ExecuteReader()

        If reader.Read() Then
            ddl_Country.DataSource = reader
            ddl_Country.DataTextField = "Inv No"
            ddl_Country.DataValueField = "Inv No"

            ddl_Country.DataBind()

        End If    
       sqlConnection1.Close()