Hide <li> asp.net items depending on user privileges - c#

I have to hide column 4 when specific users log in to a website. This is my current code for asp and c#:
protected void Page_Load(object sender, EventArgs e)
{
((LoggedIn)Page.Master).CurrentPage = LoggedIn.LoggedInMenuPages.1;
if (CurrentCustomer.AdminRole==false)
{
******NEED TO HIDE ITEM 4
}
}

Put runat="server" to the elements and also specific ID, after that go in Page_Load write the ControlID.Visible=false
ASPX
<li id="AdminElement" runat="server">
//other tags
</li>
Page_Load
AdminElement.Visible = CurrentCustomer.AdminRole;
//remove the class attribute from the aspx
AdminElement.Attributes["class"] = GetCurrentPageStyle("4");

Related

add multiple innerhtml in asp.net

my x.aspx file :-
<form id ="Content3ret" runat="server">
</form>
<asp:Button ID="Button1" runat="server" OnClick ="Button1_Click"/>
my x.aspx.cs file :-
protected void Button1_Click(object sender, EventArgs e)
{
var x = Guid.NewGuid().ToString();
Content3ret.innerhtml = "<table id = '"+x+"'> <tr> <td><input type='radio' name='radiotest' checked='checked'/></td> </tr> </table>";
}
what I am trying to do is :-
when I click button each and every time there should be new radio button added with other .
like 3 button click I need 3 radio button .
but with this code when I click only one radio button creating .
like 3 click only one radio button with new id.
can anyone help me ?
When you add data on Content3ret.innerhtml and render them on the pages, this data are lost / gone / stay on page and never come back on post back - because this inner html is not post back with the second click on button.
So you have to render them on page, and at the same time saved it somewhere - preferable on viewstate because of asp.net
so the page will be like
<form id="form1" runat="server">
<div runat="server" id="divPlaceOnme"></div>
<asp:Button ID="Button1" runat="server" Text="ok" OnClick ="Button1_Click"/>
</form>
and on code behind we have
const string InnerHtmlKeeper_name = "InnerHtmlKeeper_cnst";
string InnerHtmlKeeper
{
get
{
var RetMe = ViewState[InnerHtmlKeeper_name] as string;
if (string.IsNullOrEmpty(RetMe))
return string.Empty;
else
return RetMe;
}
set
{
ViewState[InnerHtmlKeeper_name] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
var x = Guid.NewGuid().ToString();
// we keep the new with the previous data on this ViewState
InnerHtmlKeeper += "<table id = '" + x + "'> <tr> <td><input type='radio' name='radiotest' checked='checked'/></td> </tr> </table>";
// we add the final render on the div on page
divPlaceOnme.InnerHtml = InnerHtmlKeeper;
}
Last words.
Your approaches have many issues - and your code not working as it is. The button must be on the form, and you have to render inside some other div.
The point here is to show you have to save some actions on viewstate - beside that for a good solution on your problem you must use some javascript to render the radio without post back.

Multiview controls

I wrote the following code for multiview where I am using gridview and datalist:
<ContentPlaceHolderID="ContentPlaceHolder1">
<div class="alert alert-success" >
<div class="divbtn" style="text-align:right">
<asp:LinkButton ID="gridbtn" runat="server" CssClass="glyphicon glyphicon-th" OnClick="gridbtn_Click"></asp:LinkButton>
<asp:LinkButton ID="listbtn" runat="server" CssClass="glyphicon glyphicon-th-list" OnClick="listbtn_Click"></asp:LinkButton>
</div>
</div>
<asp:MultiView runat="server" ID="multiview" ActiveViewIndex="0">
<asp:View runat="server" ID="gridview">
<uc1:GridControl runat="server" ID="GridControl"/>
</asp:View>
<asp:View runat="server" ID="listview">
<uc1:Listing runat="server" ID="Listing" />
</asp:View>
</asp:MultiView>
</asp:Content>
I am using two link buttons to call their respective views by firing two separate events as follows.
protected void listbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 1;
}
protected void gridbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 0;
}
Suppose, my datalist (Index=1) is active on my page and if there is a post back it should still be showing the datalist but on postback it automatically switches back to grid view (Index=0). I really need help with this!
You can save the index to a session variable and then read it back on post back like this:
To save:
Session["index"] = index.ToString();
Read it on page load like this:
Index = Session["index"];
You will need the session variable to maintain state per user session. If you want to maintain state for the application then you have to use the application variable.
Hi add the following code in your page_load event.
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
i think you are setting the multiview's active index to zero on every post back like follows
protected void Page_Load(object sender, EventArgs e)
{
multiview.ActiveViewIndex=0;
}
this will cause the multiview to set active index as 0 on every post back.To avoid this you have to set it as follows
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
}

Creating buttons/href from a list of names on the server side with javascript?

I have a list of button names and uri's containing links to other pages in my website. This is stored in my database. I also have a javascript function that can create buttons dynamically preferably when the page loads. Now what I need is on the server side to iterate through the list of button names and create a button for every button in the list?
Is this possible. I was thinking of maybe using the string builder to build some html to create the buttons or the method I like more is to call the javascript everytime I need a new button.
Here is my javascript function:
function createHref(Name, Uri) {
var leftDiv = document.createElement("div"); //Create left div
leftDiv.id = "left"; //Assign div id
leftDiv.setAttribute("style", "float:left; width:66.5%; line-height: 26px; text-align:left; font-size:12pt; padding-left:8px; height:26px;"); //Set div attributes
leftDiv.style.background = "#FF0000";
a = document.createElement('a');
a.href = Uri;
a.innerHTML = Name
leftDiv.appendChild(a);
document.body.appendChild(leftDiv);
}
Since this is ASP.NET I would use a Repeater and set the datasource to the button data you pull from your database.
For examples sake, I will assume you have a custom object MyButton that maps to your data:
public class MyButton
{
public MyButton()
{}
public string Name { get; set; }
public string Href { get; set; }
}
You would then have some markup for the repeater:
<asp:Repeater ID="rptMyButtons" runat="server" OnItemDataBound="rptMyButtons_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="hypUrl" runat="server" />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
You would then bind your data to the repeater and set some properties on the objects:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
rptMyButtons.DataSource = //your data from the database
rptMyButtons.DataBind();
}
}
protected void rptMyButtons_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
MyButton button = (MyButton)e.Item.DataItem;
HyperLink hypUrl = (HyperLink)e.Item.FindControl("hypUrl");
hypUrl.Text = button.Name;
hypUrl.NavigateUrl = button.Href;
}
}
It seems like you may be trying to build a sitemap?
I have a list of button names and uri's containing links to other pages in my website
Which is why I have used a <ul> and <li>'s to contain your buttons in a list which makes more sense. It is quite easy to change this back to a <div> however.

Change div things in other page

I used the code below to make a change in div of the page of this code file:
HtmlGenericControl divTest = Page.FindControl("test") as HtmlGenericControl;
divTeste.InnerText = "tested";
But now, I want to change another page. Ex.: default.aspx code behind changes a div inner text in the test.aspx ...
The code above isn't appropriated. I want permanent changes. What code should be?
How can I do it?
You need to understand how ASP.NET works. Introduction to ASP.NET
If you are on Default.aspx and then wont to move to Test.aspx you can use values from Default.aspx in Test.aspx however you can't just change different pages from the page you are on. (Actually it is possible by writing data to a database and then later accessing that data on the page you want to change but for your example I don't believe you are looking for something like that)
The closest to your example I can think of is using a Query string.
Default.aspx Markup
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Default.aspx Code behind
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Test.aspx?DivMSG=Hello");
}
Test.aspx Markup
<div id="MyTestDiv" runat="server">
</div>
Test.aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
MyTestDiv.InnerText = Request.QueryString["DivMSG"];
}
}
Update:
One way to make it permanent is to write it to a database. A quick example but you need to do the database code
Default.aspx Markup
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Default.aspx Code behind
protected void Button1_Click(object sender, EventArgs e)
{
string messageToWriteToTestDiv = TextBox1.Text;
// Code: to write messageToWriteToTestDiv to the database
}
Test.aspx Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string myMessage = "";
// Code: string myMessage = Get Message from Database to write to div
MyTestDiv.InnerText = myMessage;
}
}
Test.aspx Markup
<div id="MyTestDiv" runat="server">
</div>

Highlighting Menu Bar in ASP.NET Web Application

I have a Visual Studio 2010 and when I added a NEW project, VS gives me default css and UI themes. I have 6 aspx pages. Now whenever user is going into some specific page, I want the menu bar of that page to get highlighted so that user will know which page he/she is.
I have a page called: CommSetup.aspx. In the page_load I have written this code:
And in the master page I changed this:
In code behind:
protected void Page_Load(object sender, EventArgs e)
{
foreach (MenuItem item in NavigationMenu.Items)
{
var navigateUrlParams = item.NavigateUrl.Split('/');
if (Request.Url.AbsoluteUri.IndexOf(navigateUrlParams[navigateUrlParams.Length - 1]) != -1)
{
item.Selected = true;
}
}
}
Markup:
<body runat ="server" clientidmode ="Static" id = "MasterBody">
<form runat="server">......
<asp:Menu ID="NavigationMenu" runat="server" StaticSelectedStyle-CssClass ="Selected" CssClass="menu" .....
This is what I added in Site.css:
div.menu ul li a.Selected
{
background-color: #bfcbd6;
color: #465c71;
text-decoration: none;
}
Here's an example of doing exactly what you're asking for using CSS:
http://hicksdesign.co.uk/journal/highlighting-current-page-with-css
You should be able to achieve that using static ID naming on your controls (instead of letting ASP.NET assign the ID value to each control).
EDIT:
To get this to work with a master page, change the <body> tag in your master page to:
<body runat="server" clientidmode="Static" id="MasterBody">
Then in the Page_Load of each page, you can overwrite the id for each page (the master page in my example is of type SiteMaster):
protected void Page_Load(object sender, EventArgs e)
{
Control c = Page.Master.FindControl("MasterBody");
if (c != null)
{
c.ID = "Page1";
}
}
Update (2):
I tried running Farzin's example and it didn't seem to work, so here is what I was able to verify worked for me (you won't need the Page_Load from before in your content pages):
Site.master
<asp:Menu ID="NavigationMenu" ...>
<StaticSelectedStyle CssClass="selected" />
...
</asp:Menu>
Site.master.cs
protected void Page_Load(object sender, EventArgs e)
{
foreach (MenuItem item in NavigationMenu.Items)
{
item.Selected = Page.ResolveUrl(item.NavigateUrl).ToLowerInvariant() == Request.Path.ToLowerInvariant();
}
}
Styles/Site.css
div.menu ul li a.selected
{
/* put your style definition here */
}
You should parse current url in Master page (code behind) and assign different css clss to menu item that belongs to current url.
foreach (MenuItem item in NavigationMenu.Items)
{
var navigateUrlParams = item.NavigateUrl.Split('/');
if(Request.Url.AbsoluteUri.IndexOf(navigateUrlParams[navigateUrlParams.Length - 1]) != -1)
{
item.Selected = true;
}
}
just put this code in Form_Load event of your main master page (code behind)
You could use the ASP.NET Site Navigation functionality. Scott Mitchell has put out one of the definitive articles on it here.

Categories

Resources