注册 登录
编程论坛 ASP.NET技术论坛

[求助]选择显示下拉列表中的值却总是显示不了

mq0532 发布于 2007-05-16 19:42, 608 次点击

我在VS。2005里写的程序
页面有个下拉列表框 里面的值是通过数据库提取的
还有个提交按钮 我就像通过点击提交按钮 显示下拉列表中选择的值

但我提交的时候 总是显示的是第一个显示的值 想提取显示下边别的值 显示不了
捆饶很久了
请高手指点啊

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropdown1.aspx.cs" Inherits="dropdown1" %>

<!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:DropDownList ID="xl" runat="server" AutoPostBack="True"></asp:DropDownList>
<asp:ListBox ID="xs" runat="server"></asp:ListBox>
<asp:Button ID="tj" runat="server" Text="添加" OnClick="tj_Click" />
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.OleDb;

public partial class dropdown1 : System.Web.UI.Page
{
protected readonly string AccessString = ConfigurationSettings.AppSettings["AccessConnectionString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
// if (!IsPostBack)
// {
GetSourceList();
// }
}
protected void GetSourceList()
{
string AccessConnectionString = AccessString + Server.MapPath("csc.mdb");

string cmdtext = "select * from srtype order by stid";
OleDbConnection myConnection = new OleDbConnection(AccessConnectionString);
OleDbCommand myCommand = new OleDbCommand(cmdtext,myConnection);

myConnection.Open();
OleDbDataReader reader1 = myCommand.ExecuteReader();
//xl.Items.Clear();
while (reader1.Read())
{

xl.Items.Add(new ListItem(reader1["stname"].ToString(), reader1["stid"].ToString()));

}

xl.SelectedIndex = 0;
reader1.Close();
myConnection.Close();
}

protected void tj_Click(object sender, EventArgs e)
{
if (xl.SelectedIndex > -1)
{

Response.Write(xl.SelectedItem.Value.ToString());
}
xl.SelectedIndex = 0;

}
}

6 回复
#2
ahuinan2007-05-16 20:53

你要实现点提交后输出值,那


AutoPostBack=\"True\"

这句有点多余!

#3
rainic2007-05-16 21:36
为什么不用databind?

不过代码好像没问题

你看看程序执行到while里面了吗?
#4
tel19822007-05-16 22:24

你按照3楼的版主的说法查一下先。

#5
mq05322007-05-17 09:45
执行到了啊
页面显示的时候下拉列表里就有提取数据库里的值了

你们实验一下吧 我很困惑了
#6
Kendy1234562007-05-17 09:48
Autopostback = True 那么在你改变下拉列表的值的时候 就会刷新页面了
刷新页面会执行pageload 你检查下你的pageload事件里面是不是有重新从数据库提取数据的代码
#7
mq05322007-05-17 21:38

我的代码已经在上面有了你们看看把

1