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中。 请教如何改正?

解决方案 »

  1.   

    难道你后台编码文件没有定义dropAuthors,可不可以把源码全部贴出来.
      

  2.   

    首先,确定CS页面头部有这样一句
    using System.Web.UI.WebControls;
    其次,在Page_Load之上要有这一句
    protected System.Web.UI.WebControls.DropDownList dropAuthors;
    因为有时候.NET会发生错误,没有把它写到后台去.
      

  3.   

    后台如下,前台不变:
    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
    }
    }
      

  4.   

    to  hchxxzx(NET?摸到一点门槛):
    已经照做了改动,应该对了,可是还有问题:)
      

  5.   

    看语句是没有问题,你错误的提示还是原来的错误吗?
    建议你这样:
    新建一个页面,此页面与现有页面相同,但不能用拷贝方式.
    在页面添加一个下拉框,转到后台,上述后台语句拷过去,试试看行不行.不过你这个语句可能有一点小问题
    dropAuthors.DataTextField = "au_lname";
    再添加一句
    dropAuthors.DataValueField = "au_lname";
      

  6.   

    你把你的SQL语句拿到数据库中求一下,看有没有得到值Select au_lname From Authors
      

  7.   

    hchxxzx(NET?摸到一点门槛) :
    早求过了,没有,郁闷得啊,谢谢你:)
      

  8.   

    to hchxxzx(NET?摸到一点门槛) :
    不是没有,是有结果的,但不能显示到DropDownList中
      

  9.   

    不应该啊.你换一个SQL语句看看.并且改一下你的语法.
    dropAuthors.DataSource = dtrAuthors;
    dropAuthors.DataTextField = "au_lname";
    dropAuthors.DataValueField = "au_lname"; //添加这一句
    dropAuthors.DataBind();
      

  10.   

    你可以放个DATAGRID试试,帮定了看看确保查出来结果了没有,
      

  11.   

    不对,当然不对
    你的reader没有read呢,reader的用法你没搞对,reader和Dataset的用法完全不一样。
      

  12.   

    你要继续用Reader还是换成DataSet??如果用Reader的话不要用DataSource,在while(dtrAuthors.Read())里用Add(new ListItem(dtrAuthors["au_lname"].ToString(),dtrAuthors["au_lname"].ToString())来添加项。如果换成DataSet我就不说了,相信你知道怎么做
      

  13.   

    绑定不行,必须循环添加,在dtrAuthors = cmdSelect.ExecuteReader();
    后添加
    while(dtrAuthors.Read())
    {
    dropAuthors.Items.Add(new ListItem(dtrAuthors["au_lname"].ToString(),dtrAuthors["au_lname"].ToString())
    }