I have a user control in my master page under the login view under the Role groups.Here is the control.Its not in the content placeholder.However if i try to access it i get null result.How can i access this from my page code behind.I am having problem getting it from master page.
<asp:RoleGroup Roles="Students">
<ContentTemplate>
<uc1:studentsPanel runat="server" ID="studentcontrol" />
</ContentTemplate>
</asp:RoleGroup>
Here is how i am having my code
LoginView control = Page.Master.FindControl("studentcontrol") as LoginView;
if (control != null)
{
Label1.Text = "found";
}
Here's the code I use to get to controls in MasterPages
//Master page from user control
LoginControl control
Page page = (Page)this.Page;
MasterPage master = (MasterPage)page.Master;
control= (LoginControl )master.FindControl("studentcontrol");
if (control!= null)
{
Label1.Text = "found";
}
We can't see the whole code, but your snippets seem correct. Try first not to cast your control to LoginView - the reason might be that your panel is not of that type. To try out if the control is found at all, use
if(Page.Master.FindControl("studentcontrol") != null) {
Label1.Text = "found";
}
first before adding another possible source of failure.
There's two more explicit answers that don't need to be repeated here. You find wonderful explanations here and here.
as #Krishnraj said,
i dont know which is control within studentsPanel UserControl, but i assume that Label. You should access like that,
var Loginview = (Master.FindControl("LoginView1") as LoginView);
Control cont = new Control();
Loginview.RoleGroups[0].ContentTemplate.InstantiateIn(cont);
(cont.Controls[1].FindControl("_trylbl") as Label).Text = "Hello say";
Related
I have a project in VS 2013 with master page. In the Root.master I have the "Log In" which redirects me to the login page.
Log In
When I login I'm redirected to a certain page, let's say it Default.aspx.
In Default.aspx.cs I have:
if (Session["username"] == null)
{
Response.Redirect("Logare.aspx");
}
else
{
loginLink.Text ="Welcome, "+ Session["username"];
}
How could I do so when I'm logged in the text from Root.master to change from "Log In" to "Welcome, username"? I tried to get the loginLink id from Root.master but it's not known in Default.aspx.cs.
UPDATE
This is how "Log In" from Root.master is now:
<asp:Label ID="Label8" runat="server" Text="Log In"></asp:Label>
In Default.aspx.cs I have now:
if (Session["username"] == null)
{
Response.Redirect("Logare.aspx");
}
else
{
Label mpLabel = new Label();
mpLabel = (Label)Master.FindControl("Label8"); mpLabel.Text = "Welcome, " + Session["username"];
}
But, with this, I'm getting an error
Object reference not set to an instance of an object.
at mpLabel.Text = "Welcome, " + Session["username"];
So if I understood your question correctly, this is just about accessing control declared on the master page from within the content page code behind. You have two options to achieve that.
FindControl
Code is simple (borrowed from OP's own comment):
Label mpLabel = (Label) Master.FindControl("loginLink");
However be aware that FindControl works only with immediate children of the control. So if your structure looks like
MasterPage
Control1
Control2
loginLink
You will need to something like this:
Control c1 = Master.FindControl("Control1");
Control c2 = c1.FindControl("Control2");
Label mpLabel = (Label) c2.FindControl("loginLink");
Alternatively you can use recursive version of FindControl . It is not available out of the box, but many (really many) versions are on the web already. Here is one.
Expose controls via Master's interface
In your master page code behind define a property like that:
public Label LoginLabel
{
get { return this.loginLabel; }
}
And on the content page just use it. Don't forget to cast master to your specific type:
Label loginLabel = ((YourMasterPageClass)Master).LoginLabel;
That might not be particularly safe, so you can expose just label text instead:
public Label LoginLabelText
{
get { return this.loginLabel.Text; }
set { this.loginLabel.Text = value; }
}
((YourMasterPageClass)Master).LoginLabelText = "Welcome!";
My master page contains this:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Label ID="User" runat="server" Text="Welcome, " Visible="false"></asp:Label>
</asp:ContentPlaceHolder>
I'm trying to access the label like this:
Label welcomeLabel;
ContentPlaceHolder cPlaceHolder;
cPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (cPlaceHolder != null)
{
welcomeLabel = (Label)cPlaceHolder.FindControl("User");
if (welcomeLabel != null)
{
welcomeLabel.Text = "Welcome, " + u.Name;
welcomeLabel.Visible = true;
}
}
I've checked that ContentPlaceHolder1 is actually found and using HasControls() on it returns true but I can't seem to access the label.
Help me?
I also having same prblem.
what i have done is
MasterPage ctl00 = FindControl("ctl00") as MasterPage;
ContentPlaceHolder cplacehld = ctl00.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
Label label1= cplacehld.FindControl("User") as Label;
try with:
((Label)this.Page.Master.FindControl("User")).Text = "Welcome, " + u.Name;
((Label)this.Page.Master.FindControl("User")).Visible = true;
This issue dogged me for about two hours until I realized that if I have a asp:Content tag on my page with the contentplaceholderID set to the ID for the contentplaceholder in the master page that I will never be able to access any controls in the contenplacholder. The asp:Content page is always merged with the MasterPage content, even if the asp:Content tag is empty on your content page. To provide for the default content, I moved my label outside of the contentplaceholder tag and set the visibility to false. If I then dynamically determined that I didn't have any content for my asp:Content tag I would then just set the label visibility to true to display my default content. Not exactly elegant, but it works.
I have a login control and at is nested 2 deep in a header control
i.e Page --> Header Control --> Login Control. I cannot get a reference to the control on the page using FindControl. I want to be able to set the visible property of the control like
if (_loginControl != null)
_loginControl.Visible = false;
I ended up using a recursive FindControl method to find the nested control.
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
Are you needing to disable/hide the User Control from the ASP.NET page it resides on (or does the User Control exist on a master page, say)? If it's in the same page, then in your ASP.NET page's code-behind you'd do:
MyUserControlsID.Visible = false
Where MyUserControl is the ID of your User Control. To determine the ID of your User Control look at the markup of your .aspx page and you will see something like this:
<uc1:UserControlName ID="MyUserControlsID" runat="server" ... />
Happy Programming!
A good way would be to use:
Page.FindControl()
if that yields null, the control is not there.
Try calling this.FindControl("_loginControl") or this.Page.FindControl("_loginControl").
See MSDN for method details:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx
The login control, if it's registered in the markup, will also be an instance member of your codebehind page; you can refer to it from the codebehind class as if it were a normal member, using the same name you provided as the ID (I do recommend using codebehinds for most logic, instead of inlining code in the markup, BTW).
You can also use the FindControl() method of your page, which will search its control subtree for a control with a given ID. That takes longer, so I would recommend the first option unless the logic control is added dynamically and you don't always know it's there.
private List<Control> GetAllNestedUserControl(Control ph)
{
List<Control> Get = new List<Control>();
foreach (var control in ph.Controls)
{
if (control is UserControl)
{
UserControl uc = control as UserControl;
if (uc.HasControls())
{
Get = GetAllNestedUserControl(uc);
}
}
else
{
Control c = (Control)control;
if (!(control is LiteralControl))
{
Get.Add(c);
}
}
}
return Get;
}
just call this code from you any parent page and then get any control by the following code
List<Control> Get = GetAllNestedUserControl(ph);
Label l = (Label)Get.Find(o => o.ID == "lblusername");
l.Text = "changed from master";
For example, if i have on the aspx page:
<asp:PlaceHolder ID="tab_0" runat="server" Visible="false"></asp:PlaceHolder>
<asp:PlaceHolder ID="tab_1" runat="server" Visible="false"></asp:PlaceHolder>
and i want to access these properties in the code behind page using values from a configuration file for example
string enabledTabs = "0,1,2,3";
if there a way i can use reflection to set them to enabled or disabled e.g.
foreach(var id in enabledTabs.Split(','))
{
// <use reflection to get the correct tab control>
// Set property of the tab
tab.Visible = true;
}
I could acheive the result i want by using a switch statement and setting the particular control property, but i'd like to use reflection to get the tab to make it cleaner.
Could anyone help?
Thanks!
You don't need reflection. Use Page.FindControl:
foreach(var id in enabledTabs.Split(','))
{
PlaceHolder control = (PlaceHolder)this.FindControl("tab_"+id));
control.Visible = true;
}
foreach(var id in enabledTabs.Split(','))
{
// Set property of the tab
Page.FindControl("tab_" + id.ToString()).Visible = true;
}
Try the following:
Control tab = Control.FindControl("tab_"+id);
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 :)