I'm running this codebehind with c# in Site.Master.cs:
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "text", "myFunction()", true);
That code fires this script in my Site.Maste.ASPX:
<script>
function myFunction() {
var x = document.getElementById("XXXX");
x.style.display = "none";
}
</script>
it supposed to hide this element:
<li ID="XXXX" runat="server" ClientIDMode="Static"><a runat="server" href="~/">Home</a></li>
I can see it hides for some miliseconds, but the page refresh inmediately and it appears again. How can the item could keep hidden after page refresh?
Edit: Thanks for your answer. After some hours of thinking and testing I got what I wanted with this: I took the < li > out of the LoggedInTemplate so I can reach it from C# (I was unable to see the control from C# if it was inside the LoggedInTemplat, I dont know why ), well, then I just set visible-false with C# in Masterpage:
protected void Page_Init(object sender, EventArgs e)
{
if ((System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
//MessageBox.Show("Autenticado");
Aprobacionesbtn.Visible = true; //button in the navbar
Reportesmenu.Visible = true; //dropdown menu in the navbar
}
else if ((System.Web.HttpContext.Current.User == null))
{
//MessageBox.Show("No autenticado");
Aprobacionesbtn.Visible = false; //button in the navbar
Reportesmenu.Visible = false; //dropdown menu in the navbar
}
}
Ok, we need some example code
user q:
Well, unless the controls are marked as runat = server, and furthermore, unless the controls in question have a "id", then we kind of missing two things we need here!!
So, say I have a menu bar (master page).
I want to hide (or show) somthing things, say maybe based on their logon rights (say what roles they belong to).
So, add some "id" and "runat" server tags to ANY of the buttons you want to hide/show/control.
So, you don't have to add "id" and runat="server" to "everything", but just those few controls (menu options) you need/want to control.
So, say this menu bar:
the first li (list item) is a menu bar - contractor. If the user logged on is NOT a contractor, then I hide that menu item.
the next item is a WHOLE drop down list. That drop down menu item is only for "site" admins.
So, code behind, on master page load is this:
so, based on users role membership, if they are a member of the SiteAdmin group, then visible = true
Same goes for contractors - again it is a rather simple matter to hide/show the menu item(s).
so, you WILL HAVE to persist these values, and set them EVERY TIME on page load in master.
And the reason is that when you use the master page menu bar, and navigate to a whole new page (url), then the page is 100% re-loaded, and thus any settings you make will be lost.