Reading User control property inside Nested Master pages in Content page - c#

I have a user control which is in the root master page. The content page connects to this root master page via nested master page
root.master>apps.master>content.aspx
the user control in the root.master has a drop downlist that sets a property when the dropdownlist selection is changed.
I need to access this user control property in the content page.
any help is appreciated
user control property
private string _userCurrentCity = string.Empty;
public string userCurrentCity
{
get { return _userCurrentCity; }
set { _userCurrentCity = value; }
}
protected void ddl_City_SelectedIndexChanged(object sender, EventArgs e)
{
string CurrentCity = "";
CurrentCity = ddl_City.SelectedItem.Text;
lbl_CurrentCity.Text = CurrentCity;
HiddenField_CityID.Value = ddl_City.SelectedValue;
UpdatePanel2.Update();
userCurrentCity = CurrentCity;//this sets the usercontrol property
}
in my content page
UserControl cnt = this.Master.Master.FindControl("Change1") as UserControl;
lbl_Result.Text = cnt.userCurrentCity;
is this correct, i set the userCurrentCity property in the ddl selected change event. Your code looks logical but it is not working.

You need some code like this in code behind:
UserControl cnt = this.Master.FindControl("IDOfTheUserControl") as UserControl
after that:
cnt.Property //to access the wanted property.
EDIT: I don't understand yhat you have nested master pages.
Try this.Master.Master.FindControl and other things like before.
UserControl cnt = this.Master.Master.FindControl("IDOfTheUserControl") as UserControl

Related

how to change master page label's text after a button click on child page

I am using shopping cart icon showing number of products in cart. but when i add item to cart shopping is not updated.so i want to know if there is any method to change master page label's text after a button click on child page.
I would recommend to provide a public property in your MasterPage that you can use to set/get the Label's Text.
in your Master(assuming it's type is called SiteMaster):
public String ShoppingCartNumber{
get{ return LblShoppingCart.Text; }
set{ LblShoppingCart.Text = value; }
}
In your button's click event handler:
SiteMaster master = (SiteMaster)Page.Master;
master.ShoppingCartNumber = "1234";
This approach is straight-forward, less prone to errors and easily readable. You could even change the control in your master without needing to change the pages (f.e. if you want to replace the Label with a TextBox).
Try this
Label mpLabel = (Label) Master.FindControl("masterPageLabel");
if(mpLabel != null)
{
Label1.Text = "Master page label = " + mpLabel.Text;
}
Try this:
Add in your masterpage.cs file:
public Label lbl
{
get { return YourLabelId; }
set { YourLabelId= value; }
}
Add this in your content page:
<%# MasterType VirtualPath="~/YourMasterPageName.Master" %>
Then access in content page in your button click event:
string name = Master.lbl.text;

Find Image Control from user control

I am really new to ASP and C#.
I have a User control which has Two Radio Buttons, and and Image control.
I want to load this control dynamically on click of a button and at the same time give ImageURL to the image control.
FileUpload is on the aspx page.
Can anyone help me??
Control MyUserControl = LoadControl("MyControl.ascx"); PlaceHolder1.Controls.Add(MyUserControl);
I was able to load the user control.
Now how to provide the imageURL.
Thanks in advance.
public partial class MyUserControl:UserControl
{
public string ImageUrl
{
set{ image1.ImageUrl=value;}
get{return image1.ImageUrl;}
}
}
var ctrl=LoadControl("MyControl.ascx") as MyUserControl;
if(ctrl!=null)
{
ctrl.ImageUrl = "image.mpg";
}
Expose the image URL in your MyControl.ascx as public
Then casting MyUserControl to your usercontrol and assign the image
example :
Control MyUserControl1 = LoadControl("MyControl.ascx");
MyUserControl temp = (MyUserControl) MyUserControl1;
temp.ImageURL = "urlhere.jpg";

Change panel "visible" property on MasterPage from Child pages

I want to show some panel with a label, both located on a MasterPage, from inside it's child pages.. I already did the coding on the MasterPage:
public class MyMaster : MasterPage
{
public void ShowPanel(string pMessage)
{
labelInside.Text = pMessage;
myPanel.visible = true;
}
}
Then I make the calls from child pages:
public void ShowPanel(string pMessage)
{
MyMaster masterPage = this.Master as MyMaster;
masterPage.ShowPanel(pMessage);
}
This "works" ok, but it won't show nothing, since I need the page to be "refreshed" in an "ajax-way" like an UpdatePanel, which I can't use because the Trigger is in another page, right?
I really need this to work.. even if you have another completely different way to do this, I would appreciate.
You must place your panel inside an UpdatePanel(UpdateMode conditional) and in ShowPanel call its Update method.
Have you considered having the masterpage just have a placeholder for the label, but having each child page put its own content label inside that placeholder, which it would then have full control over?
you can subClass your page, and expose a property say.. MyPage.FooVisible
than in your masterPage, you can:
myPage = this.Page as MyPage
if (myPage != null) myPage.FooVisble = false;
in your page you can handle that any way you like,
FooVisible {
set { SomeElement.Visible = value; }
}
pseudo code of course :)

Accessing top master page properties in a nested master page code behind

I have a nested master page that has its own master page. The parent master page has a property defined in its code behind.
Public ReadOnly Property SelectedPage() As String
Get
Return _selectedPage
End Get
End Property
How can I reference the parent master page's property from within either the child master page's code behind Page_Load or aspx template page?
VB.Net:
DirectCast(Master, MyMastPageType).SelectedPage
C#:
((MyMastPageType)Master).SelectedPage
http://msdn.microsoft.com/en-us/library/system.web.ui.masterpage.master.aspx
protected void Page_Load(object sender, EventArgs e)
{
MyDemoMaster m = Master as MyDemoMaster;
m.MyProperty = "My button text";
}
See:
How to access controls inside a
nested master page?
The right way of accessing Master
page properties from a child page
Like this:
DirectCast(MyMastPageType, Master).SelectedPage
Here is how I use
MasterPage tmp = this.Master;
while (tmp.Master != null)
{
tmp = tmp.Master;
}

Dynamically adding Content blocks to Masterpage fails after Master.FindControl

I've encountered an odd problem that doesn't make any sense to me. I am trying to dynamically set up MasterPage Content controls on a page. I have it working nicely with the following code:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
MasterPageFile = "~/MasterPages/Default.master";
string existantContentPlaceHolderID = "ContentPlaceHolder1";
string nonExistantContentPlaceHolderID = "foo";
//Control c = Master.FindControl(existantContentPlaceHolderID);
//Control c1 = Master.FindControl(nonExistantContentPlaceHolderID);
TextBox t = new TextBox
{
Text = "Text"
};
ITemplate iTemplate = new GenericITemplate(container => container.Controls.Add(t));
AddContentTemplate(existantContentPlaceHolderID, iTemplate);
}
public delegate void InstantiateTemplateDelegate(Control container);
public class GenericITemplate : ITemplate
{
private readonly InstantiateTemplateDelegate m_instantiateTemplate;
public void InstantiateIn(Control container)
{
m_instantiateTemplate(container);
}
public GenericITemplate(InstantiateTemplateDelegate instantiateTemplate)
{
m_instantiateTemplate = instantiateTemplate;
}
}
This works great, except I want to be able to double-check that the contentPlaceHolderIDs exist on the MasterPage before calling AddContentTemplate as the Page will throw an error if you add a Content control that points to a non-existing ContentPlaceHolder.
The problem I am having is that in the above example when I call one of the commented Master.FindControl lines, the TextBox no longer renders.
Does anyone have any ideas why this might be... I cannot makes heads or tails of what is going on.
Thanks,
Max
The problem is that AddContentTemplate just records its parameters in a hashtable ready to be combined with the master page instance when it is created. Calling it after the master page has been created won't do anything, and reading the Master property causes the master page to be created.
The best way I can see around this is to create a separate instance of the master page with LoadControl, which you can inspect without affecting the page's own Master property...
MasterPage testMaster = (MasterPage) LoadControl( MasterPageFile );
Control c = testMaster.FindControl(existantContentPlaceHolderID);
There's some overhead in creating a second instance, but it's not immediately obvious to me whether it will be worth worrying about.

Categories

Resources