部分代码如下:
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 pet
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table2;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Label Label1;
private int rowNum = 2;
protected System.Web.UI.Control hotPetControl;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection con = DB.createConnection();
SqlCommand cmd = new SqlCommand("select top 4 petID from pet order by petID desc",con);
con.Open();
int i=0;
SqlDataReader sdr = cmd.ExecuteReader();
TableRow tr = new TableRow();
while(sdr.Read())
{
i=i+1;
TableCell tc = new TableCell();
hotPetControl hpc = (hotPetControl)this.LoadControl("hotPetControl.ascx");
hpc.PetID=sdr.GetString(0);
tc.Controls.Add(hpc);
tr.Controls.Add(tc);
if(i%this.rowNum==0)
{
this.Table2.Rows.Add(tr);
tr = new TableRow();
}
}
if(i%this.rowNum!=0)
{
this.Table2.Rows.Add(tr);
}
sdr.Close();
con.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
}
}编译的时候提示没有using命名空间,怎么会这样,是不是需要倒入什么命名空间才能用用户控件的类打照对象hotPetControl hpc = (hotPetControl)this.LoadControl("hotPetControl.ascx");hotPetControl 是用户控件项目hotPetControl.ascx的名字

解决方案 »

  1.   

    using 用户控件hotPetControl的类所在的名称空间protected System.Web.UI.Control hotPetControl;//该句去掉
      

  2.   

    我知道的有两种
    1。 protected System.Web.UI.WebControls.PlaceHolder PH;  
        Control myControl1=Page.LoadControl("control/Get_U_information.ascx");
        这个不能更改控件的属性
    2。在aspx页面注册控件<%@ Register TagPrefix="rxjy" TagName="header" Src="control/header.ascx" %>
      在cs文件使用控件的类所在的命名空间,并定义一个控件的对象,
    GU=(Get_U_information)Page.LoadControl("control/Get_U_information.asc")PH.Controls.Add(GU);
    this.GU.editbuton.Click +=new System.EventHandler(this.GU.editbuton_Click);
      

  3.   

    namespace pet
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    /// <summary>
    /// hetPetControl 的摘要说明。
    /// </summary>
    public class hetPetControl : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Label labName;
    protected System.Web.UI.WebControls.Label labType;
    protected System.Web.UI.WebControls.Label labPrice;
    protected System.Web.UI.WebControls.HyperLink hlDetails;
    protected System.Web.UI.WebControls.Image petImage;
    private string petID; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!this.IsPostBack)
    {
    SqlConnection con = DB.createConnection();
    SqlCommand cmd = new SqlCommand("select * from pet where perID='"+this.petID+"'",con);
    con.Open();
    SqlDataReader sdr = cmd.ExecuteReader();
    sdr.Read();
    this.labName.Text = sdr.GetString(1);
    this.labType.Text = sdr.GetString(2);
    this.labPrice.Text = sdr.GetSqlMoney(3).ToString();
    this.hlDetails.NavigateUrl = "petDetails.aspx?petID="+this.petID;
    this.petImage.ImageUrl = "/pet/petPhoto/"+sdr.GetString(4);
    con.Close();
    }
    } public string PetID
    {
    set
    {
    this.petID = value;
    }
    }用了一楼的方法了
    using pet;
    还是不对啊!愚蒙中!11