代码如下:
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;//database
using System.Data.OleDb;public class dtgNavigationBar : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid dtgNBar;
protected System.Web.UI.WebControls.Label Label1; private OleDbConnection conNBar;
private OleDbCommand cmdNBar;
private OleDbDataAdapter dapNBar;
private DataSet dstNBar;
private DataView dtvNBar;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//connect to database
string strConnAccess = "Provider = Microsoft.Jet.Ole.4.0; Data Source =" + HttpContext.Current.Server.MapPath("DataSource\\test.mdb");
conNBar = new OleDbConnection();
conNBar.ConnectionString = strConnAccess;
try
{
conNBar.Open();
         cmdNBar = new OleDbCommand();
cmdNBar.CommandText = "SELECT * FROM hqcs_NavigationBarItems";    
cmdNBar.Connection = conNBar;
dapNBar = new OleDbDataAdapter(cmdNBar);
dstNBar = new DataSet();
dapNBar.Fill(dstNBar,"NavigationBar");
         dtvNBar = dstNBar.Tables["NavigationBar"].DefaultView;
         
         //test number of records
         int nRecords = 0;
nRecords = dstNBar.Tables["NavigationBar"].Rows.Count;
Label1.Text = nRecords.ToString();
         dtgNBar.DataSource = dtvNBar;
dtgNBar.DataBind(); conNBar.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ID = "dtgNavigationBar";
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
我是把这个做成用户自定义控件,然后再一个aspx页面中使用这个用户自定义控件的,但是在目标aspx页面中没有任何显示。
请问这是什么问题?
小弟是新手,分数不多,故只能给点小分,还要留着以后问问题,请大家见谅。

解决方案 »

  1.   

    1 纠正一下 你这个是 用户控件 不是 自定义控件
    2 你在查询分析器内运行SELECT * FROM hqcs_NavigationBarItems看有结果没有
      

  2.   

    这里
    try
    {
    conNBar.Open();
             cmdNBar = new OleDbCommand();你的Command没有制定Connection
      

  3.   

    Access也有查询分析器吗?
    我在ms sql 2000里也创建了一个同样的数据库,用查询分析器运行了一下,有结果的。
    有点奇怪。
      

  4.   

    我在下面定义了cmd的connection了阿
    cmdNBar.Connection = conNBar;
      

  5.   

    我用的Access是2003 version的
    是不是和这个有关系阿。
    我换个底版本的试试。
      

  6.   

    贴完整 aspx ascx 和aspx.cs ascx.cs 代码
      

  7.   

    我想问,在aspx中使用用户控件的时候有什么要求吗?
    aspx为:
    <%@ Register TagPrefix="hqcs" TagName="hqcsNavigationBar" Src="../ascx/hqcsNavigationBar.ascx" %>
    <%@ Page language="c#" Codebehind="Index.aspx.cs" AutoEventWireup="false" Inherits="hqcs.Index" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>hqcs</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <hqcs:hqcsNavigationBar id="HqcsNavigationBar1"runat="server"></hqcs:hqcsNavigationBar>
    </form>
    </body>
    </HTML>
      

  8.   

    ascx是:
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="hqcsNavigationBar.ascx.cs" Inherits="hqcs.ascx.dtgNavigationBar" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <P><FONT face="宋体">
    <asp:datagrid id="dtgNBar" runat="server" >
    <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#000099"></SelectedItemStyle>
    <AlternatingItemStyle BackColor="#CCCCCC"></AlternatingItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="Black"></HeaderStyle>
    <FooterStyle BackColor="#CCCCCC"></FooterStyle>
    <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999"></PagerStyle>
    </asp:datagrid></FONT></P>
    <asp:Label id="Label1" runat="server"></asp:Label>
      

  9.   

    ascx.cs是:
    namespace hqcs.ascx
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; //database
    using System.Data.OleDb;
    using System.Data.SqlClient;

    //Global funstions
    //using hqcs.Global; /// <summary>
    /// Summary description for hqcsNavigationBar.
    /// </summary>
    public class dtgNavigationBar : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.DataGrid dtgNBar;
    protected System.Web.UI.WebControls.Label Label1; private OleDbConnection conNBar;
    private OleDbCommand cmdNBar;
    private OleDbDataAdapter dapNBar;
    private DataSet dstNBar;
    private DataView dtvNBar;
    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    //connect to database
    string strConnAccess = "Provider = Microsoft.Jet.Ole.4.0; Data Source =" + HttpContext.Current.Server.MapPath("DataSource\\test.mdb");
    conNBar = new OleDbConnection();
    conNBar.ConnectionString = strConnAccess;
    try
    {
    conNBar.Open(); //cmdNBar = new OleDbCommand("Select * from hqcs_NavigationBarItems",conNBar);
    cmdNBar = new OleDbCommand();
    cmdNBar.CommandText = "SELECT * FROM hqcs_NavigationBarItems";    
    cmdNBar.Connection = conNBar;
    dapNBar = new OleDbDataAdapter(cmdNBar);
    dstNBar = new DataSet();
    dapNBar.Fill(dstNBar,"NavigationBar"); dtvNBar = dstNBar.Tables["NavigationBar"].DefaultView; int nRecords = 0;
    nRecords = dstNBar.Tables["NavigationBar"].Rows.Count;
    Label1.Text = nRecords.ToString();
    dtgNBar.DataSource = dtvNBar;
    dtgNBar.DataBind(); conNBar.Close();
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex.Message);
    }

    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.ID = "dtgNavigationBar";
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  10.   

    看来看去 
    好像 this.ID = "dtgNavigationBar";
    这个不知道有什么用? 去掉试试或者你先把 strConnAccess  Response.Write(strConnAccess )出来看看是否 正确。
      

  11.   

    加上HttpContext.Current.Server.MapPath()之后,路径返回是:
    Provider = Microsoft.Jet.Ole.4.0;Data Source = D:\hqcs\Pages\DataSouce\hqcs.mdb
    怎么会是Pages\DataSouce\hqcs.mdb
    我用绝对路径加入之后,还是没有结果显示出来
    Provider = Microsoft.Jet.Ole.4.0;Data Source = D:\hqcs\DataSouce\hqcs.mdb
    有没有什么方法可以单独测试用户自定义控件的阿?
    我把这个代码直接放到页面中去试试,不用用户控件试试
    主要是对debug工具不熟悉,不知道怎么样去调试,看不到每一步的详细信息。
    去找篇调试的文章看看先。
      

  12.   

    大哥,谢谢,我终于找到原因了
    是我的provider的原因
    不是 Microsoft.Jet.Ole.4.0;
    而应该是 Microsoft.Jet.OleDb.4.0
    现在好了
    真是粗心阿。
    谢谢你这么热心帮助。