Sometimes a user control needs to access some public methods or variables or any other objects for that matter in its parent page. The user control provides the Page object which gives access to its container parent. The problem with this page object is that, it is the general page object. This means that this page object doesnt contain the necessary type details which will enable you to access the object in the required parent container.
In order to achieve this, the general page object has to be type casted to its parent type after which the accessible objects within the parent will be visible to the intellisense.
For eg. if the parent page has the following code..
namespace testwebapp
{
public partial class parentpage : System.Web.UI.Page
{
public string setLabelvalue
{
set
{
Label1.Text = value;
}
get
{
return Label1.Text;
}
}
}
}
In the above code, I am seeting the value of the label to string 'Pradeep'. In order to call this public property from the user control contained in this page, use the following code.
namespace testwebapp
{
public partial class ChildControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
((parentpage)Page).setLabelvalue = "pradeep";
}
}
}
In the above code the following statment type casts the Page object to the 'parentpage' type.
((parentpage)Page).setLabelvalue = "pradeep";
Sunday, September 2, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment