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();
}
}
Related
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 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 had written code to Enable and disable hyperlinks on master page from the content page. Every thing is working fine and the hyperlinks are getting disabled also after calling the DisableHyperlinkInMasterPage Method. When i am calling the EnableHyperlinkInMasterPage method still the hyperlinks are not working. If we are leaving that page , then i found that the hyperlinks starts working. My issue is after calling the EnableHyperlinkInMasterPage method , without leaving the page the hyperlinks are still disabled and not working and after leaving the page it getting enabled . Please help me that how i fix this issue.I debug the code and not found any error on EnableHyperlinkInMasterPage method.
private void DisableHyperlinkInMasterPage()
{
AssociateMaster mymaster = (AssociateMaster)Page.Master;
HyperLink home = (HyperLink)mymaster.FindControl("Home");
home.Enabled = false;
HyperLink profile = (HyperLink)mymaster.FindControl("ProfileLink");
profile.Enabled = false;
HyperLink report = (HyperLink)mymaster.FindControl("Report");
report.Enabled = false;
HyperLink signout = (HyperLink)mymaster.FindControl("SignOut");
signout.Enabled = false;
}
private void EnableHyperlinkInMasterPage()
{
AssociateMaster mymaster = (AssociateMaster)Page.Master;
HyperLink home = (HyperLink)mymaster.FindControl("Home");
home.Enabled = true;
HyperLink profile = (HyperLink)mymaster.FindControl("ProfileLink");
profile.Enabled = true;
HyperLink report = (HyperLink)mymaster.FindControl("Report");
report.Enabled = true;
HyperLink signout = (HyperLink)mymaster.FindControl("SignOut");
signout.Enabled = true;
}
Try using properties to enable and disable links. And try using FindControl as less as you can. Consider you have a link in your MasterPage named lnkTest. Write this code in your master page:
public bool TestLinkEnabled
{
get { return lnkTest.Enabled; }
set {lnkTest.Enabled = value; }
}
Then in your content page after adding this line to your page:
<%# MasterType VirtualPath="~/MasterPage.master" %>
enable and disable this link using:
Master.TestLinkEnabled = true;
Master.TestLinkEnabled = false;
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;
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.