ASP.Net(VB)-使用以前的下拉列表中的数据填充下拉列表.[找不到成员]



当前正在制作一个带有下拉列表的表单,该表单根据之前的下拉列表更改数据。我将使用此示例作为参考,这是我迄今为止所做的:

设计:

<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

我加载了一个数据表,而不是使用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

最新更新