【问题标题】:ASP.Net (VB) - Populate drop down list using data from previous drop down list. [Member Not Found]ASP.Net (VB) - 使用先前下拉列表中的数据填充下拉列表。 [未找到成员]
【发布时间】:2021-01-01 20:07:11
【问题描述】:

目前正在制作一个带有下拉列表的表单,该表单根据之前的下拉列表更改数据。我使用this example 作为参考,这是我目前所做的:

设计:

        <asp:Table ID="Table1" runat="server" Width="100%">

        <asp:TableRow runat="server">

            <asp:TableCell runat="server" Width="10%">
                <asp:Label ID="Label1" runat="server" Text="Line" Font-Size="x-Large"></asp:Label>
         
            </asp:TableCell>
                
            <asp:TableCell ID="TableCell11" runat="server" Width="3%">
                <asp:Label ID="label_txt" runat="server" Text=" :" Font-Size="x-Large"></asp:Label>
              
            </asp:TableCell>

            <asp:TableCell runat="server" Width = "40%">
                <asp:dropdownlist ID="line" runat="server"  BorderStyle="Solid" Font-Size="x-Large" BorderWidth="1px" Width = 80% ReadOnly="True"
                autopostback=true onselectedindexchanged= "line_selectedindexchanged"></asp:dropdownlist>

            </asp:TableCell>

            </asp:TableRow>

            <asp:TableRow runat="server">
            
                <asp:TableCell ID="TableCell1" runat="server" Width  = 10%>
                <asp:Label ID="Label2" runat="server" Text="Process" Font-Size="x-Large"></asp:Label>

            </asp:TableCell>

            <asp:TableCell ID="TableCell12" runat="server" Width = 3%>
                <asp:Label ID="Label8" runat="server" Text=" :" Font-Size="x-Large"></asp:Label>
             
            </asp:TableCell>

            <asp:TableCell ID="TableCell2" runat="server" Width = 40%>
                <asp:dropdownlist ID="process" runat="server"  BorderStyle="Solid" Font-Size="x-Large" BorderWidth="1px" Width = 80% ReadOnly="True" 
                autopostback=true onselectedindexchanged= "process_selectedindexchanged"></asp:dropdownlist>

            </asp:TableCell>
            
        </asp:TableRow>

        <asp:TableRow runat="server">              

            <asp:TableCell ID="TableCell5" runat="server" Width=10%>
                <asp:Label ID="Label4" runat="server" Text="Equipment" Font-Size="x-Large"></asp:Label>

            </asp:TableCell>

            <asp:TableCell ID="TableCell14" runat="server" Width=3%>
                <asp:Label ID="Label10" runat="server" Text=" :" Font-Size="x-Large"></asp:Label> 
             
            </asp:TableCell>

            <asp:TableCell ID="TableCell6" runat="server" Width=40%>
                <asp:dropdownlist ID="equipment" runat="server"  BorderStyle="Solid" Font-Size="x-Large" BorderWidth="1px" Width=80% ReadOnly="True"
                 autopostback=true onselectedindexchanged= "equipment_selectedindexchanged"></asp:dropdownlist>

            </asp:TableCell>

            </asp:tablerow>

            <asp:TableRow ID="TableRow3" runat="server">

            <asp:TableCell ID="TableCell3" runat="server" Width =  10%>
                <asp:Label ID="Label3" runat="server" Text="Step" Font-Size="x-Large"></asp:Label>

            </asp:TableCell>

            <asp:TableCell ID="TableCell13" runat="server" Width = 3%>
                <asp:Label ID="Label9" runat="server" Text=" :" Font-Size="x-Large"></asp:Label>  
           
            </asp:TableCell>

            <asp:TableCell ID="TableCell4" runat="server" Width = 40%>
                
                <asp:DropDownList ID="step" runat="server" BorderStyle="Solid" Font-Size="x-Large" BorderWidth="1px" Width = 80%
                autopostback=true >
                </asp:DropDownList> 
     
            </asp:TableCell>

        </asp:TableRow>  

    </asp:Table>

代码:

Imports System.Data.SqlClient

Public Class WebForm1
Inherits System.Web.UI.Page

Dim conn As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("SQLIOTConnectionString").ConnectionString)

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

    If Session("EmpID") Is Nothing Then

        Response.Redirect("~/Login.aspx")

    Else

        If Not IsPostBack Then 'whenever hv dropdownlist, need to have this in load sub, or else won't get ddl data.

            CaseDate.Text = DateTime.Now.ToString("yyyy-MM-dd")

            CreateBy.Text = Session("EmpID").ToString

            ReportPIC.Text = Session("EmpID").ToString

            FillLine()

        End If

    End If

End Sub

Protected Sub line_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim level1_ID As Integer = Convert.ToInt32(line.SelectedValue.ToString())
    FillProcess(level1_ID)

End Sub

Protected Sub process_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim level2_ID As Integer = Convert.ToInt32(process.SelectedValue.ToString())
    FillEquipment(level2_ID)

End Sub

Protected Sub equipment_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim level3_ID As Integer = Convert.ToInt32(equipment.SelectedValue.ToString())
    FillStep(level3_ID)

End Sub

Private Sub FillLine()

    Dim cmd As New SqlCommand
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text

    cmd.CommandText = "select distinct level1_id, [LCODE1]+ ' | '+[LNAME1] as [LCODE1]" _
                      & " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP]"

    Dim objDs As New DataSet()
    Dim dAdapter As New SqlDataAdapter()

    dAdapter.SelectCommand = cmd
    conn.Open()
    dAdapter.Fill(objDs)
    conn.Close()

    If objDs.Tables(0).Rows.Count > 0 Then

        line.DataSource = objDs.Tables(0)
        line.DataTextField = "LCODE1"
        line.DataValueField = "level1_id"
        line.DataBind()
        line.Items.Insert(0, "")

    End If


End Sub

Private Sub FillProcess(ByVal level1_ID As Integer)

    Dim cmd As New SqlCommand
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text

    cmd.CommandText = "select distinct level2_id, [LCODE2]+ ' | '+[LNAME2] as [LCODE2]" _
                      & " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level1_id = @level1_id"

    cmd.Parameters.AddWithValue("@level1_id", level1_ID)

    Dim objDs As New DataSet()
    Dim dAdapter As New SqlDataAdapter()
    dAdapter.SelectCommand = cmd
    conn.Open()
    dAdapter.Fill(objDs)
    conn.Close()

    If objDs.Tables(0).Rows.Count > 0 Then

        process.DataSource = objDs.Tables(0)
        process.DataTextField = "LCODE2"
        process.DataValueField = "level2_id"
        process.DataBind()
        process.Items.Insert(0, "")

    End If

End Sub

Private Sub FillEquipment(ByVal level2_ID As Integer)

    Dim cmd As New SqlCommand
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text

    cmd.CommandText = "select distinct level3_id, [LCODE3]+ ' | '+[LNAME3] as [LCODE3]" _
                      & " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level2_id = @level2_id"

    cmd.Parameters.AddWithValue("@level2_id", level2_ID)

    Dim objDs As New DataSet()
    Dim dAdapter As New SqlDataAdapter()
    dAdapter.SelectCommand = cmd
    conn.Open()
    dAdapter.Fill(objDs)
    conn.Close()

    If objDs.Tables(0).Rows.Count > 0 Then

        process.DataSource = objDs.Tables(0)
        process.DataTextField = "LCODE3"
        process.DataValueField = "level3_id"
        process.DataBind()
        process.Items.Insert(0, "")

    End If

End Sub

Private Sub FillStep(ByVal level3_ID As Integer)

    Dim cmd As New SqlCommand
    cmd.Connection = conn
    cmd.CommandType = CommandType.Text

    cmd.CommandText = "select distinct [LCODE4]+ ' | '+[LNAME4] as [LCODE4]" _
                      & " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level3_id = @level3_id"

    cmd.Parameters.AddWithValue("@level3_id", level3_ID)

    Dim objDs As New DataSet()
    Dim dAdapter As New SqlDataAdapter()
    dAdapter.SelectCommand = cmd
    conn.Open()
    dAdapter.Fill(objDs)
    conn.Close()

    If objDs.Tables(0).Rows.Count > 0 Then

        process.DataSource = objDs.Tables(0)
        process.DataTextField = "LCODE4"
        'process.DataValueField = "level3_id"
        process.DataBind()
        process.Items.Insert(0, "")

    End If

End Sub

根据我在示例中的理解,一旦我从第一个下拉列表中选择了一个值,它之前的下一个将具有值,依此类推。但是在测试过程中,我从第一个下拉列表中选择值后,出现错误

JavaScript runtime error: Member not found.

我不知道 JavaScript 是如何工作的,我什至没有接触过它……谁能帮忙解决这个问题?

编辑:

使用玛丽的建议:

Protected Sub line_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim level1_ID As Integer = Convert.ToInt32(line.SelectedValue.ToString())
    FillProcess(level1_ID)

End Sub

Protected Sub ddlprocess_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim level2_ID As Integer = Convert.ToInt32(ddlprocess.SelectedValue.ToString())
    FillEquipment(level2_ID)

End Sub

Protected Sub equipment_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)

    Dim level3_ID As Integer = Convert.ToInt32(equipment.SelectedValue.ToString())
    FillStep(level3_ID)

End Sub

Private Sub FillLine()

    Dim dt As New DataTable
    Dim strSql = "select distinct level1_id, [LCODE1]+ ' | '+[LNAME1] as [LCODE1]" _
                      & " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP]"
    Using conn As New SqlConnection(ConStr),
        cmd As New SqlCommand(strSql, conn)            
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    If dt.Rows.Count > 0 Then
        line.DataSource = dt
        line.DataTextField = "LCODE1"
        line.DataValueField = "level1_id"
        line.DataBind()
        line.Items.Insert(0, "")
    End If

End Sub

Private Sub FillProcess(ByVal level1_ID As Integer)

    Dim dt As New DataTable
    Dim strSql = "select distinct level2_id, [LCODE2]+ ' | '+[LNAME2] as [LCODE2]" _
                 & "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level1_id = @level1_id"
    Using conn As New SqlConnection(ConStr),
        cmd As New SqlCommand(strSql, conn)
        cmd.Parameters.Add("@level1_id", SqlDbType.Int).Value = level1_ID
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    If dt.Rows.Count > 0 Then
        ddlprocess.DataSource = dt
        ddlprocess.DataTextField = "LCODE2"
        ddlprocess.DataValueField = "level2_id"
        ddlprocess.DataBind()
        ddlprocess.Items.Insert(0, "")
    End If

End Sub

Private Sub FillEquipment(ByVal level2_ID As Integer)

    Dim dt As New DataTable
    Dim strSql = "select distinct level3_id, [LCODE3]+ ' | '+[LNAME3] as [LCODE3]" _
                 & "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level2_id = @level2_id"
    Using conn As New SqlConnection(ConStr),
        cmd As New SqlCommand(strSql, conn)
        cmd.Parameters.Add("@level2_id", SqlDbType.Int).Value = level2_ID
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    If dt.Rows.Count > 0 Then
        equipment.DataSource = dt
        equipment.DataTextField = "LCODE3"
        equipment.DataValueField = "level3_id"
        equipment.DataBind()
        equipment.Items.Insert(0, "")
    End If

End Sub

Private Sub FillStep(ByVal level3_ID As Integer)

    Dim dt As New DataTable
    Dim strSql = "select distinct level4_id, [LCODE4]+ ' | '+[LNAME4] as [LCODE4]" _
                 & "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level3_id = @level3_id"
    Using conn As New SqlConnection(ConStr),
        cmd As New SqlCommand(strSql, conn)
        cmd.Parameters.Add("@level3_id", SqlDbType.Int).Value = level3_ID
        conn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    If dt.Rows.Count > 0 Then
        [step].DataSource = dt
        [step].DataTextField = "LCODE4"
        [step].DataValueField = "level3_id"
        [step].DataBind()
        [step].Items.Insert(0, "")
    End If

End Sub

但同样的问题仍然存在......

【问题讨论】:

    标签: javascript asp.net vb.net visual-web-developer-2010


    【解决方案1】:

    我能看到的唯一会导致错误的是名称过程。这可能会导致与 System.Diagnostics 中的 Process 类发生冲突。我把名字改成了ddlProcess

    我加载了一个数据表,而不是使用 DataSetDataAdapter

    连接需要在使用它们的方法中是本地的,以便可以关闭和释放它们。他们使用在.Dispose 方法中释放的非托管资源。即使有错误,Using...End Using 块也会为您处理此问题。我将命令包含在 using 块中。

    在 Sql Server 中,.Add 方法是首选。见http://www.dbdelta.com/addwithvalue-is-evil/https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 还有一个: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications 这是另一个 https://andrevdm.blogspot.com/2010/12/parameterised-queriesdont-use.html

    让我有点惊讶的是,下拉列表允许您在项目已经被数据绑定时插入它。如果没有必要,可以删除该行。

    Private ConStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("SQLIOTConnectionString").ConnectionString
    
    Private Sub FillProcess(ByVal level1_ID As Integer)
        Dim dt As New DataTable
        Dim strSql = "select distinct level2_id, [LCODE2]+ ' | '+[LNAME2] as [LCODE2]
                       FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level1_id = @level1_id"
        Using conn As New SqlConnection(ConStr),
            cmd As New SqlCommand(strSql, conn)
            cmd.Parameters.Add("@level1_id", SqlDbType.Int).Value = level1_ID
            conn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
        If dt.Rows.Count > 0 Then
            ddlProcess.DataSource = dt
            ddlProcess.DataTextField = "LCODE2"
            ddlProcess.DataValueField = "level2_id"
            ddlProcess.DataBind()
            ddlProcess.Items.Insert(0, "")
        End If
    End Sub
    

    【讨论】:

    • 我试图效仿你的榜样。将process 更改为ddlproces,对所有下拉列表使用datatableusing,甚至删除了虚拟项目的代码。但还是一样的结果。我将在我的问题中编辑当前代码。
    • [LCODE2]" _ &amp; "FROM 之间似乎没有空格在 Visual Studio 的最新版本中,如果没有 "_ &amp;",字符串可以从一行继续到另一行可能问题出在 asp 布局代码中.我没有看到错误,但我对那部分不是很熟悉。
    • 那么最新的发展。我试图在一个新项目中重复代码。不是我第一种形式的其他组件。只是表格和代码本身。它工作正常。有一些小问题,但还不是问题。猜猜其他东西弄乱了这段代码。一旦我弄清楚问题所在,我会将其标记为正确的第一个问题并发布另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多