DropDownList的绑定的问题:DropDownList.aspx如下:
<html>
<head>
<title>DropDownList.aspx</title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:DropDownList ID="dropAuthors" Runat=server/>
<asp:Button Text="Pick Author!" Runat=server ID="Button1" />
</form>
</body>
</html>我在DropdownList.aspx.cs下的
private void Page_Load(object sender, System.EventArgs e)
{}
中写入以下代码:
// 在此处放置用户代码以初始化页面
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors;
// Retrieve records form database
conPubs = new SqlConnection(@"Server=HARVARD\MYSQL;UID=sa;PWD=;Database=Pubs");
conPubs.Open();
cmdSelect = new SqlCommand("Select au_lname From Authors",conPubs);
dtrAuthors = cmdSelect.ExecuteReader();
// Bind to Repeater
dropAuthors.DataSource = dtrAuthors;
dropAuthors.DataTextField = "au_lname";
dropAuthors.DataBind();
dtrAuthors.Close();
conPubs.Close();
所提示的错误是:
d:\inetpub\wwwroot\ASP.NET揭露\DropDownList.aspx.cs(35): 找不到类型或命名空间名称“dropAuthors”(是否缺少 using 指令或程序集引用?)
这怎么就错了呢,原来写在一个页面是不错的啊? 不解,在线waiting中。 请教如何改正?
<html>
<head>
<title>DropDownList.aspx</title>
</head>
<body>
<form runat="server" ID="Form1">
<asp:DropDownList ID="dropAuthors" Runat=server/>
<asp:Button Text="Pick Author!" Runat=server ID="Button1" />
</form>
</body>
</html>我在DropdownList.aspx.cs下的
private void Page_Load(object sender, System.EventArgs e)
{}
中写入以下代码:
// 在此处放置用户代码以初始化页面
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors;
// Retrieve records form database
conPubs = new SqlConnection(@"Server=HARVARD\MYSQL;UID=sa;PWD=;Database=Pubs");
conPubs.Open();
cmdSelect = new SqlCommand("Select au_lname From Authors",conPubs);
dtrAuthors = cmdSelect.ExecuteReader();
// Bind to Repeater
dropAuthors.DataSource = dtrAuthors;
dropAuthors.DataTextField = "au_lname";
dropAuthors.DataBind();
dtrAuthors.Close();
conPubs.Close();
所提示的错误是:
d:\inetpub\wwwroot\ASP.NET揭露\DropDownList.aspx.cs(35): 找不到类型或命名空间名称“dropAuthors”(是否缺少 using 指令或程序集引用?)
这怎么就错了呢,原来写在一个页面是不错的啊? 不解,在线waiting中。 请教如何改正?
解决方案 »
- |zyciis| 如何为控件设置一个属性像TextBox中的Text一样的属性(值为标记中的数据)谢谢
- iframe里面输入的时候直接粘贴内存中的内容如何清除HTML代码
- asp.net MVC3+EF哪个版本?
- SqlDataReader的简单问题?十分感谢
- 进来帮帮忙急
- 如何判断已经登陆 asp.net 急
- 打开的数据控件不能刷新数据,怎么办?
- 请帮我看看这个网站,指点迷津!
- 如何在数据库里,把存储的用逗号分割的词组一个一个的读出来?高手帮忙呀!
- 如何设置导出Excel中的每个单元格的宽度?
- 100分请教高手怎样实现: 点击"添加图片"按钮,就生成一个HtmlInputFile控件,和一个“删除”按钮,就和126.com邮箱里添加附件那样。点“
- 请教一对一聊天怎么实现?
using System.Web.UI.WebControls;
其次,在Page_Load之上要有这一句
protected System.Web.UI.WebControls.DropDownList dropAuthors;
因为有时候.NET会发生错误,没有把它写到后台去.
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;namespace ASP.NET揭露
{
/// <summary>
/// DropDownList 的摘要说明。
/// </summary>
public class DropDownList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList dropAuthors; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors;
// Retrieve records form database
conPubs = new SqlConnection(@"Server=HARVARD\MYSQL;UID=sa;PWD=;Database=Pubs");
conPubs.Open();
cmdSelect = new SqlCommand("Select au_lname From Authors",conPubs);
dtrAuthors = cmdSelect.ExecuteReader();
// Bind to Repeater
dropAuthors.DataSource = dtrAuthors;
dropAuthors.DataTextField = "au_lname";
dropAuthors.DataBind();
dtrAuthors.Close();
conPubs.Close();
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
已经照做了改动,应该对了,可是还有问题:)
建议你这样:
新建一个页面,此页面与现有页面相同,但不能用拷贝方式.
在页面添加一个下拉框,转到后台,上述后台语句拷过去,试试看行不行.不过你这个语句可能有一点小问题
dropAuthors.DataTextField = "au_lname";
再添加一句
dropAuthors.DataValueField = "au_lname";
早求过了,没有,郁闷得啊,谢谢你:)
不是没有,是有结果的,但不能显示到DropDownList中
dropAuthors.DataSource = dtrAuthors;
dropAuthors.DataTextField = "au_lname";
dropAuthors.DataValueField = "au_lname"; //添加这一句
dropAuthors.DataBind();
你的reader没有read呢,reader的用法你没搞对,reader和Dataset的用法完全不一样。
后添加
while(dtrAuthors.Read())
{
dropAuthors.Items.Add(new ListItem(dtrAuthors["au_lname"].ToString(),dtrAuthors["au_lname"].ToString())
}