网上找个很多相似的问题都没解决我的问题呀,使用DropDownList无论怎么改都只能取第一个值呀,前台代码如下
<td width="130" align="center">按学生统计学时数</td>
<td><asp:DropDownList ID="d" runat="server" Width="100">
</asp:DropDownList>
<asp:Button Text="选择" OnClick="SubmitBtn_Click" runat="server"/>
<asp:Label runat="server" ID="h" />
</td>
-------------------------
后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ArrayList a= new ArrayList();
a.Add("中");
a.Add("高");
this.d.DataSource=a;
this.d.DataBind(); }
}public void SubmitBtn_Click(object sender, EventArgs e)
{
//string selected=Request.Form[d.UniqueID];
string selected=d.SelectedItem.Value;//每次这里都只能取到第一个值呀
string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + @"\mydb.mdb";
OleDbConnection conn = new OleDbConnection(connectionstring);
DataSet ds = new DataSet();
OleDbDataReader dr;
string commandtext = "select * from myt where 实到人数<>null and 大中专='"+selected+"'";
OleDbCommand comm = new OleDbCommand(commandtext,conn);
comm.Connection.Open();
dr=comm.ExecuteReader();
int su=0;
int as1=0;
int k=0;
int ad=0;
string dd=null;
while(dr.Read())
{
as1=Convert.ToInt32(dr["实到人数"]);
su=su+as1;
}
dr.Close();
}
<td width="130" align="center">按学生统计学时数</td>
<td><asp:DropDownList ID="d" runat="server" Width="100">
</asp:DropDownList>
<asp:Button Text="选择" OnClick="SubmitBtn_Click" runat="server"/>
<asp:Label runat="server" ID="h" />
</td>
-------------------------
后台代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ArrayList a= new ArrayList();
a.Add("中");
a.Add("高");
this.d.DataSource=a;
this.d.DataBind(); }
}public void SubmitBtn_Click(object sender, EventArgs e)
{
//string selected=Request.Form[d.UniqueID];
string selected=d.SelectedItem.Value;//每次这里都只能取到第一个值呀
string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + @"\mydb.mdb";
OleDbConnection conn = new OleDbConnection(connectionstring);
DataSet ds = new DataSet();
OleDbDataReader dr;
string commandtext = "select * from myt where 实到人数<>null and 大中专='"+selected+"'";
OleDbCommand comm = new OleDbCommand(commandtext,conn);
comm.Connection.Open();
dr=comm.ExecuteReader();
int su=0;
int as1=0;
int k=0;
int ad=0;
string dd=null;
while(dr.Read())
{
as1=Convert.ToInt32(dr["实到人数"]);
su=su+as1;
}
dr.Close();
}
a.Add("高","2"
);
Ht.Add("中", "1");
Ht.Add("高", "2");
this.DropDownList1.DataSource = Ht;
this.DropDownList1.DataValueField = "key";
this.DropDownList1.DataTextField = "value";
this.DropDownList1.DataBind();
this.d.Items.Add(new ListItem("高","2"))
像你如果要取下拉框的"中"、"高"这几个字的话,就用:
string selected=d.SelectedItem.Text.ToString()
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" AutoEventWireup="true" EnableEventValidation="false" Debug="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.WebControls.WebParts" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>语音室使用统计</title>
<style type="text/css">
<!--
.STYLE1 {
font-size: xx-large;
color: #000000;
}
-->
</style>
</head>
<body>
<br /><br />
<script language="c#" runat="server">
void SubmitBtn_Click(object sender, EventArgs e)
{
string selected=d.SelectedItem.ToString();
string connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + @"\mydb.mdb";
OleDbConnection conn = new OleDbConnection(connectionstring);
DataSet ds = new DataSet();
OleDbDataReader dr;
string commandtext = "select * from myt where 实到人数<>null and 大中专='"+selected+"'";
OleDbCommand comm = new OleDbCommand(commandtext,conn);
comm.Connection.Open();
dr=comm.ExecuteReader();
int su=0;
int as1=0;
int k=0;
int ad=0;
string dd=null;
while(dr.Read())
{
as1=Convert.ToInt32(dr["实到人数"]);
su=su+as1;
}
dr.Close();
h.Text=Convert.ToString(su);
}
</script>
<div align="center" class="STYLE1">语音室使用统计</div>
<form id="q" runat="server">
<table width="500" border="1" align="center">
<tr>
<td width="130" align="center">按学生统计学时数</td>
<td><asp:DropDownList ID="d" runat="server" Width="100">
<asp:ListItem Value="中">中</asp:ListItem>
<asp:ListItem Value="高">高</asp:ListItem>
</asp:DropDownList>
<asp:Button Text="选择" OnClick="SubmitBtn_Click" runat="server"/>
<asp:Label runat="server" ID="h" />
</td>
</tr> <tr>
<td width="130" align="center">按老师统计学时数</td>
<td><asp:GridView ID="GridView1" runat="server" Width=100% AutoGenerateColumns="True">
</asp:GridView>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
改为
string selected=d.SelectedValue;试试
这一句不对!~~
<asp:ListItem Value="中">中 </asp:ListItem>
<asp:ListItem Value="高">高 </asp:ListItem>
你的Value和Text都是同样的值,,也可以写成
<asp:ListItem Text="中" Value="中"></asp:ListItem>
你要取Text值,要用:string selected=d.SelectedItem.Text.ToString();
你要取Value值,要用:string selected=d.SelecteValue.ToString()
或者是:string selected=d.SelectedItem.Value.ToString()
改成string selected=d.SelectedItem.Value;也还是不行
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="d" runat="server" Width="100">
</asp:DropDownList>
<asp:Button ID="Button1" Text="选择" OnClick="SubmitBtn_Click" runat="server" />
<asp:Label runat="server" ID="h" />
</div>
</form>
</body>
</html>using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ArrayList a = new ArrayList();
a.Add("中");
a.Add("高");
this.d.DataSource = a;
this.d.DataBind();
}
} protected void SubmitBtn_Click(object sender, EventArgs e)
{
//string selected=Request.Form[d.UniqueID];
string selected = d.SelectedItem.Value;//每次这里都只能取到第一个值呀 }
}
}
{
this.d.Items.Add(new Itemlist("中","0");
this.d.Items.Add(new Itemlist("高","1");
}
<td><asp:DropDownList ID="d" runat="server" Width="100">
<asp:ListItem Value="t">中</asp:ListItem>
<asp:ListItem Value="t1">高</asp:ListItem>
</asp:DropDownList>
<asp:Button Text="选择" OnClick="SubmitBtn_Click" runat="server"/>
<asp:Label runat="server" ID="h" />
</td>
在ListItem中,如果Value是中文就只能选择第一个值,如果是英文就可以了,我也不知道是什么原因,还是请高人分需一下,还得感谢很多号心人呀
onselectedindexchanged="DropDownList73_SelectedIndexChanged" Width="98%">
<asp:ListItem>选择</asp:ListItem>
<asp:ListItem>对</asp:ListItem>
<asp:ListItem>端</asp:ListItem>
<asp:ListItem>设</asp:ListItem>
<asp:ListItem>表</asp:ListItem>
</asp:DropDownList>
Mc = DropDownList73.Text