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!";
Related
I am using asp.net C#, I have some problems. I want to change text label in site master when click on button.
In site.master:
<asp:label runat="server" id="lblUser">
Other forms, I have tried with this code, but not working:
((Label)Master.FindControl("lblUser")).Text = "Hello USER";
This may work:
Master.Controls.OfType<System.Web.UI.WebControls.Label>.First(c => c.ID == "lblUser").InnerText = "Hello USER";
What you done is correct that must work.
you can achieve that like
In Site.Master.cs make label control public
public string GetUser
{
get
{
return lblUser.Text;
}
set
{
lblUser.Text = value;
}
}
Then set the value to GetUser in otherform.aspx.cs
if(!Ispostback)
{
Master.GetUser = "Hello USER";
}
I have a .ASPX page that pulls in a user control from a master page, and I am trying to call an update() method in one user control (hereby referred to as control1), to an update panel that is in another user control (hereby referred to as control2).
I have set a reference to control2 in the .ascx page of control1:
<%# Reference Control="~/controls/MobileMenu.ascx" %>
I have instanced control2 and am trying to call the public method from the code-behind of control1:
controls_MobileMenu mobi = new controls_MobileMenu();
mobi.Update(ThisCustomer);
Here is the method that fires in control2:
public void Update(Customer customer)
{
getItemNums(customer);
}
protected void getItemNums(Customer customer)
{
int numItems = ShoppingCart.NumItems(customer.CustomerID, CartTypeEnum.WishCart);
if (numItems > 0)
{
lbwishlist.ForeColor = Color.Red;
lbwishlist.Text = "My Wishlist ( " + numItems + " )";
}
else
{
lbwishlist.ForeColor = Color.White;
lbwishlist.Text = "My Wishlist";
}
}
When my code runs though, and I step through it, my lbwishlist linkbutton is null and the code throws a null reference exception. Can anyone explain why this is considered null when the other control calls it? And is there any way around this?
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";
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;
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";