access controls on a asp.net master page from child page - asp.net

i want to access a span on a asp.net master page from child page, so i'd made a public property on that master page-->
public partial class Ui_MasterPage_UI : System.Web.UI.MasterPage
{
public int tax = 0;
public string notification
{
set
{
(this.FindControl("notification") as HtmlAnchor).InnerText = value.ToString();
}
}
------------------//some code
}
and now wants to access this from a child page to set some text into that htmlanchor tag,
so that i'd written some script-->
child page
public partial class Ui_ProductDetails : System.Web.UI.Page
{
protected void ListView_ProductDetails_itemcommand(object sender, ListViewCommandEventArgs e)
{
Master.notification = "some text"; ////////showing error
------------------//some code
}
------------------//some code
}
but getting the syntax error
i think there is some problem in above code,,,,,so plz review it......
is there any other way to do this ???
thnku

You need to cast the Page.Master property to the type of your Master Page.
((Ui_MasterPage_UI)Page.Master).Notification = "some text";

Related

Call parent page function from user control

I have a Default.aspx page and I am using a usercontrol in it. On some condition in usercontrol.cs I have to invoke a function present in Default.aspx.cs page (i.e parent page of user control). Please help and tell me the way to do this task.
You have to cast the Page property to the actual type:
var def = this.Page as _Default;
if(def != null)
{
def.FunctionName();
}
the method must be public:
public partial class _Default : System.Web.UI.Page
{
public void FunctionName()
{
}
}
But note that this is not best-practise since you are hard-linking the UserControl with a Page. Normally one purpose of a UserControl is reusability. Not anymore here. The best way to communicate from a UserControl with it's page is using a custom event which can be handled by the page.
Mastering Page-UserControl Communication - event driven communication
Add an event to the user control:
public event EventHandler SpecialCondition;
Raise this event inside your user control when the condition is met:
private void RaiseSpecialCondition()
{
if (SpecialCondition != null) // If nobody subscribed to the event, it will be null.
SpecialCondition(this, EventArgs.Empty);
}
Then in your page containing the user control, listen for the event:
public partial class _Default : System.Web.UI.Page
{
public void Page_OnLoad(object sender, EventArgs e)
{
this.UserControl1.OnSpecialCondition += HandleSpecialCondition;
}
public void HandleSpecialCondition(object sender, EventArgs e)
{
// Your handler here.
}
}
You can change the EventArgs to something more useful to pass values around, if required.
parent.aspx.cs
public void DisplayMsg(string message)
{
if (message == "" || message == null) message = "Default Message";
Response.Write(message);
}
To Call function of parent Page from user control use the following:
UserControl.ascx.cs
this.Page.GetType().InvokeMember("DisplayMsg", System.Reflection.BindingFlags.InvokeMethod, null, this.Page, new object[] { "My Message" });
This works fine for me..
Try this
MyAspxClassName aspxobj= new MyUserControlClassName();
aspxobj.YourMethod(param);

find control in masterpage

I am trying to set a label in master page from content page, not using FindControl. So, in master page I declared:
public partial class MainMasterPage : System.Web.UI.MasterPage
{
public string UserOfficeLabel
{
get { return lblUserOffice.Text; }
set { lblUserOffice.Text = value; }
}
public string OfficeLocationLabel
{
get { return lblOfficeLocation.Text; }
set { lblOfficeLocation.Text = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
....
}
}
"UserOfficeLabel" and "OfficeLocationLabel" are labels on master page. Then in content page (.aspx) I added the following directive under "Page" directive:
<%# MasterType VirtualPath="~/Main/MainMasterPage.master" %>
In conent page's code behind (.cs file) I try to access/set the label:
Master.UserOfficeLabel = ...
but UserOfficeLabel is not an option for Master (VS Intellisense does not list it as an option). When I add it anyway, it says "MainMasterPage.UserOfficeLabel is inaccessible to its protection level"
I think you can find what you are looking for here:
http://odetocode.com/blogs/scott/archive/2005/07/16/mastertype-in-asp-net-2-0.aspx.
In theory when you compile you should see the code below inside a partial class
Public Shadows ReadOnly Property Master() As otc
Get
Return CType(MyBase.Master,otcMaster)
End Get
End Property
I've done something like what you are trying by declaring a variable
Dim LocalMasterPageRef As MyMasterPageName
LocalMasterPageRef = CType(Me.Master, MyMasterPageName)
...
LocalMasterPageRef.xxxx
Hope it helps.

Pass Data into User Control on Master Page from Sub Page

I have a user control on the master page and I would like to pass in a value into that user control from the subpage, how would I be able to pass the values?
This control is in the master page
<%# Register TagPrefix="test" TagName="Data" Src="controls/TEST.ascx" %>
This code variable is within the user control
public partial class Controls_TEST : System.Web.UI.UserControl
{
private string _Title;
public string Title
{
get { return _Title; }
set { _Title = value; }
}
}
Code within the subpage
public partial class sub_page : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Controls_Test m = LoadControl("~/Controls/TEST.ascx");
m.Title = "TEST";
}
}
Note the sample code within subpage does not work because it cannot find that user control within the subpage.
I've tried Page.Master.FindControl and it also does not work for me. PLease help.
Use properties to communicate from your Page to your MasterPage and use properties to communicate from your MasterPage to the UserControl.
To get a reference to the control in your MasterPage you should provide a public property that returns it:
For example(in MasterPage):
public Controls_Test MyControl
{
get
{
return Controls_TEST1;
}
}
And you can call this property from one of your ContentPages in this way(f.e. if your master's type is named "SiteMaster"):
protected void Page_Load(object sender, EventArgs e)
{
((SiteMaster)Page.Master).MyControl.Title = "TEST";
}
As a rule of thumb: the more you encapsulate your controls, the more robust ,failsafe, maintanable and extendable your code will be.
Hence it would be better to provide only access to the Title rather than to the whole UserControl.
In MasterPage:
public String Title
{
get
{
return Controls_TEST1.Title;
}
set
{
Controls_TEST1.Title = value;
}
}
In the ContentPage:
((SiteMaster)Page.Master).Title = "TEST";
On this way you could change the logic and controls in your UserControl and MasterPage without having problems in your pages that already have accessed the UserControl directly.

Control posts back http request, how do I get it? (asp.net)

Request["ControlName"] doesn't work because I have been told the control is probably in a container... so how do I get the http request? The FORM part of the Request starts like this:
{__EVENTTARGET=ctl00%24lstThemeChooser&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=%2fwEP
lstThemeChooser is the name of my control...
Blue Theme
Orange Theme
defined in my masterpage aspx file
Attention! Pseudocode!
public interface IMyMasterPage
{
string ThemeChooserID { get; }
}
public partial class MyMasterPage : MasterPage, IMyMasterPage
{
public string ThemeChooserID { get { return lstThemeChooser.ClientID; } }
}
public MyPage : Page
{
protected void Page_PreInit(...)
{
string id = ((IMyMasterPage)this.Master).ThemeChooserID;
string val = Request[id];
}
}
ASP.NET prepends the container to the control name, so your control on the page is called ctl00%24lstThemeChooser.
Try
Request[lstThemeChooser.ClientID]

Update links on master page with data from child page

I have a menu of report links in my master page. I need to append an ID to the end of each whenever the user changes a value on the child page. What's a good way to accomplish this?
UPDATE: I should have mentioned that the child update is happening inside an UpdatePanel, meaning the master page is not reloaded when the change happens.
A MasterPage is really a child control of the page which it controls. You can control a MasterPage like any other control on your page (almost). All you need to do is get a reference to it.
You add a property to the code of your MasterPage, so its code may look something like this:
public partial class _default : System.Web.UI.MasterPage
{
protected string m_myString = string.Empty;
public string myString
{
get { return m_myString; }
set { m_myString = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Then you have to cast the this.Master property to your MasterPage
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Cast here to get access to your MasterPage
_default x = (_default)this.Master;
x.myString = "foo";
}
}
In response to your UPDATE:
The updated panel could write the ID to a hidden field and the menu events could look for that hidden fields in Request.Form["fieldName"].
Note that you shouldn't fieldName.Text because ASP.NET does a bad job of returning the right value for fields that have been AJAXed.

Resources