[英]ASP.Net (VB) - Populate drop down list using data from previous drop down list. [Member Not Found]
目前正在制作一個帶有下拉列表的表單,該表單根據之前的下拉列表更改數據。 我使用這個例子作為參考,這是我到目前為止所做的:
設計:
<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
但同樣的問題仍然存在......
我能看到的唯一會導致錯誤的是名稱過程。 這可能會導致與 System.Diagnostics 中的 Process 類發生沖突。 我將名稱更改為ddlProcess
。
我加載了一個數據表,而不是用DataSet
或DataAdapter
愚弄。
連接需要位於使用它們的方法的本地,以便可以關閉和處理它們。 他們使用在他們的.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-使用.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.