Possible to created a submenu programmatically in c#? - asp.net

On my page theres buttons on the top. I would like to add sub menus to those buttons:
protected void AddMenuItems()
{
// Check privledges and add links to admin pages accordingly.
// List adds pages in order of user precedence.
if (_cu.IsUser)
{
NavigationMenu.Items.Add(new MenuItem("Generate UserIDs", "Generate UserIDs", null, "~/GenPrefixList.aspx"));
}
}
Is there a way I can add a sub menu to the item?

Yes, each MenuItem has a ChildItems collection to which you can add an indefinite number of items.
protected void AddMenuItems()
{
// Check privledges and add links to admin pages accordingly.
// List adds pages in order of user precedence.
if (_cu.IsUser)
{
var newItem = new MenuItem("Generate UserIDs", "Generate UserIDs", null, "~/GenPrefixList.aspx");
var subItem = new MenuItem("Sub Menu Item", "Submenu Item", null, "page.aspx");
newItem.ChildItems.Add(subItem);
NavigationMenu.ChildItems.Add(newItem);
}
}
The "Sub Menu Item" will be a child of "Geneate UserIDs", which is a child of the navigation menu.
Update: The Items reference in your code should be ChildItems. Items is the collection used for the Windows application menu; you added the ASP.NET tag so I assume this is a web application.

Related

addEventListener to an element gets removed

I have an ASP.net webForms, using .NET 4.7.2 TreeView.
I'm using the TreeView component to render a webpage. this renders the tree as a series of tables for node labels and div elements containing child nodes.
I'm trying to add a click event to the node, which is an element located in the table.
The TreeNode has been overriden, to make use of RenderPreText, which adds an attribute role="treeitem" to each of these node's label.
ie
protected override void RenderPreText(HtmlTextWriter writer) {
writer.AddAttribute("role", "treeitem");
}
The following code is what I'm using to add the click event to each node's label element:
window.addEventListener("load", (event) => {
var treeItems = document.querySelectorAll('[role="treeitem"]');
for (var i = 0; i < treeItems.length; i++)
{
treeItems[i].addEventListener('click', OnNodeSelect);
}
});
function OnNodeSelect(event) { alert('Node Selected'); }
The issue i'm having, is that when the node is selected, the event fires and is then removed, so can only be clicked a single time.
What am I doing wrong, how do I add an event that doesn't get removed after single activation?

Removing Tabbar from tabbedpage leaves blank space. How to remove it?

I want to remove Tabbar from TabbedPage. I got it working but it leaves blank space or page height is not updated after hiding Tabbar.
Note that when we swipe through the pages, the blank goes and never comes back. This issue appears only for the first time.
I have tried from this link. But it doesn't work.
Also tried following
private void Element_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
try
{
TabBar.Hidden = true;
//TabBar.Bounds = new CoreGraphics.CGRect(View.Subviews[0].Frame.X, View.Subviews[0].Frame.Y,
// View.Subviews[0].Frame.Width, 0);
if (TabBar.Hidden)
{
// page
View.Subviews[0].Frame = new CoreGraphics.CGRect(0, 0, View.Subviews[1].Frame.Width, NativeView.Frame.Height);
// Tabbar
View.Subviews[1].Frame = new CoreGraphics.CGRect(View.Subviews[0].Frame.X, View.Subviews[0].Frame.Y,
View.Subviews[0].Frame.Width, 0);
}
else
{
View.Subviews[1].Frame = new CoreGraphics.CGRect(View.Subviews[1].Frame.X, View.Subviews[1].Frame.Y,
View.Subviews[1].Frame.Width, 49);
View.Subviews[0].Frame = new CoreGraphics.CGRect(View.Subviews[0].Frame.X, View.Subviews[0].Frame.Y,
View.Subviews[0].Frame.Width, View.Subviews[0].Frame.Height - 49);
}
//if (TabBar.Hidden)
// View.Subviews[1].Frame = new CoreGraphics.CGRect(View.Subviews[1].Frame.X, View.Subviews[1].Frame.Y, View.Subviews[1].Frame.Width, 0);
//else
// View.Subviews[1].Frame = new CoreGraphics.CGRect(View.Subviews[1].Frame.X, View.Subviews[1].Frame.Y, View.Subviews[1].Frame.Width, 49);
}
catch (Exception ex)
{
//TraceLog("Element_PropertyChanged" + ex.Message);
}
}
EDIT
I open tabbed page on list view item clicked. I allow to add multiple tabs dynamically. Also I have created custom tabbar using ContentView that I update when pages are added or removed from TabbedPage.
The sequence is :
- User opens first tab.
- Click on home icon given on tabbed page.
- open the second page by click on another list item
- swipe through the page and the page height will be normal.
Here is the code on ListView_ItemTapped (Not posted exactly how is it actually but you can get idea :) )
MultiTab ObjMultiTab = new MultiTab(); // Initialize tabbed page
// Get data from server
ObjMultiTab.Data = ObjData;
int Id = Convert.ToInt32(ObjData.id);
if (ActiveList.ContainsKey(Id)) // Dictionary that contains info about index and pageid that are already open
{
TabId = ActiveList[ObjData.id];
CurrentPage = Children[TabId]; // If user taps on already opend page
}
else
{
Count += 1; ActiveList.Add(Id, Count);
Children.Add(new SecondTabbedPage(TableData)); // Or add new child
CurrentPage = Children[Count];
}
await Application.Current.MainPage.Navigation.PushModalAsync(ObjMultiTab);
Is there any way to remove blank space for the first time also ?
Adding following function to the renderer removed blank space from TabbedPage.
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
TabBar.Hidden = true;
var page = View.Subviews[0];
var tabbar = View.Subviews[1];
tabbar.Bounds = CGRect.Empty;
page.Bounds = UIScreen.MainScreen.Bounds;
}
There is a solution that doesn't require any renders and works on both Android and iOS.
Wrap the TabbedPage in a NavigationPage so the structure of your app becomes
NavigationPage (root)
TappedPage
NavigationPage
ContentPage (with tabbar)
ContentPage (without tabbar)
On the TabbedPage you have to hide the navigationbar of the 'root' NavigationPage, otherwise you have 2 navbars.
<TabbedPage
...
HasNavigationBar="False">
If you push a page using the 'root' NavigationPage, the tabbar is hidden and there is no blank space at the bottom.
--- Edit ---
See my example at:
https://github.com/Jfcobuss/HideTabbarExample/tree/master/HideTabbarExample
Downside of this solution is
It is bit of an hacky workaround
The title next to back-button is the title of the TabbedPage, not the current tab
The animation to the next page is not as fluent as default

Hide siteMapNode when at particular view

I have siteMapNode inside SiteMap in Web.sitemap and want to hide one of the menu items, when I am at the page .../example.aspx?title=hiding
I tried to hide that menu from example.aspx.cs at Page_Load with smth like:
MenuItemCollection menuItems = Menu.Items;
MenuItem menuItem = new MenuItem();
foreach (MenuItem item in menuItems)
{
if (item.Text == "home")
menuItem = item;
}
menuItems.Remove(menuItem);
but I couldn't find needed item. Also I have tried to add logic into MenuTop_MenuItemDataBound, but it isn't executing at every url change

Dynamically Add/Remove Menu Items in ASP.NET?

I have a menu control (Menu1) and I want to add/remove items from the menu based on certain information I have stored about the authenticated user in the database. I'm not sure though how to access specific menu items from the menu control and remove them at runtime?
ASP.NET menus can be accessed via code behind. A menu declared in the markup, having the id 'Menu1' for example, could be accessed like so:
foreach (MenuItem item in Menu1.Items) {
if (item.NavigateUrl.Contains(pageName)) {
item.Selected = true;
item.Text = "customText";
}
// ...
}
In that example, the currently selected menu item is chosen according to the current page the menu is on. Just as well, the Items collection may be used to add or remove single menu items.
Note, on menu items, the ChildItems collection can be used to alter the submenu items collection.
More infos: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.items.aspx
#Edit: made it more coherent with the data in the question
I wish you was more specific but I know that the notification for comments kinda sucks here. so if you could tell me exactly
what is the information you'll Add/Remove items based on ?
how are they're stored in the database (which means are they are stored in one of the membership tables ? in the profile's table ? or a custom table where the user is referenced in ?
what's the technique you imagine in your mind in plain English ?
I'm not sure though how to access
specific menu items from the menu
control and remove them at runtime?
A. Remove items:
Menu1.Items.Remove(Menu1.FindItem("Jobs"))
B. Add Items:
Menu1.Items.Add(new MenuItem("News"))
OR use this to specify the required
properties for the new added item:
MenuItem item = new MenuItem()
item.NavigateUrl =""
item.Text = "Child Test"
Menu1.Items.Add(mnuTestChild)
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;
string selectCmd = "Select * from Pages where Status='"+1+"'";
SqlDataAdapter dap = new SqlDataAdapter(selectCmd,con);
DataSet ds = new DataSet();
dap.Fill(ds);
if (!Page.IsPostBack)
{
int x = 0;
string parent_id = "";
foreach (DataRow dr in ds.Tables[0].Rows)
{
parent_id = dr[0].ToString();
Menu1.Items.Add(new MenuItem(dr["Title"].ToString(), dr["URL"].ToString(), dr["ID"].ToString()));
}
x++;
}
}

jqGrid Toolbar CustomButton OnClick: How can I get the parent grid row id?

I am using the jqGrid for ASP.NET MVC, and have a grid with a subgrid. In that subgrid, I have added a button to the toolbar like so:
ToolBarSettings = new ToolBarSettings()
{
ShowRefreshButton = true,
CustomButtons = new List<JQGridToolBarButton>()
{
new JQGridToolBarButton()
{
Text = "Custom",
Position = ToolBarButtonPosition.Last,
OnClick="CustomClick" }
}
},
etc...
}
The CustomClick is a javascript callback, and it fires without any problems, but I am having trouble getting the parent grid row id in the CustomClick callback.
How can I get the parent row id in the CustomClick function?
Thanks, Dennis
The child Grid id itself contains the parentKey. when ever a child grid is created the id of the child grid is ParentGridName_ParentKey_ChildGridName. So you can get the Parent key
Below is the code for custom button :
<CustomButtons>
<Trirand:JQGridToolBarButton ToolTip="Custom button" OnClick="GetParentKey" />
</CustomButtons>
Then inside GetParentKey function you can get the parentKeyID as follows :
function GetParentKey()
{
var GridId = this.id.toString().split('_');
var parentKey = GridId[1];
}
Inside of CustomClick function you has as this the DOM element of the table from which navigator the custom button are clicked. There are no "parent row", but you can get the id of the currently selected row (if any exist) per
var rowid = $(this).jqGrid('getGridParam', 'selrow');
see example from the following answer oder search for another examples to the navButtonAdd method.

Resources