2012-06-21 65 views
1

我是新的ASP.net和VB.net.So我從開始ASP.Net 3.5書學習VB 2008傳遞值不顯示?

上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 

和選擇.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」的列表框時,標籤框會傳遞值,但它未能顯示。

+1

設置lstAuthor.AutoPostBack =真 – adatapost

+0

AVD請回答這個Q&A –

+0

高興你得到它的工作。沒關係!請選擇CoDe aDDIct的答案。 – adatapost

回答

0

設置TrueAutoPostBack屬性:

<asp:ListBox ID="lstAuthor" 
      runat="server" 
      AutoPostBack="True" ></asp:ListBox> 
+0

謝謝你回答這個問題,但ADV已經給出了答案。 –

相關問題