Thursday, 21 May 2015

How to Access Parent Page (.aspx) control (IDs) from its child User Control (.ascx) in asp.net


how to access .aspx page in User Control (.ascx) page



Description:-

In this example we explain that how to access .aspx page in UserControl (.acsx) page. Or how to pass .aspx page value to UserControl (.ascx) page in asp.net.

There is a live requirement that we need sometime and at that time we confused that how to use this all value that is in ASPX page to UserControl. So there are two way to use aspx page value in usercontrol page.by using public properties and second is page item collection.

Using Page Item Collection

There is new item collection packed with Page object from 2.0 framework. We can also use this collection to pass objects to usercontrols.

Note:
We can also use HttpContext objects item collection for this.



Aspx Page CodeBehind
protected void Page_Load(object sender, EventArgs e)
    {      
        Page.Items.Add("Name", "ZZZ");
    }
UserControl CodeBehind
public partial class Header : System.Web.UI.UserControl
{
   protected void Page_Load(object sender, EventArgs e)
    {
        lblName.Text = Page.Items["Name"].ToString();
    }
}


0 comments:

Post a Comment