我用一个CheckBoxList控件绑定数据,不知道为什么什么都不显示,调试无错误提示,代码也没发现什么错误,麻烦大家帮我看看是什么原因,小弟先谢了。代码如下:
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.CheckBoxList chkTitles;
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrTitles; conPubs = new SqlConnection( @"Server=LCH;uid=sa;pwd=123;Database=Pubs" );
cmdSelect = new SqlCommand( "Select Title From Titles", conPubs );
conPubs.Open();
dtrTitles = cmdSelect.ExecuteReader(); chkTitles.DataSource = dtrTitles;
chkTitles.DataTextField = "Title";
chkTitles.DataBind(); dtrTitles.Close();
conPubs.Close();
}
}

解决方案 »

  1.   

    1. 确认 Page_Load 执行了private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
      Response.Write("Page_Load 执行了<BR>");
      .....
    }

    }2. 确认
    Select Title From Titles有记录返回3. 在浏览器端查看一下HTML源码
      

  2.   

    思归大哥,我还是找不出什么原因啊,没道理啊。我用别的控件绑定就能显示。我在浏览器查看HTML源码如下:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body>
    <form name="Form1" method="post" action="WebForm1.aspx" id="Form1">
    <input type="hidden" name="__VIEWSTATE" value="dDwtMjAwODM4NTM0Njs7bDxjaGtUaXRsZXM6MDs+PtyU7prrxkSPVTTx++UayUxUfHq2" /> <P><FONT face="宋体"></FONT></P>
    <P>&nbsp;</P>
    <P>
    <table id="chkTitles" border="0"></table></P>
    <P>&nbsp;</P>
    <P><FONT face="宋体">
    <input type="submit" name="Button1" value="Button" id="Button1" /></FONT></P>
    </form>
    </body>
    </HTML>
      

  3.   

    check if Page_Load is executed or not, check you have<%@ Page AutoEventWireup="true" %> in your aspx or you havethis.Load += new EventHandler(Page_Load);in your codebehind