.cs中代码:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //数据库连接字符      
        String connectionString = "Server=localhost;Database=gwd;uid=sa;pwd=xxx;";
        //要执行的Sql语句 
        String cmdText = "select * from stu";
        //建立连接 
        SqlConnection conn = new SqlConnection(connectionString);
        //生成命名实例 
        SqlCommand command = new SqlCommand(cmdText, conn);
        //打开数据库
        conn.Open();
        //生成数据集实例 
        DataSet dataSet = new DataSet();
        SqlDataAdapter sqlDA = new SqlDataAdapter();
        sqlDA.SelectCommand = command;
        //填充数据集 
        sqlDA.Fill(dataSet);
        //绑定DropDownList控件
        this.DropDownList1.DataSource = dataSet.Tables[0];
        this.DropDownList1.DataValueField = "number";
        this.DropDownList1.DataTextField = "name";
        this.DropDownList1.DataBind();
        //关闭数据库
        conn.Close();    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.Label1.Text = "你选择的学生是:学号" + this.DropDownList1.SelectedValue.ToString() + "姓名" + this.DropDownList1.SelectedItem.ToString();
    }
}
.aspx中代码:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>
        <br />
        <br />
        <br />
        &nbsp;<br />
        <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList><br />
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label" Width="422px"></asp:Label><br />
        <br />
        <br />
        <br />
        &nbsp;<br />
        </div>
    </form>
</body>
</html>
为什么Label中只显示第一条记录呢?

解决方案 »

  1.   

    this.DropDownList1.SelectedItem.ToString();   zhe ge shi xuan zhe xiang de ji he ba!
      

  2.   

    你的意思是label中显示的数据不变是吧?
    你给OnSelectIndexChange里写Response.Wirte(DropDownList1.SelectedValue.ToString());
    看看,有种预感,你要看label显示东东,需要用到ajax
      

  3.   

    (lolly) 
    好像还是只显示 1
    也就是第一个刚栽入的那个选项
      

  4.   


       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
       { 
            this.Label1.Text = "你选择的学生是:学号" +                this.DropDownList1.SelectedValue.ToString() + "姓名" + this.DropDownList1.SelectedItem.ToString(); 
       } 
    这个应该只触发了一次,你SelectedItem就没有变当然就只显示一个了,里面做个循环吧
      

  5.   


    你看看如果给Page_Load里添加
     if (!IsPostBack)
    {
      数据绑定写这里
    }
    这样能不能解决问题,我的可以估计是因为我直接用了sqldatasource  protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                
            //数据库连接字符      
            String connectionString = "Server=localhost;Database=gwd;uid=sa;pwd=xxx;"; 
            //要执行的Sql语句 
            String cmdText = "select * from stu"; 
            //建立连接 
            SqlConnection conn = new SqlConnection(connectionString); 
            //生成命名实例 
            SqlCommand command = new SqlCommand(cmdText, conn); 
            //打开数据库 
            conn.Open(); 
            //生成数据集实例 
            DataSet dataSet = new DataSet(); 
            SqlDataAdapter sqlDA = new SqlDataAdapter(); 
            sqlDA.SelectCommand = command; 
            //填充数据集 
            sqlDA.Fill(dataSet); 
            //绑定DropDownList控件 
            this.DropDownList1.DataSource = dataSet.Tables[0]; 
            this.DropDownList1.DataValueField = "number"; 
            this.DropDownList1.DataTextField = "name"; 
            this.DropDownList1.DataBind(); 
            //关闭数据库 
            conn.Close(); 
            }
            
        }
      

  6.   

    autopostback = true;是不是你一点就post了????
      

  7.   

    我加了个循环string r_flag = "#";
                for (int i = 0; i < DropDownList1.Items.Count; i++)
                {
                    if (DropDownList1.Items[i].Selected == true)
                    {
                        r_flag += DropDownList1.Items[i].Value + "#";
                    }                this.Label1.Text = "你选择的学生是:学号" + r_flag + "姓名" + this.DropDownList1.SelectedItem.ToString();
                    
                }怎么输出:   你选择的学生是:学号#姓名张一 
      

  8.   

    (lolly) 和帅好厉害~
    成功了  谢谢了哈