I need to make some of the NavigationMenu items visible only to some users.
In my case I need to make 2 of them available only to admin.
I searched google and I found a command which works :
NavigationMenu.Items.Remove(NavigationMenu.FindItem("HERE GOES THE NAME OF THE MENU I NEED TO REMOVE"));
Till here,everything is ok.
But which command do I need to use to make it available again ?
Please help me. Thnx in advance ;)
public partial class SiteMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
NavigationMenu.Items.Remove(NavigationMenu.FindItem("Employers"));
if (Matrix.UserLoggedId == Guid.Empty)
{
hlLogin.Visible = true;
lblUsername.Visible = false;
lnkLogout.Visible = false;
}
else
{
// here goes the code to add the menu
hlLogin.Visible = false;
lblUsername.Visible = true;
lnkLogout.Visible = true;
Marin.Employee.DetailsDataTable emp = Matrix.GetEmployeeByUniqueId(Matrix.UserLoggedId);
if (emp.Rows.Count > 0)
{
lblUsername.Text = emp.Rows[0]["Firstname"].ToString();
bool isAdmin =Convert.ToBoolean(emp.Rows[0]["isAdmin"]);
if (isAdmin)
{
//here goes the code to show the menu
}
else
{
NavigationMenu.Items.Remove(NavigationMenu.FindItem("Employers"));
}
}
}
}
Try this, replace x with correct values.
var mi = new MenuItem(x,x,x,x)
NavigationMenu.Items.Add(mi);
For more info on MenuItem check this -
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitem.aspx
Related
I'm working on a simple multi-staged registration page for a site I'm building, and I give the user the choice of choosing programs/programming languages he knows using checkboxes:
but when I hit the "next" button, in order to go to the next stage, the checkbox I checked isn't set to true, but checkbox no. 18 is set to true(although I didn't check it)
I'm certain it has something to do with the stage before this one, in which I'm building dynamically radio buttons in which the user is choosing his profession (such as Artist, singer and etc').
there are 17 radio buttons, and they are somehow interfering with the next stage, in which the checkbox's checked values are only starting from checkbox no. 18 as I mentioned earlier.
here is some of the code:
else if (int.Parse(ViewState["DivID"].ToString()) == 2)
{
// save the Birthday Date, Language and country of the user.
ViewState["year"] = int.Parse(DropDownYear.SelectedValue);
ViewState["month"] = int.Parse(DropDownMonth.SelectedValue);
ViewState["day"] = int.Parse(DropDownDay.SelectedValue);
ViewState["Language"] = int.Parse(langDropDown.SelectedValue);
ViewState["Country"] = int.Parse(CountryDropDown.SelectedValue);
// ---------------------------------------------
// change from part 2 of the registration to part 3
registrationP2.Visible = false;
BindProfessions(radios, Page);
registrationP3.Visible = true;
radios.Visible = true;
}
else if (int.Parse(ViewState["DivID"].ToString()) == 3)
{
// change from part 3 of the registration to part 4
ViewState["Profid"] = CheckRadio(radios);
registrationP3.Visible = false;
BindKnowledge(CheckboxCon, Page);
registrationP4.Visible = true;
CheckboxCon.Visible = true;
// ---------------------------------------------
//next.Visible = true;
}
else if(int.Parse(ViewState["DivID"].ToString()) == 4)
{
List<int> v = GetCheckBox(CheckboxCon);
ViewState["Knowids"] = GetCheckBox(CheckboxCon);
}
Binding methods:
public static void BindProfessions(HtmlControl ctrl, Page thispage)
{
List<Profession> Plist = Profession.GetProfessionList();
foreach (Profession p in Plist)
{
HtmlInputRadioButton rd_button = new HtmlInputRadioButton();
const string GROUP_NAME = "Professions";
rd_button.Name = GROUP_NAME;
string LinkID = "P" + p.ProfessionID.ToString();
rd_button.Attributes["id"] = LinkID;
RegisterUserControl userprofession = (RegisterUserControl)thispage.LoadControl("~/RegisterUserControl.ascx");
userprofession.imgP = p.ProfPath;
userprofession.fieldName = p.ProfName;
userprofession.IDnum = p.ProfessionID;
userprofession.RadioName = LinkID;
userprofession.EnableViewState = false;
rd_button.EnableViewState = false;
ctrl.Controls.Add(rd_button);
rd_button.Value = p.ProfessionID.ToString();
ctrl.Controls.Add(userprofession);
}
}
public static void BindKnowledge(HtmlControl ctrl, Page thispage)
{
List<Knowledge> Plist = Knowledge.RetKnowledgeList();
foreach (Knowledge p in Plist)
{
HtmlInputCheckBox rd_button = new HtmlInputCheckBox();
const string GROUP_NAME = "knowledge";
rd_button.Name = GROUP_NAME;
string LinkID = "Know" + p.ProgramID.ToString();
rd_button.Attributes["id"] = LinkID;
rd_button.Value = p.ProgramID.ToString();
RegisterUserControl userprofession = (RegisterUserControl)thispage.LoadControl("~/RegisterUserControl.ascx");
userprofession.imgP = p.ProgPath;
userprofession.fieldName = p.PName;
userprofession.IDnum = p.ProgramID;
userprofession.RadioName = LinkID;
userprofession.EnableViewState = false;
rd_button.EnableViewState = false;
ctrl.Controls.Add(rd_button);
ctrl.Controls.Add(userprofession);
}
}
checking methods for both radios and checkbox :
public static int CheckRadio(HtmlControl ctrl)
{
try
{
int counter = 0;
int id = -1;
foreach (Control rdButton in ctrl.Controls)
{
if (rdButton is HtmlInputRadioButton)
{
HtmlInputRadioButton bu = (HtmlInputRadioButton)rdButton;
if (bu.Checked)
{
counter++;
id = int.Parse(bu.Value);
}
}
}
if (counter > 1)
{
return -1;
}
return id;
}
catch (Exception e)
{
return -1;
}
}
public static List<int> GetCheckBox(HtmlControl ctrl)
{
List<int> id_list = new List<int>();
foreach (Control rdButton in ctrl.Controls)
{
if (rdButton is HtmlInputCheckBox)
{
HtmlInputCheckBox bu = (HtmlInputCheckBox)rdButton;
if (bu.Checked)
{
id_list.Add(int.Parse(bu.Value));
}
}
}
return id_list;
}
}
when debugging you can see, that if I choose the first 3 professions, the values returned to me in the List<int> v are 18, 19, and 20
photo: debugging photo
I should mention that after I create the dynamic usercontrols and checkbox/radion buttons, I'm creating them again at postback in protected void Page_Load.
I'm stuck on this for days, and I don't know from where the problem emanates, is it because of ViewState, or the way I'm creating the controls... I really don't know.
Thanks in advance, Idan.
edit:
I played with it a bit, and have found out that when I disable the Binding of the professions which I have initiated earlier in Page_load it does work correctly, page load code look at the second if statement :
protected void Page_Load(object sender, EventArgs e)
{
IsPageRefresh = false;
if (!IsPostBack)
{
ViewState["DivID"] = 1;
ViewState["postids"] = System.Guid.NewGuid().ToString();
Session["postid"] = ViewState["postids"].ToString();
}
else
{
if (ViewState["postids"].ToString() != Session["postid"].ToString())
{
IsPageRefresh = true;
}
Session["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Session["postid"].ToString();
}
if (int.Parse(ViewState["DivID"].ToString()) == 3)
{
//BindProfessions(radios, Page);
}
else if(int.Parse(ViewState["DivID"].ToString()) == 4)
{
BindKnowledge(CheckboxCon, Page);
}
}
the problem is that I still need to initiate it again after hitting the button in order to get the checked value, how can I fix this thing, and why this is happening? your help would very much be appreciated.
The problem happens because the page recognize that I added 17 new checkbox's, and than when I go over them the first 17 are not checked until the 18'th(the first one of the ones that I checked) what ends up not checking the right checkbox....
And to make it clears I add the other radio buttons to a different div on the page, I don't know what is happening here
for anyone who has the same problem.
I ended up creating the object in Page_PreInit() and it solved the problem, it is recommended(by things I read) to create dynamic objects in Page_PreInit, before anything else is happening to the page.
protected void Page_PreInit(object sender, EventArgs e)
{
try
{
if (!IsPostBack && Session["DivID"] == null)
{
Session["DivID"] = 1;
}
if ((int)Session["DivID"] == 3)
{
InitBindProfessions(Page);
}
else if ((int)Session["DivID"] == 4)
{
InitBindKnowledge(Page);
}
}
catch
{
Response.Redirect("HomePage.aspx");
}
}
InitBindKnowledge and InitBindProfessions are just like BindKnowledge and BindProfession but without adding usercontrols to the control tree
i have a very large database of images from the web which i am categorizing (downloaded locally).
so i have a website (locally) to do this, but the db queries were taking long, so i got an idea to "preload" the next page, so that only the very first load of the page would be slow. I save the list of items loaded in a seperate thread in session. So far so good.
I wanted to optimize further, and did some testing on what took the longest, and loading the images to check the size to see if i needed to scale them (set image height and width on the img obj) - so i wanted to do this with a parallel.foreach loop - but after doing this, my buttons on the page stopped responding? i can see the page runs through the page_load event when i press a button, but it doesn't reach the buttons "code":
protected virtual void btnSaveFollowPosts_Click(object sender, EventArgs e)
{...}
any take on what i am doing wrong? i have tried to limit the degree of paralellelism to 1 just to see if that would fix it - but it did not.
Update - code:
trying to boil it down:
protected void Page_Load(object sender, EventArgs e)
{
Search(false);
}
protected void Search(bool updateCounters)
{
if (Session[SessionItems] == null)
{
if (Session[SessionItemsCache] == null)
{
//if is being constructed, wait, else construct
//if construction is not running
if (Session[SessionCacheConstructionRunning] == null)
{
StartPreLoadContent();
}
while (Session[SessionCacheConstructionRunning] != null)
{
Thread.Sleep(25); //block main thread untill items ready
}
}
List<ContentView> contentViewList = Session[SessionItemsCache] as List<ContentView>;
Session[SessionItemsCache] = null; //clean preload cache
Session[SessionItems] = contentViewList; //save in current usage storage
Filltable(ref tblContent, contentViewList);
//preload next batch
StartPreLoadContent();
}
else
{
List<ContentView> contentViewList = Session[SessionItems] as List<ContentView>; //get items from session
Session[SessionItems] = contentViewList; //save in current usage storage
Filltable(ref tblContent, contentViewList);
}
}
protected void StartPreLoadContent()
{
Session[SessionCacheConstructionRunning] = true;
//start task
Thread obj = new Thread(new ThreadStart(RunPreLoadContent));
obj.IsBackground = true;
obj.Start();
}
protected void RunPreLoadContent()
{
using (DBEntities entities = new DBEntities())
{
entities.CommandTimeout = 86400;
IQueryable<ContentView> query = entities.ContentView.Where(some criterias);
List<ContentView> contentViewListCache = query.ToList();
ParallelOptions options = new ParallelOptions();
options.MaxDegreeOfParallelism = 7;
Parallel.ForEach(contentViewListCache, options, content =>
{
try
{
Interlocked.Increment(ref imageSizeCount);
string path = Path.Combine(basePath, content.LocalPath);
int imageSize = 150;
using (System.Drawing.Image realImage = System.Drawing.Image.FromFile(path))
{
double scale = 0;
if (realImage.Height > realImage.Width)
{
scale = (double)realImage.Height / imageSize;
}
else
{
scale = (double)realImage.Width / imageSize;
}
if (scale > 1)
{
content.ImageHeight = (int)((double)realImage.Height / scale);
content.ImageWidth = (int)((double)realImage.Width / scale);
content.ImageScaled = true;
}
content.ShowImage = true;
}
}
catch (Exception)
{
}
});
Session[SessionItemsCache] = contentViewListCache;
Session[SessionCacheConstructionRunning] = null; //cache ready
}
protected virtual void btnSave_Click(object sender, EventArgs e)
{
try
{
//save
...some reading and saving going on here...
//update
Session[SessionItems] = null;
Search(true);
}
catch (Exception error)
{
ShowError(error);
}
}
I agree with a previous comment: you should probably do this logic earlier in the page lifecycle. Consider overriding OnInit and putting it there.
Also, you could try this line of code instead of your current thread code (which is more suited to Windows not Web programming):
using System.Threading.Tasks;
Task.Run(() => { RunPreLoadContent(); });
I'm very new to ASP.NET so there is a high risk that I am missing something simple here, but I have a problem with my Session. When I click the "NewTurn" button the first time, it creates the player just as it should, however, when I click it again, somehow all the stats are back as if it got created again (stats such as "health" is back to max value) but I can see in debug mode that it only enters the "if == null" function ONCE.
I don't understand what I am doing wrong, here is the code:
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
Session["PrevObject"] = player;
}
Character prevObj = (Character)Session["PrevObject"];
prevObj = CreateCard(prevObj);
Session["PrevObject"] = prevObj;
}
EDIT: I found the error and it had nothing to do with the Session, I just returned the object without setting the correct values before sending it back. Thank you for your suggestions, though and I'm sorry to have wasted your time!
please check comment in your code
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
Session["PrevObject"] = player;
}
Character prevObj = (Character)Session["PrevObject"];
// I think here you made mistake
prevObj = CreateCard(prevObj);
// check here again using DEbug point
// and make sure prevObj not null
Session["PrevObject"] = prevObj;
}
You should have to mention the Property of the player will be assign to the Session["PrevObject"].
suppose I have the class such as
public class Character
{
public playtime{get;set;}
Character(int i)
{
playtime=i;
}
}
Now I will code as you mention:
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
//Here is a little change you will have to specify what shuold be assign to the Session //variable
Session["PrevObject"] = player.playtime;
}
Hope this will help you.
Vote if you find helpful
Thanks is always appreciated.
I am using rad menu (telerik) in my website. the menu items are binding to menu in code behind. i take the navigate URL from database and the navigate URL will be sometimes a JavaScript like
js=OpenSupportWin();
Instead of page url.
So when I give this to navigateurl nothing is happening. please help me to solve this.
private void GenerateVerticalMenuTelerik(string pParentMenuId, RadMenuItem rmi)
{
List<DBMenus> ListVerticalMenus = new List<DBMenus>();
ListVerticalMenus = MenuGenerator.GetVerticalMainMenusFromNode(pParentMenuId, ClsLogin.Instance.IsAuthenticated());
if (ListVerticalMenus != null)
{
foreach (DBMenus asVertical in ListVerticalMenus)
{
try
{
bool IsSelectedVertical = (m_SelectedMenuPath.SelectedSubItemId == asVertical.SiteMapNodeId);
RadMenuItem rmItem = new RadMenuItem();
rmItem.Text = asVertical.WORDINGTextTitle;
rmItem.NavigateUrl = PII.ToolWeb.Tools.pkFunctions.GetPageLinkURL(asVertical.SiteMapNodeUrl);
rmi.Items.Add(rmItem);
}
catch (Exception ex)
{
}
}
}
}
string url_js = asVertical.SiteMapNodeUrl;
if (url_js.Contains("js"))
{
url_js = url_js.Replace("?js=", "");
url_js = url_js.Replace(";", "");
url_js = "javascript:" + url_js;
}
i wrote these lines of code and it works for me
Set your navigateUrl to something like this:
item.NavigateUrl = "javascript:OpenSupportWin();";
I am in need of some help. I am building a custom web user control which is going to be picking colours.
As of right now, it has two [asp:textbox]es on the page (foregroundColour and backgroundColour).
I want my foreground colour to default to "FFFFFF" and my background colour to default to "000000", AND I the user to be able to specify their own initial values though the tag.
<ucFontChooser id="testchooser" runat="server" foregroundcolor="AABBCC" />
I have two properties that are tied to the textboxes:
[DefaultValue("000000")]
public string ForegroundColor { get { return foregroundColorSelectorHex.Text; } set { foregroundColorSelectorHex.Text = value; } }
[DefaultValue("FFFFFF")]
public string BackgroundColor { get { return backgroundColorSelectorHex.Text; } set { backgroundColorSelectorHex.Text = value; } }
Please note that I am aware that the DefaultValue is only for the visual studio property window and doesn't actually set any value.
However, I have tried to set the default value by in the constructor of the usercontrol to do:
ForegroundColor = "000000"
This gives me an exception, because at the point of the constructor, the foregroundColorSelectorHex is null.
So if I try and setting the default in the OnInit event, or on *Page_Load* then it simply always uses the default I set and the set property value of "AABBCC" is overwritten.
What is the correct way to do what am I trying?
Thank you.
After much debugging, I came up with a solution to do exactly what I need.
public override string ID
{
set
{
base.ID = value;
InitializeProperties();
}
}
protected void InitializeProperties()
{
ForegroundColor = "000000";
BackgroundColor = "ffffff";
EnableBackgroundColor = false;
PrimaryFont = "Arial";
SecondaryFont = "Helvetica";
TertiaryFont = "sans-serif";
}
the .NET framework will call mycontrol.ID = "bla", which is when the user controls subcontrols come into context. It is called BEFORE the containing control/page reads the tags attributes and sets the properties.
Looks hacky, but works great.
I think a complete answer would be too long to answer here, and it's documented at MSDN already.
See this page: http://msdn.microsoft.com/en-us/library/ms972975.aspx
And search for the text "Adding Properties and Methods to a User Control"
(But read the whole article anyway so you understand the details.)
However, from the code in the article, it should you how to return a default value if the property is null. Assuming you have a property named CategoryID, the property should be defined as:
public int CategoryID
{
get
{
object o = ViewState["CategoryID"];
if (o == null)
return 0; // return a default value
else
return (int) o;
}
set
{
ViewState["CategoryID"] = value;
}
}
You can use the same pattern to define the color.
Try keeping the value of the property in the viewstate and then setting the .Text in the Page_PreRender, like so:
public string ForegroundColor
{
get
{
if (ViewState["forecolor"] == null)
{
ViewState["forecolor"] = "000000";
}
return (string) ViewState["forecolor"];
}
set
{
ViewState["forecolor"] = value;
}
}
protected override void OnInit(EventArgs e)
{
this.PreRender += Page_PreRender;
base.OnInit(e);
}
private void Page_PreRender(object sender, EventArgs e)
{
foregroundColorSelectorHex.Text = ForegroundColor;
}
Not sure this will work, but could you do something like this in your page_load:
protected void Page_Load {
if (String.IsNullOrEmpty(ForegroundColor))
ForegroundColor = "FFFFFF";
}
You should initialize the value when it is not postback i.e.
if(!Page.IsPostBack){
//Initialize your values
ForegroundColor = "FFFFFF";
}
Can't you create your foregroundColorSelectorHex and backgroundColorSelectorHex in the constructor ?
public string ForegroundColor {
get { return foregroundColorSelectorHex.Text; }
set { foregroundColorSelectorHex.Text = value; }
}
public string BackgroundColor {
get { return backgroundColorSelectorHex.Text; }
set { backgroundColorSelectorHex.Text = value; }
}
public FontChooser () {
foregroundColorSelectorHex = new ColorSelector();
backgroundColorSelectorHex = new ColorSelector();
ForegroundColor = "FFFFFF";
BackgroundColor = "000000";
}