Reputation: 147
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 )
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 .
I need a help or suggest from everyone.
Thank you for reading my question! ps : my english isn't very well.
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; }
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
}
}
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 .
Upvotes: 0
Views: 2865
Reputation: 7375
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"/>
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; }
}
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.
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.
Upvotes: 1