在asp.net 2用户控件之间传递数据 [英] Passing Data between two usercontrols in asp.net
问题描述
我有一个UC_Categories.ascx (UC_1)哪些约束类别名称。 UC_Products.ascx (UC_2)将表明产品的类别名称。他们两人都是在网页称为BookShop.aspx(页)
i have a UC_Categories.ascx ( UC_1 ) which constraints categoryname . UC_Products.ascx ( UC_2 ) will show products by categoryname. Both of them are in a Page called BookShop.aspx ( Page )
在页面,当用户单击 UC_1 (步骤1),它将呈现一个 UC_2 按类别名称(步骤2)。我处理由发送带有参数的要求就是要类别名称的页第1步。第二步:创建一个新的 UC_2 后,设置的属性值,类别名称,并执行FillProductByCategoryName方法。然后加入 UC_2 ,即可在页面的占位符。但我不显示的 UC_2 。
In the Page, when user click a UC_1 (step 1 ) , it will render a UC_2 by categoryname (step2 ). I process step 1 by sending a request with param that is categoryname to Page. Step2 create a new UC_2 ,set Properties value that is categoryname , and execute FillProductByCategoryName method. then add UC_2 to a PlaceHolder in the Page. but i don't show UC_2 .
我需要帮助或建议大家
感谢您阅读我的问题!
PS:我的英语不是很好。
Thank you for reading my question! ps : my english isn't very well.
在UC2的codebehind:
in the codebehind of the UC2 :
public void FillProduct()
{
ProductsMN productsMN = new ProductsMN();
if (dlBook == null)
{
dlBook = new DataList();
dlBook.DataSource = productsMN.GetByCategoryName(CategoryName);
dlBook.DataBind();
}
else
{
dlBook.DataSource = productsMN.GetByCategoryName(CategoryName);
dlBook.DataBind();
}
}
public string CategoryName { get; set; }
在页面codebehind
in the codebehind of the page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
string categoryName = Request.QueryString["categoryName"] as string;
if (!string.IsNullOrWhiteSpace(categoryName))
{
BookContent.Controls.Clear(); // BookContent : Placeholder
Control c = Page.LoadControl("~/UC/UC_Books.ascx") as UC.UC_Books;
UC.UC_Books ucBook = new UC.UC_Books();
ucBook.CategoryName = categoryName;
ucBook.FillProduct(); //line 10
BookContent.Controls.Add(ucBook); //line 11
}
}
在页面的pageLoad的,useBook包含数据。但在页面(视图),我没有看到数据。我认为// line11不执行或不正确的。
at the PageLoad of the Page, useBook contains data. but in the Page (view ), i don't see data. i think //line11 isn't execute or not true .
推荐答案
您需要公开的公共属性和用户控件的控件的父页面的构造函数。
You need to expose public properties and a constructor of the UserControl's Controls to the parent page.
假设你的用户控件得到了一个标签:
Say your Usercontrol has got a label:
<asp:Label ID="MyLabel" runat="server" visible="true"/>
在用户控件的codebehind添加此。
In the codebehind of the UserControl add this.
//Constructor
public MyUserControl()
{
Category = new Label();
}
//Exposing the Label
public Label Category
{
get { return this.MyLabel; }
set { this.MyLabel = value; }
}
假设你已经添加了用户控件到父页面,它的ID是的MyUserControl。
Assume you have added the UserControl to the parent page and its ID is "MyUserControl".
要在用户控件的标签值设置为使用的东西这样的:
To set the label value of the UserControl to something use this:
MyUserControl.Category.Text=Response.QueryString["categoryname"];//Obviously you would want to encode it first.
如果您需要调用在该用户的codebehind父页面的功能,你将不得不使用委托。我不会推荐,虽然这个方法。
If you need to call functions of the parent page in the UserControl's codebehind, you will have to use delegates. I would not recommend this method though.
这篇关于在asp.net 2用户控件之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!