How to Add attribute on Master page body, and access that from an user control is being used under that master page.?
 | Add attribute in master page body, access from user control |
| <body runat="server" id="MasterBody" class="MainTable" >
| | 1:
| | 2:In Master page Code behind file
| | 3:
| | 4:public HtmlGenericControl BodyTag
| | 5: {
| | 6: get
| | 7: {
| | 8: return MasterBody;
| | 9: }
| | 10: set
| | 11: {
| | 12: MasterBody = value;
| | 13: }
| | 14: }
| | 15:
| | 16:
| | 17:
| | 18:In user Control Pre render .
| | 19:This is how u can access the master page property from a user control is being used under that master page
| | 20:
| | 21: MasterPage objMaster = this.Page.Master;
| | 22: System.Type t = objMaster.GetType();
| | 23: object bodyObj = (object)t.GetProperty("BodyTag").GetValue(objMaster, null);
| | 24: HtmlGenericControl body = (HtmlGenericControl)bodyObj;
| | 25: body.Attributes.Add("onKeyPress", "return checkKeyCode(event);");
| | 26: |
|
 | How to access a master page property in user control |
| protected void Page_PreRender(object sender, EventArgs e)
| | 1: {
| | 2:
| | 3: MasterPage objMaster = this.Page.Master;
| | 4:
| | 5: System.Type t = objMaster.GetType();
| | 6: object userObj = (object)t.GetProperty("objUser").GetValue(objMaster,null) ;
| | 7: PortalUser user = (PortalUser)userObj;
| | 8:
| | 9: if (user.Id == 0)
| | 10: {
| | 11: this.IsLoggedIn = false;
| | 12: }
| | 13: else
| | 14: {
| | 15: this.IsLoggedIn = true;
| | 16: }
| | 17:
| | 18: }
| | 19: |
|