| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4314 人关注过本帖
标题:[求助]DropDownList连接数据库后选不了下拉列表里的值,提交后一直显示默认的 ...
取消只看楼主 加入收藏
mq0532
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-9-7
收藏
 问题点数:0 回复次数:3 
[求助]DropDownList连接数据库后选不了下拉列表里的值,提交后一直显示默认的第一个

<asp:DropDownList ID="sn" runat="server" AutoPostBack="false">
</asp:DropDownList>

OleDbDataAdapter dasn = new OleDbDataAdapter(cmdTextsn,AccessConn);
DataSet dssn = new DataSet();
dasn.Fill(dssn,"srname");
sn.Items.Clear();
sn.DataSource=dssn.Tables["srname"].DefaultView;
sn.DataTextField = "srname";
sn.DataValueField = "snid";
sn.DataBind();

string price1=Convert.ToString((Convert.ToDouble(dj.Text.ToString()))*(Convert.ToDouble(sd.Text.ToString())));
string cmdText = "INSERT INTO sr(name,date1,perprice,number1,price) VALUES('"+sn.SelectedItem.Text.ToString()

+"','"+xxxytime.Text+"','"+dj.Text.ToString()+"','"+sd.Text.ToString()+"','"+price1+"')";
OleDbCommand myCommand = new OleDbCommand(cmdText,AccessConnString);
AccessConnString.Open();
myCommand.ExecuteNonQuery();
AccessConnString.Close();

Response.Redirect("sr.aspx");


插入到数据库后 一直是显示的第一个值 也就是选择下面的值就插不到库里 是不是我得用个事件处理啊
请各位大哥帮我完善下 小弟不声感激!!!

搜索更多相关主题的帖子: 数据库 DropDownList 列表 
2007-04-16 12:02
mq0532
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-9-7
收藏
得分:0 
那我不用写onselected事件了吧
2007-04-18 10:26
mq0532
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-9-7
收藏
得分:0 
就是想取得 下拉列表中的值 插到库里 就这么简单
2007-04-18 18:23
mq0532
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-9-7
收藏
得分:0 

<%@ Page language="c#" CodeFile="MyDropDownList.aspx.cs" AutoEventWireup="false" Inherits="MyDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>使用DropDownList控件显示数据库中的内容</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td colspan="3" width="100%">使用ListBox控件显示数据库中的内容如下:</td>
</tr>
<tr>
<td width="50%">
<asp:DropDownList id="SourceList" runat="server" Width="100%"></asp:DropDownList></td>
<td width="50%">
<asp:Button id="AddBtn" runat="server" Width="100px" Text="添加到列表中"></asp:Button></td>
</tr>
<tr>
<td>
<asp:ListBox id="DirectList" runat="server" Width="150px" Height="200px"></asp:ListBox>
</td>
<td valign="middle">
<br>
<asp:Button ID="DeleteBtn" Width="60" Runat="server" Text="×" CommandName="addone" Font-Bold="True"
ForeColor="Red"></asp:Button>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td colspan="3" width="100%">
<asp:Label id="SucessMessage" runat="server" ForeColor="Red" Font-Bold="True" Visible="False">请选中列表控件的数据!</asp:Label></td>
</tr>
</table>
</form>
</body>
</HTML>



using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration;

/// <summary>
/// Summary description for MyDropDownList.
/// </summary>
public partial class MyDropDownList : System.Web.UI.Page
{

private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["ACCESSCONNECTIONSTRING"].ToString();

private void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
///绑定源列表控件的数据
GetSourceListData();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DeleteBtn.Click += new System.EventHandler(this.DeleteBtn_Click);
this.AddBtn.Click += new System.EventHandler(this.AddBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void GetSourceListData()
{

///清空ListBox控件的所有数据项
SourceList.Items.Clear();
string AccessConnectionString = SQLCONNECTIONSTRING + Server.MapPath("csc.mdb");
///创建数据库的链接对象
String cmdText = "SELECT snid,srname from srname ORDER BY snid";
OleDbConnection myConnection = new OleDbConnection(AccessConnectionString);
OleDbCommand myCommand = new OleDbCommand(cmdText, myConnection);

///查询数据库
myConnection.Open();
OleDbDataReader reader = myCommand.ExecuteReader();

///读取SqlDataReader中的数据
while(reader.Read())
{
SourceList.Items.Add(new ListItem(reader["srname"].ToString(),reader["snid"].ToString()));
}

///关闭读取器和数据库的链接
reader.Close();
myConnection.Close();
}

private void AddBtn_Click(object sender, System.EventArgs e)
{
///添加下拉列表框中选择的数据项到列表框中
if(SourceList.SelectedIndex > -1)
{
DirectList.Items.Add(SourceList.SelectedItem.ToString());
}

///清空下拉框和列表框的选项
SourceList.SelectedIndex = -1;
DirectList.SelectedIndex = -1;
SucessMessage.Visible = false;
}

private void DeleteBtn_Click(object sender, System.EventArgs e)
{
if(DirectList.SelectedIndex > -1)
{
///删除列表框中选择的数据项
DirectList.Items.Remove(DirectList.SelectedItem);
SucessMessage.Visible = false;
}
else
{
///显示提示信息
SucessMessage.Visible = true;
}

}
}


这样做 在LISTBOX里无法添加值

可在同样的下面的程序里就可以实现 请高手大哥帮忙啊

<%@ Page language="c#" Codebehind="MyDropDownList.aspx.cs" AutoEventWireup="false" Inherits="Example_6_3.MyDropDownList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>使用DropDownList控件显示数据库中的内容</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table>
<tr>
<td colspan="3" width="100%">使用ListBox控件显示数据库中的内容如下:</td>
</tr>
<tr>
<td width="50%">
<asp:DropDownList id="SourceList" runat="server" Width="100%"></asp:DropDownList></td>
<td width="50%">
<asp:Button id="AddBtn" runat="server" Width="100px" Text="添加到列表中"></asp:Button></td>
</tr>
<tr>
<td>
<asp:ListBox id="DirectList" runat="server" Width="150px" Height="200px"></asp:ListBox>
</td>
<td valign="middle">
<br>
<asp:Button ID="DeleteBtn" Width="60" Runat="server" Text="×" CommandName="addone" Font-Bold="True"
ForeColor="Red"></asp:Button>
<br>
<br>
<br>
</td>
</tr>
<tr>
<td colspan="3" width="100%">
<asp:Label id="SucessMessage" runat="server" ForeColor="Red" Font-Bold="True" Visible="False">请选中列表控件的数据!</asp:Label></td>
</tr>
</table>
</form>
</body>
</HTML>


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace Example_6_3
{
/// <summary>
/// Summary description for MyDropDownList.
/// </summary>
public class MyDropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label SucessMessage;
protected System.Web.UI.WebControls.ListBox DirectList;
protected System.Web.UI.WebControls.DropDownList SourceList;
protected System.Web.UI.WebControls.Button AddBtn;
protected System.Web.UI.WebControls.Button DeleteBtn;
private readonly string SQLCONNECTIONSTRING = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"].ToString();

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///绑定源列表控件的数据
GetSourceListData();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DeleteBtn.Click += new System.EventHandler(this.DeleteBtn_Click);
this.AddBtn.Click += new System.EventHandler(this.AddBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void GetSourceListData()
{
///清空ListBox控件的所有数据项
SourceList.Items.Clear();

///创建数据库的链接对象
String cmdText = "SELECT Staff_ID,Staff_Name from Staff ORDER BY Staff_ID";
SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING);
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);

///查询数据库
myConnection.Open();
SqlDataReader reader = myCommand.ExecuteReader();

///读取SqlDataReader中的数据
while(reader.Read())
{
SourceList.Items.Add(new ListItem(reader["Staff_Name"].ToString(),reader["Staff_ID"].ToString()));
}

///关闭读取器和数据库的链接
reader.Close();
myConnection.Close();
}

private void AddBtn_Click(object sender, System.EventArgs e)
{
///添加下拉列表框中选择的数据项到列表框中
if(SourceList.SelectedIndex > -1)
{
DirectList.Items.Add(SourceList.SelectedItem);
Response.Write(SourceList.SelectedItem);
}

///清空下拉框和列表框的选项
SourceList.SelectedIndex = -1;
DirectList.SelectedIndex = -1;
SucessMessage.Visible = false;
}

private void DeleteBtn_Click(object sender, System.EventArgs e)
{
if(DirectList.SelectedIndex > -1)
{
///删除列表框中选择的数据项
DirectList.Items.Remove(DirectList.SelectedItem);
SucessMessage.Visible = false;
}
else
{
///显示提示信息
SucessMessage.Visible = true;
}

}
}
}


小弟跪求了

2007-05-08 16:34
快速回复:[求助]DropDownList连接数据库后选不了下拉列表里的值,提交后一直显示 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019373 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved