下面是我用.aspx调用.ascx的问题,
///////////////////////控件代码//////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class promodel : System.Web.UI.UserControl
{
    private string product_nameid;
    public string Product_nameid
    {
        set
        {
            this.product_nameid = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SqlConnection con = db.createConnection();
            SqlCommand cmd = new SqlCommand( "select * from TabProName where product_typeid ='"+ this.product_nameid +"'",con);
            //cmd.Connection = con;
            con.Open();
            //cmd.CommandText =;
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            this.lblproname.Text = sdr.GetString(1);
            this.lblprotype.Text = sdr.GetString(4);
            this.lblproprice.Text = sdr.GetDecimal(3).ToString();
            this.imgpromodel.ImageUrl ="../images/" + sdr.GetSqlString(2).ToString();
            this.hplinfor.NavigateUrl = "~/default.aspx";
        }
        con.Close();
        sdr.Close();
    }
}
///////////////////////////////////页面代码///////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class product : System.Web.UI.Page
{
    private int rowNum = 2;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = db.createConnection();
            SqlCommand cmd = new SqlCommand("select top 4 * from TabProName  order by product_nameid DESC", con);
            con.Open();
            int i = 0;
            SqlDataReader sdr = cmd.ExecuteReader();
            sdr.Read();
            TableRow tr = new TableRow();
            while (sdr.Read())
            {
                i = i + 1;
                TableCell tc = new TableCell();
                Control myctr = Page.LoadControl("mycontrol/promodel.ascx");
                myctr.Product_nameid = sdr.GetString(5);-----错误在这里找不到我定义的属性,传不了值
                tc.Controls.Add(myctr);
                tr.Controls.Add(tc);
                if (i % this.rowNum == 0)
                {
                    this.tabpro1.Rows.Add(tr);
                    tr = new TableRow();
                }
            }
            if (i % this.rowNum != 0)
            {
                this.tabpro1.Rows.Add(tr);
            }
            sdr.Close();
            con.Close();
        }
    }
}

解决方案 »

  1.   

    Control myctr = Page.LoadControl("mycontrol/promodel.ascx");
    这里判断一下
    myctr是不是null
      

  2.   

    promodel myctr = Page.LoadControl("mycontrol/promodel.ascx") as promodel;
      

  3.   

    我的promodel myctr = Page.LoadControl("mycontrol/promodel.ascx") 后面加不上as promodel这句AS后面就没有promodel不知道这什么,是不是缺少USING
      

  4.   


    不是,as 是类型转换的Page.LoadControl("mycontrol/promodel.ascx")  返回的是一个Control而不是promodel必须用as 来转换
    最好判断一下是否为空
    if(myctr!=null)
    {
       //TODO:你的操作
    }另外用户控件里的属性最好用ViewState来存储,以免丢值
      

  5.   

    解决了没?criedshy的是正解。
      

  6.   

    用你的方法试了,控件加进来 了,现在提示是: CS0117: “System.Web.UI.Control”并不包含“Product_nameid”的定义
      

  7.   

    promodel myctr = Page.LoadControl("mycontrol/promodel.ascx") as promodel;if(myctr!=null)
    {
       myctr.Product_nameid = sdr.GetString(5);
    }有错误提示?CS0117: “System.Web.UI.Control”并不包含“Product_nameid”的定义说明没有转换成功。要引用mycontrol/promodel.ascx的命名空间
      

  8.   

    为什么要这么写?loadcontrol
    页面上直接拖拽这个控件时,直接该控件id.属性 = string.Empty;就可以了。
      

  9.   

    怎么引用mycontrol/promodel.ascx的命名空间?
      

  10.   

    你在页面里能访问promodel 不?我看你代码里都没有命名空间,应该不是这个问题
      

  11.   

    应该是可以访问的呀,我用
    if(myctr!=null)
    {
      response.write("有");
    }
    else
    {
      response.write("无");
    }
    这样可以输出“有”,应该是能访问吧!
      

  12.   


    promodel myctr = Page.LoadControl("mycontrol/promodel.ascx") as promodel;
    是的
      

  13.   

    可是为什么接收收不到Product_nameid的值呢?
      

  14.   

    Product_nameid 你最好用ViewState存储public string Product_nameid
    {   get
       {
          if(ViewState["Product_nameid"]!=null)
          {
             return ViewState["Product_nameid"].ToString();
          }
          retrun string.Empty;
       }
       set
       {
          ViewState["Product_nameid"]= value;
       }
    }