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

[求助]DropDownList 控件绑定.

垃圾的沉默 发布于 2007-08-23 17:01, 438 次点击
<%@ Page Language="C#" Debug="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb"%>
<script runat="server">
private void Page_Load(Object sender,EventArgs E)
{
if(!IsPostBack)
{
DataZLei();
}
}
//-----------------------------------------
public void DataZLei()
{
OleDbConnection cnn;
OleDbCommand cmd;
OleDbDataReader datar;
string strcnn,sql;
strcnn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../../EDNdata/EDNdatabase.mdb");
cnn=new OleDbConnection(strcnn);
cnn.Open();
sql="select * from ZLei";
cmd=new OleDbCommand(sql,cnn);
datar=cmd.ExecuteReader();
while(datar.Read())
{
ListItem theitem=new ListItem();
theitem=ListItem(datar["Title"].ToString());
theitem.Value=datar["ZClass"].ToString();
drop1.Item.add(theitem);
}
cnn.Close();
}
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<form runat="server">
<table width="760" height="150" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="185" align="right">类别:</td>
<td width="575"><asp:DropDownList id="drop1" runat="server" BackColor="#000000" BorderColor="#330000" class="gcc" >
</asp:DropDownList></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>
string不能隐式转换为Object......
ListItem theitem=new ListItem();
theitem=ListItem(datar["Title"].ToString());
theitem.Value=datar["ZClass"].ToString();
drop1.Item.add(theitem);
错误在这里..
我不知道怎么改 大家帮帮我..我折腾了一天了
3 回复
#2
冰彩虹2007-08-23 18:24
什么错误?

//记事本里写的,可能有些拼写等错误吧
while(datar.Read())
{
ListItem theitem = new ListItem(datar["Title"].ToString(),datar["ZClass"].ToString());
drop1.Items.add(theitem);
}
#3
川流不息2007-08-24 04:30
赞成楼上写法,关于楼主的错误,愚见:
ListItem theitem=new ListItem();//这里是一个新的对象
theitem=ListItem(datar["Title"].ToString());//这里错了,你必须在ListItem前面加new才行。
theitem.Value=datar["ZClass"].ToString();
drop1.Item.add(theitem);

#4
console2007-08-25 20:17
SqlDataReader dr = null;
string ConnectionString = "server=.;database=pubs;uid=as;pwd=sa";
try
{
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select UserName from UserInfo",con);
dr = cmd.ExecuteReader();
this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "要帮定的字段名";
this.DropDownList1.DataBind();
}
catch(Exception ex)
{
throw new Exception("错作错误"+ex.Message);
}

这样行不
1