下面是我用.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();
}
}
}
///////////////////////控件代码//////////////////
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();
}
}
}
这里判断一下
myctr是不是null
不是,as 是类型转换的Page.LoadControl("mycontrol/promodel.ascx") 返回的是一个Control而不是promodel必须用as 来转换
最好判断一下是否为空
if(myctr!=null)
{
//TODO:你的操作
}另外用户控件里的属性最好用ViewState来存储,以免丢值
{
myctr.Product_nameid = sdr.GetString(5);
}有错误提示?CS0117: “System.Web.UI.Control”并不包含“Product_nameid”的定义说明没有转换成功。要引用mycontrol/promodel.ascx的命名空间
页面上直接拖拽这个控件时,直接该控件id.属性 = string.Empty;就可以了。
if(myctr!=null)
{
response.write("有");
}
else
{
response.write("无");
}
这样可以输出“有”,应该是能访问吧!
promodel myctr = Page.LoadControl("mycontrol/promodel.ascx") as promodel;
是的
{ get
{
if(ViewState["Product_nameid"]!=null)
{
return ViewState["Product_nameid"].ToString();
}
retrun string.Empty;
}
set
{
ViewState["Product_nameid"]= value;
}
}