我有一个下拉列表控件,该控件显示名为模型的表中的主键,以及一个文本框,当我使用下拉列表时,该控件应显示同一表中的另一个值(外键)。PK 和 FK 都有一个值。
由于我不知道如何执行此操作,因此我使用了一种搜索方法,每次有人从下拉列表中选择一个新值时都应该调用该方法。
搜索代码:
Public Function searchArea(ByVal model As String) As String
Dim mycon As New Connection
Using connection As New SqlConnection(mycon.GetConnectionString())
Using command As New SqlCommand()
command.Connection = connection
command.CommandType = CommandType.Text
command.CommandText = "SELECT area FROM model WHERE model= @model"
command.Parameters.AddWithValue("@model", model)
connection.Open()
Dim dataReader As SqlDataReader = command.ExecuteReader()
If (dataReader.Read()) Then
Return dataReader.ToString(0)'this query should always have a single value
End If
Return "Nothing"
End Using
End Using
End Function
事件代码:
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged
Dim objModel As New ModelDAO 'the class where the method is
TextBox9.Text = objModel.searchArea(DropDownList1.Text)
End Sub
.
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource1" DataTextField="MODEL" DataValueField="MODEL">
<asp:ListItem Text="-Select-" Value="" />
</asp:DropDownList>
<asp:TextBox ID="TextBox9" runat="server" Enabled="False" Height="24px"
Width="219px"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBUserInterfaceConnectionString %>"
SelectCommand="SELECT [MODEL] FROM [MODEL]">
</asp:SqlDataSource>
但正如我所想,它不起作用,你能帮帮我吗?
编辑:谢谢,现在它触发了,但我得到了值:'S'那个值不属于我的表。你能告诉我我的搜索方法是否可以吗?