<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Web.UI" %>
<%@ import Namespace="System.Web.UI.WebControls" %>
<script runat="server">    // Insert page code here
    
       void Page_Load(Object sender, EventArgs e){
            string connectionString = "server=\'(local)\'; trusted_connection=true; database=\'Northwind\'";
            SqlConnection dbConnection = new SqlConnection(connectionString);
    
            string queryString = "SELECT firstname,lastname FROM [Employees]";
            SqlCommand dbCommand = new SqlCommand();
            dbCommand.CommandText = queryString;
            dbCommand.Connection = dbConnection;
    
            SqlDataAdapter dataAdapter = new SqlDataAdapter(queryString,dbConnection);
    
    
            DataSet dataSet = new DataSet();
    
            dataAdapter.Fill(dataSet,"Employees");
    
            ListBox1.DataSource=dataSet;
            ListBox1.DataTextField="lastname";
    
            ListBox1.DataValueField="firstname";
    
            ListBox1.DataBind();
    
            ListBox1.Visible=true;
    
    DataGrid1.DataSource=dataSet;
    DataGrid1.DataBind();
    
    }
    
        //
    
    void Button1_Click(object sender, EventArgs e) {
    
    Label1.Text="";
    
    ----------------
想在label1里显示选择的数据,这里应该写什么?
--------------------
    
    
    }</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:ListBox id="ListBox1" runat="server" Width="200px"></asp:ListBox>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>        <!-- Insert content here -->
    </form>
</body>
</html>

解决方案 »

  1.   

    Label1.Text=ListBox1.SelectedItem.Text;
      

  2.   

    我测试下来不对
    ====================
    “/”应用程序中的服务器错误。
    --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
    行 43:     Label1.Text="";
    行 44: 
    行 45: Label1.Text=ListBox1.SelectedItem.Text; 
      

  3.   

    你的DataGrid1是哪来的啊?我在下面怎么没看到申明啊
      

  4.   

    TO: matuos(雪松)
    那个不要管他,测试时候是没有的
      

  5.   

    是不是listbox没有选择。。设置个断点试试!