Im trying to access a ModalPopupExtender control and it allways is returning null or object set not set to an intance of an object. I've tried master.Page.FindControl("") and master.FindControl("") and im still not getting the result
MasterPage master = Page.Master as MasterPage;
AjaxControlToolkit.ModalPopupExtender popup = master.Page.FindControl("ModalPopupExtender2") as AjaxControlToolkit.ModalPopupExtender;
Updated: Cannot change the text of my labels in master page
MasterPage master = Page.Master;
AjaxControlToolkit.ModalPopupExtender popup1 = master.FindControl("ModalPopupExtender1") as AjaxControlToolkit.ModalPopupExtender;
Label lblMessage = master.FindControl("lblMessage") as Label;
lblMessage.Text = msg;
Literal ltrlMessage = master.FindControl("ltrlMessage") as Literal;
ltrlMessage.Text = msg;
Label MessageStatus = master.FindControl("lblMessageStatus") as Label;
MessageStatus.Text = msgStatus;
popup1.Show();
you could do
MasterPage master = Page.Master;
If your page is a child of your master page
try this:
AjaxControlToolkit.ModalPopupExtender popup = (AjaxControlToolkit.ModalPopupExtender)Page.Master.FindControl("ModalPopupExtender2");
Regards
Check out this answer. You can have a strongly typed master page, so you don't have to find and then cast the control. The control on the master would be publicly accessible, and the page would know the type of the master page and have it accessible.
EDIT:
the control is not public
Assuming you've set the Master property in your page directive:
<%# Page MasterPageFile="~/MyMaster.master" ...
Odds are, you probably don't need to actually get to the control. Rather, you need to set something in the master page. I'd just use an internal method to do what you need to do:
public partial class MyMaster: MasterPage
{
internal void SetTheFoo(string foo)
{
this.WhateverControl.Text = foo;
}
//etc...
}
Then, from your page, just call it:
Master.SetTheFoo("Foo");
If you still need to get to the control, then in your master page, you could add a public property exposing your modal popup extender.
public AjaxControlToolkit.ModalPopupExtender MyModalPopup
{
get { return this.TheNonPublicModalPopupExtenderControl; }
}
Related
I have this following property in my custom user control:
public string selectedtab
{
get
{
if (ViewState["AdminCurrentNavID"] != null)
{
return ViewState["AdminCurrentNavID"].ToString();
}
else {
isfirstload = true;
return null;
}
}
set { ViewState["AdminCurrentNavID"] = value; }
}
I am setting the value of it on my Page_Load() in ascx control. What i need to do is that after setting the value of this property I need to access it from masterpage.cs in code behind. you can see how currently I am trying to do in below code, but the issue is that I am not able to get the value i thing it is because the masterpage's Page_Load() rendering before the ascx control so I thats why I am getting null value, please help, thanks.
masterpage.cs:
usercontrols.mainmenu adminmenu = (usercontrols.mainmenu)LoadControl("~/mymenupath.ascx");
lbmsg.Text = adminmenu.selectedtab;
When you call LoadControl in your master page, you are actually creating a new instance of your user control, not accessing the one you have somewhere in your site.
When you declare the User Control in your page you should have given it an id. You could access the property with something like ((usercontrols.mainmenu)MyUserControlId).selectedtab
I found the solution by using Delegate, you can see in the link below.
http://webdeveloperpost.com/Articles/Return-value-from-user-control-in-ASP-NET-and-C-Sharp.aspx
I have a custom user control that I made called OrderForm.ascx. I also have an .aspx file that utilizes the OrderForm control.
I want to access a control on the .aspx file from the OrderForm control. Is there a way to do this?
You could use the FindControl method in the user control like this:
Label label = Page.FindControl("Label1") as Label;
if (label != null)
string labelText = label.Text;
As a note on the above, depending on where the Label is in the page, you may need to use recursion to find the Label.
You could also create a property on the page that returns the text of the Label:
public string LabelText
{
get { return Label1.Text; }
}
To access the property from the user control, here are two options:
Option #1
string labelText = ((PageName)Page).LabelText;
Option #2
string labelText = Page.GetType().GetProperty("LabelText").GetValue(Page, null).ToString();
If you have two user controls, ControlA and ControlB, and they are both registered on the same page you can easily access one from the other. Simply create a public property that you want to have access to in ControlB such as:
Public ReadOnly Property ControlB_DDL() As DropDownList
Get
Return Me.ddlItems
End Get
End Property
And then you can reference that property in ControlA after finding that control:
ControlB ctrlB = (ControlB)Page.FindControl("cB");
DropDownList ddl = ctrlB.ControlB_DDL;
Refer here for more info: http://www.dotnetcurry.com/ShowArticle.aspx?ID=155
To access the controls of .ascx in .aspx.
HiddenField selectedEmailsId = performanceReportCtrl.FindControl("CONTROLID") as HiddenField;
And to access controls of aspx in ascx.
HiddenField selectedEmailsId = Page.FindControl("CONTROLID") as HiddenField;
I have created a menu control in asp.net and have put it into master page. This menu control have property ActiveItem which when modified makes one or another item from menu appear as active.
To control active item from child page I have created a control which modifies master page property enforced by IMenuContainer interface to update menu control's active item in master page.
public class MenuActiveItem : Control
{
public ActiveItemEnum ActiveItem
{
get
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return ActiveItemEnum.None;
return masterPage.ActiveItem;
}
set
{
var masterPage = Page.Master as IMenuContainer;
if (masterPage == null)
return;
masterPage.ActiveItem = value;
}
}
}
Everything works perfectly and I really enjoy this solution, but I was thinking that, if I knew how, I would have created a custom directive with same feature instead of custom control because it just makes more sense that way.
Does anybody know how to do it?
You should be able to turn this into a custom property of your Page, which you can set in the Page directive.
Create a base class for your page, and then change your Page directives like this:
<%# Page Language="C#" MasterPageFile="~/App.master"
CodeFileBaseClass="BasePage" ActiveItem="myActiveItem" AutoEventWireup="true"
CodeFile="Page1.aspx.cs" Inherits="Page1" %>
You may have to change the property to be a string, and do a conversion to the enum. But otherwise your code can remain the same, and it doesn't have to be in a control.
How can I hide a user control on the master page from a content page? This is code I have on my content page's load.
Dim banner As UserControl = DirectCast(Master.FindControl("uc_banner1"), UserControl)
banner.Visible = True
Does nothing for me :(
Expose the visible property of the user control via a property of the MasterPage.
In your MasterPage:
public bool MyBannerVisibility
{
get { return uc_banner1.Visible; }
set { uc_banner1.Visible = value; }
}
Then make sure to add a reference in your content page:
<%# MasterType TypeName="YourMasterPageTypeName" %>
Then in your content page just do:
Master.MyBannerVisibility = false;
Edit: Since your using VB.net I used a code converter to convert it for you:
Public Property MyBannerVisibility() As String
Get
Return uc_banner1.Visible
End Get
Set
uc_banner1.Visible = value
End Set
End Property
Content Page:
Master.MyBannerVisibility = False
Good day everyone,
I am building a page in ASP.NET, and using Master Pages in the process.
I have a Content Place Holder name "cphBody" in my Master Page, which will contain the body of each Page for which that Master Page is the Master Page.
In the ASP.NET Web page, I have a Content tag (referencing "cphBody") which also contains some controls (buttons, Infragistics controls, etc.), and I want to access these controls in the CodeBehind file. However, I can't do that directly (this.myControl ...), since they are nested in the Content tag.
I found a workaround with the FindControl method.
ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Master.FindControl("cphBody");
ControlType myControl = (ControlType) contentPlaceHolder.FindControl("ControlName");
That works just fine. However, I am suspecting that it's not a very good design. Do you guys know a more elegant way to do so?
Thank you!
Guillaume Gervais.
I try and avoid FindControl unless there is no alternative, and there's usually a neater way.
How about including the path to your master page at the top of your child page
<%# MasterType VirtualPath="~/MasterPages/PublicUI.Master" %>
Which will allow you to directly call code from your master page code behind.
Then from your master page code behind you could make a property return your control, or make a method on the master page get your control etc.
public Label SomethingLabel
{
get { return lblSomething; }
}
//or
public string SomethingText
{
get { return lblSomething.Text; }
set { lblSomething.Text = value; }
}
Refers to a label on the master page
<asp:Label ID="lblSomething" runat="server" />
Usage:
Master.SomethingLabel.Text = "some text";
//or
Master.SomethingText = "some text";
Rick Strahl has a good explanation (and sample code) here - http://www.west-wind.com/Weblog/posts/5127.aspx
Nothing to do different. Just write this code on child page to access the master page label control.
Label lblMessage = new Label();
lblMessage = (Label)Master.FindControl("lblTest");
lblMessage.Text = DropDownList1.SelectedItem.Text;
I use this code for acess to files recursively:
/// <summary>
/// Recursively iterate through the controls collection to find the child controls of the given control
/// including controls inside child controls. Return all the IDs of controls of the given type
/// </summary>
/// <param name="control"></param>
/// <param name="controlType"></param>
/// <returns></returns>
public static List<string> GetChildControlsId(Control control, Type controlType)
{
List<string> FoundControlsIds = new List<string>();
GetChildControlsIdRecursive(FoundControlsIds, control, controlType);
// return the result as a generic list of Controls
return FoundControlsIds;
}
public static List<string> GetChildControlsIdRecursive(List<string> foundControlsIds, Control control, Type controlType)
{
foreach (Control c in control.Controls)
{
if (controlType == null || controlType.IsAssignableFrom(c.GetType()))
{
// check if the control is already in the collection
String FoundControl = foundControlsIds.Find(delegate(string ctrlId) { return ctrlId == c.ID; });
if (String.IsNullOrEmpty(FoundControl))
{
// add this control and all its nested controls
foundControlsIds.Add(c.ID);
}
}
if (c.HasControls())
{
GetChildControlsIdRecursive(foundControlsIds, c, controlType);
}
}
Hi just thought i'd share my solution, found this works for accessing a 'Control' that is inside an < asp:Panel> which is on a 'ContentPage', but from C# code-behind of the 'MasterPage'. Hope it helps some.
add an < asp:Panel> with an ID="PanelWithLabel" and runat="server" to your ContentPage.
inside the Panel, add an < asp:Label> control with ID="MyLabel".
write (or copy / paste the below) a function in your MasterPage Code-behind as follows: (this accesses the label control, inside the Panel, which are both on the ContentPage, from the Master page code-behind and changes its text to be that of a TextBox on the Master page :)
protected void onButton1_click(object sender, EventArgs e)
{
// find a Panel on Content Page and access its controls (Labels, TextBoxes, etc.) from my master page code behind //
System.Web.UI.WebControls.Panel pnl1;
pnl1 = (System.Web.UI.WebControls.Panel)MainContent.FindControl("PanelWithLabel");
if (pnl1 != null)
{
System.Web.UI.WebControls.Label lbl = (System.Web.UI.WebControls.Label)pnl1.FindControl("MyLabel");
lbl.Text = MyMasterPageTextBox.Text;
}
}