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.
Related
I have a webform with a checkbox in it. I need to do two things differently based on an environment setting.
Add a class
Add the Text attribute so a label gets created
<% if setting == true) { %>
<asp:CheckBox ID="optionCheckbox" class="option-checkbox radio-checkbox" runat="server" Text="Label Text"/>
<% } else { %>
<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
<% } %>
The problem with this is the page won't render because the ids are the same, even though only one could ever get rendered. There is a lot of other processing with javascript and such so I don't want different ids for each scenario.
I was able to fix this by keeping only the "base" checkbox code below and then overriding the PreRender event to add the class and set the Text attribute there.
<asp:CheckBox ID="optionCheckbox" class="option-checkbox" runat="server"/>
Code Behind:
CheckBox optionCheckbox = this.optionCheckbox as CheckBox;
if (optionCheckbox != null)
{
optionCheckbox.Text = "Label Text";
optionCheckbox.Attributes.Add("class", "option-checkbox radio-checkbox");
}
I'd still like to know if there is a way to do this in the markup file though.
Sorry about this syntax , I don't use ASPNet webPage.
You can implement it logically..
Firstly , define class and text variable and set value for business..
Finaly set checkbox class and Text value by variable
var class = "option-checkbox";
var text = "";
if(setting == true)
{
class = "option-checkbox radio-checkbox";
text = "Label Text";
}
<asp:CheckBox ID="optionCheckbox" class="setClassProperty" runat="server" Text="SetTextProperty"/>
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 have this in my Master page:
<asp:ContentPlaceHolder ID="BannerPlaceHolder" runat="server">
<asp:HyperLink ID="PortalBanner" runat="server"
ImageUrl="Images/banner-12-5-11.jpg"
NavigateUrl="~/Default.aspx"></asp:HyperLink>
</asp:ContentPlaceHolder>
I want to be able to change the hyperlink ImageUrl but I get a null exception:
Object reference not set to an instance of an object.
Here's my code to access the HyperLink
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("BannerPlaceHolder");
HyperLink hp= (HyperLink)cp.FindControl("PortalBanner");
Are you sure that it is in the Master of the Master? If not, simply change it to:
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.FindControl("BannerPlaceHolder");
Another approach is to provide a property in your master that you can access from your page by casting it to the actual type.
in the master's codebehind:
public string PortalBannerImageUrl
{
get {
return this.PortalBanner.ImageUrl;
}
set {
this.PortalBanner.ImageUrl = value;
}
}
in the page:
var myMaster = this.Master as YourMasterType;
if(myMaster != null)
{
myMaster.PortalBannerImageUrl = newImageUrl;
}
I want to update master page hyperlinks from child page. Here is the code i written to update
master page elements.
HyperLink h1 = this.Master.FindControl("AnLogin") as HyperLink;
h1.NavigateUrl = "#";
h1.Text = Session["UserName"].ToString();
HyperLink h2 = this.Master.FindControl("AnLogout") as HyperLink;
h2.Text = "Logout";
h2.NavigateUrl = "~/Logout.aspx";
if (Session["UserType"].ToString() == "Admin")
{
Response.Redirect("~/Admin.aspx");
}
Master page is updating only when i am not redirecting to another page. If i am redirecting to another page, that hyperlinks remain same as static.
Here i need to update master page hyperlinks that should be same for all pages further i can traverse. How to accomplish this?
Here i suggest not to post back the page for such small operations.
You can set this condition on aspx page like :-
<% if (Session["UserType"].ToString() == "Admin") {%>
// do something
<%} else { %>
// do something
<%} %>
I work with a master page in which there is one HyperLink. I set that HyperLink NavigateUrl form (master page's) child page.
for that i use this code in child page.
HyperLink hl = (HyperLink)this.Master.FindControl("linkviewmysite");
hl.NavigateUrl = "../" + ds.Tables[0].Rows[0]["username"].ToString();
Response.Redirect("Siteadmindata.aspx", false);
here linkviewmysite is the id of master page's HyperLink.
now problem is when i set NavigateUrl it's not work.
But when is remove this line (Response.Redirect("Siteadmindata.aspx", false);) from code than navigateurl set and HyperLink is work fine for me.
so now what should i do for it with Response.Redirect.
i got the answer
HyperLink hl = (HyperLink)this.Master.FindControl("linkviewmysite");
session["user_name"] = ds.Tables[0].Rows[0]["username"].ToString();
Response.Redirect("Siteadmindata.aspx", false);
now in master page page load
if(!postback)
{
if(session["user_name"]!=null)
{
linkviewmysite.navigationurl="../"+session["user_name"].tostring();
}
}