.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 />
<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 />
<br />
</div>
</form>
</body>
</html>
为什么Label中只显示第一条记录呢?
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 />
<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 />
<br />
</div>
</form>
</body>
</html>
为什么Label中只显示第一条记录呢?
解决方案 »
- 100分跪求解答--关于WebService重定向问题
- System.NullReferenceException: 未将对象引用设置到对象的实例。
- 怎么使用服务器控件开发
- PagedDataSource 返回值
- 请教 .csproj 项目文件是否应该排除出VSS之外?
- DataGrid分页栏显示在一行下面,不能跨行显示郁闷了.
- .NET程序在外网有何办法得到内网IP
- 每个配置文件中只允许存在一个 <configSections> 元素,并且,如果存在该元素,它还必须是根 <configuration> 元素的第一个子元素。
- new 一个 TextBox之后,出错了?
- 如何改变GRID中的超级连接列的颜色???
- asp.net 读取Excel 格式问题
- 高手请介绍.net三层结构的感念
你给OnSelectIndexChange里写Response.Wirte(DropDownList1.SelectedValue.ToString());
看看,有种预感,你要看label显示东东,需要用到ajax
好像还是只显示 1
也就是第一个刚栽入的那个选项
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Label1.Text = "你选择的学生是:学号" + this.DropDownList1.SelectedValue.ToString() + "姓名" + this.DropDownList1.SelectedItem.ToString();
}
这个应该只触发了一次,你SelectedItem就没有变当然就只显示一个了,里面做个循环吧
你看看如果给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();
}
}
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();
}怎么输出: 你选择的学生是:学号#姓名张一
成功了 谢谢了哈