1

我是 ASP.net 和 VB.net 的新手。所以我从 VB 2008 中的 ASP.Net 3.5 开始学习这本书

select.aspx.vb 上的代码是

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration


Partial Public Class _Select
    Inherits System.Web.UI.Page

    Private Conn As String = WebConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString

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

        If Not Page.IsPostBack Then
            FillAuthorList()
        End If
    End Sub

    Private Sub FillAuthorList()
        lstAuthor.Items.Clear()
        ' Define the Select statement.
        ' Three pieces of information are needed: the unique id
        ' and the first and last name.
        Dim selectSQL As String = "SELECT Nama_Depan, Nama_Belakang, ID FROM Employee"
        ' Define the ADO.NET objects.
        Dim con As New SqlConnection(Conn)
        Dim cmd As New SqlCommand(selectSQL, con)
        Dim reader As SqlDataReader
        ' Try to open database and read information.
        Try
            con.Open()
            reader = cmd.ExecuteReader()
            ' For each item, add the author name to the displayed
            ' list box text, and store the unique ID in the Value property.
            Do While reader.Read()
                Dim newItem As New ListItem()
                newItem.Text = reader("Nama_Depan") & ", " & reader("Nama_Belakang")
                newItem.Value = reader("ID").ToString()
                lstAuthor.Items.Add(newItem)
            Loop
            reader.Close()
        Catch err As Exception
            lblResults.Text = "Error reading list of names."
            lblResults.Text &= err.Message
        Finally
            con.Close()
        End Try
    End Sub

    Protected Sub lstAuthor_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lstAuthor.SelectedIndexChanged
        ' Create a Select statement that searches for a record
        ' matching the specific author ID from the Value property.
        Dim selectSQL As String
        selectSQL = "SELECT * FROM Employee "
        selectSQL &= "WHERE ID='" & lstAuthor.SelectedItem.Value & "' "
        ' Define the ADO.NET objects.
        Dim con As New SqlConnection(Conn)
        Dim cmd As New SqlCommand(selectSQL, con)
        Dim reader As SqlDataReader
        ' Try to open database and read information.
        Try
            con.Open()
            reader = cmd.ExecuteReader()
            reader.Read()

            ' Build a string with the record information,
            ' and display that in a label.
            Dim sb As New StringBuilder()
            sb.Append("<b>")
            sb.Append(reader("Nama_Depan"))
            sb.Append(", ")
            sb.Append(reader("Nama_Belakang"))
            sb.Append("</b><br />")

            lblResults.Text = sb.ToString()
            reader.Close()
        Catch err As Exception
            lblResults.Text = "Error getting author. "
            lblResults.Text &= err.Message
        Finally
            con.Close()
        End Try
    End Sub
End Class

而 select.aspx 是

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Select.aspx.vb" Inherits="connn._Select" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ListBox ID="lstAuthor" runat="server" ></asp:ListBox>
        <br />

    <asp:Label ID="lblResults" runat="server">


    </asp:Label>
    </div>
    </form>
</body>
</html>

我正在尝试选择 ID="lstAuthor" 的列表框时,标签框将传递该值但未能显示。

4

1 回答 1

0

设置TrueAutoPostBack属性:

<asp:ListBox ID="lstAuthor" 
             runat="server"  
             AutoPostBack="True" ></asp:ListBox>
于 2012-06-21T06:35:32.533 回答