DataRow[] drowpar = dt.Select("Parent_Id=" + 0);
foreach (DataRow dr in drowpar)
{
MenuItem objMenuItem = new MenuItem();
objMenuItem.Text = dr["Page_Name"].ToString();
objMenuItem.NavigateUrl = dr["Page_Url"].ToString();
MenuBar.Items.Add(objMenuItem);
}
foreach (DataRow dr in dt.Select("Parent_Id >" + 0))
{
MenuItem objMenuItem = new MenuItem();
objMenuItem.Text = dr["Page_Name"].ToString();
objMenuItem.NavigateUrl = dr["Page_Url"].ToString();
//MenuBar.FindItem(dr["Parent_Id"].ToString()).ChildItems.Add(objMenuItem);
MenuBar.FindItem(dr["Parent_Id"].ToString()).ChildItems.Add(objMenuItem);
//MenuBar.Items.Add(objMenuItem);
}
i am binding asp.net menu control using database and getting this below in childitem binding to menu
Object reference not set to an instance of an object.
MenuBar.FindItem(dr["Parent_Id"].ToString()).ChildItems.Add(objMenuItem);
That error message only happens for specific situations, and the possible reasons are:
objMenuItem is null - which obviously in your code it is not
dr["Parent_Id"] is null, and calling ToString() causes the error
MenuBar.FindItem(dr["Parent_Id"].ToString()) - this isn't finding the item with the given parent ID
The ChildItems collection on the menu item is null
If you want to be really sure where the object is not null, then you can do the following:
var id = dr["Parent_ID"].ToString();
var menuItem = MenuBar.FindItem(id);
if (menuItem != null)
menuItem.ChildItems.Add(objMenuItem);
Having the code together masks where the actual error occurs. Note, there is one other scenario where i didn't account for, which is the error is within the MenuBar itself, and it's masked. Without seeing the full stack trace, it's hard to tell. If you can post that, we can isolate it further.
Related
I have an MVC app with leaflet running in it, I am parsing xml data to get the paths to the leaflet tiles which I then display in a drop down via a ViewData["value"]. The problem is I can't seem to figure out how to get that selected value and pass it down to the leaflet js as a path and then display everything. I tried many different ways to get the selection data but I'm just hitting a wall again and again.
The below code is how I send it to the view. I display it via an #Html.DropDownList("layerType", ViewData["value"] as List)
string outputPath;
outputPath = ConfigurationManager.AppSettings.Get("outputPath");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(outputPath + #"\log.xml");
XmlNodeList layerType = xDoc.GetElementsByTagName("layerType");
XmlNodeList layerPath = xDoc.GetElementsByTagName("layerPath");
XDocument doc = XDocument.Load(outputPath + #"\log.xml");
var count = doc.Descendants("layers")
.Descendants("layerData")
.Count();
List<SelectListItem> li = new List<SelectListItem>();
foreach (int i in Enumerable.Range(0, count))
{
li.Add(new SelectListItem { Text = layerPath[i].InnerText, Value = i.ToString() });
}
ViewData["value"] = li;
return View(li);
How would I get the selected data and simply pass it down into the leaflet part inside the js tags.
Would I maybe pass it back into a controller and then back to the view?
#Html.DropDownList("layerType",
new SelectList((IEnumerable) ViewData["value"]), "Value", "Text")
In the Post method, you have to fill the viewdata again to avoid error if model returns invalid
Refer Below Link :
MVC Select List with Model at postback, how?
Working Code
https://dotnetfiddle.net/2lrb2S
Get Value on View
<script>
var conceptName = $('#nameofyourdropdowncontrol').find(":selected").text();
//code for passing value
</script>
Based on this Code I've build the basics of a function to give each user a predefined set of favorites.
However this particular code creates a new AOT element for each favorite of each user. I would like to configure the menu items manually and simply distribute them with the script.
How can I (in X++) get a reference to an existing \Menu Items\Display node and add that to an object of the Menu class?
This is what I have so far:
MyFavorites obj = new MyFavorites();
Menu menuNode;
MenuFunction mf;
TreeNode treeNode;
info("Applying favorites...");
menuNode = obj.getOrCreateRoot();
if (menuNode == null) return;
treeNode = menuNode.AOTfindChild("Administrator");
if (!treeNode)
{
menuNode.addSubmenu("Administrator");
treeNode = menuNode.AOTfindChild("Administrator");
info("Created submenu");
}
else info("Found submenu");
menuNode = treeNode;
// Here I need help. I don't want to recreate all these AOT nodes every time the script is run.
mf = new MenuFunction("Fav_AllUsers",MenuItemType::Display);
mf.AOTsave();
menuNode.addMenuitem(mf);
Here's a quick job I wrote that adds "SalesTable" menu to your favorites.
Also here's a link that shows how to copy favorites between users
A thing to note is the table SysPersonalization, which stores a blob of the data so you'll have to use either the object UserMenuList, Menu, or that table to accomplish what you want. This should get you started though.
static void Job4(Args _args)
{
TreeNode treeNode;
TreeNode menuToAdd = TreeNode::findNode(#"\Menu Items\Display\SalesTable");
TreeNodeIterator iterator;
UserMenuList userMenu;
Menu menuNode;
treeNode = infolog.userNode();
iterator = treeNode.AOTiterator();
treeNode = iterator.next();
if (treeNode)
{
userMenu = treeNode;
// find 'My Favorites' user menu;
treeNode = userMenu.AOTfindChild("#SYS95713");
// Note menuNode is a different object than userMenu
menuNode = treeNode;
menuNode.addMenuitem(menuToAdd);
}
}
I'm trying to create my first extension for visual studio and so far I've been following this tutorial to get me started (http://www.diaryofaninja.com/blog/2014/02/18/who-said-building-visual-studio-extensions-was-hard).
Now I have a custom menu item appearing when I click on a file in the solution explorer.
What I need now for my small project is to get the path of the file selected in the solution explorer but I can't understand how can I do that.
Any help?
---------------------------- EDIT ------------------------------
As matze said, the answer is in the link I posted. I just didn't notice it when I wrote it.
In the meanwhile I also found another possible answer in this thread: How to get the details of the selected item in solution explorer using vs package
where I found this code:
foreach (UIHierarchyItem selItem in selectedItems)
{
ProjectItem prjItem = selItem.Object as ProjectItem;
string filePath = prjItem.Properties.Item("FullPath").Value.ToString();
//System.Windows.Forms.MessageBox.Show(selItem.Name + filePath);
return filePath;
}
So, here are two ways to get the path to the selected file(s) :)
For future reference:
//In your async method load the DTE
var dte2 = await ServiceProvider.GetGlobalServiceAsync(typeof(SDTE)) as DTE2;
var selectedItems = dte2.SelectItems;
if(selectedItems.MultiSelect || selectedItems.Count > 1){ //Use either/or
for(short i = 1; i <= selectedItems.Count; i++){
//Get selected item
var selectedItem = selectedItems[i];
//Get associated project item (selectedItem.ProjectItem
//If selectedItem is a project, then selectedItem.ProjectItem will be null,
//and selectedItem.Project will not be null.
var projectItem = selectedItem.ProjectItem;
//Get project for ProjectItem
var project = projectItem.ContainingProject;
// Or get project object if selectedItem is a project
var sproject = selectedItem.Project;
//Is selectedItem a physical folder?
var isFolder = projectItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFolder;
//Else, get item's folder
var itemFolder = new FileInfo(projectItem.Properties.Item("FullPath").ToString()).Directory;
//Find config file
var configFiles itemFolder.GetFiles("web.config");
var configfile = configFiles.length > 0 ? configFiles[0] : null;
//Turn config file into ProjectItem object
var configItem = dte2.solution.FindProjectItem(configFile.FullName);
}
}
I hope someone finds this helpful...
The article you mentioned already contains a solution for that.
Look for the menuCommand_BeforeQueryStatus method in the sample code. It uses the IsSingleProjectItemSelection method to obtain an IVsHierarchy object representing the project as well as the id of the selected item. It seems that you can safely cast the hierarchy to IVsProject and use it´s GetMkDocument function to query the item´s fullpath...
IVsHierarchy hierarchy = null;
uint itemid = VSConstants.VSITEMID_NIL;
if (IsSingleProjectItemSelection(out hierarchy, out itemid))
{
IVsProject project;
if ((project = hierarchy as IVsProject) != null)
{
string itemFullPath = null;
project.GetMkDocument(itemid, out itemFullPath);
}
}
I don´t want to copy the entire code from the article into this answer, but it might be of interest how the IsSingleProjectItemSelection function obtains the selected item; so I just add some notes instead which may guide into the right direction... The method uses the GetCurrentSelection method of the global IVsMonitorSelection service to query to the current selected item.
I have a form which, based on the answers given in the prior page, can have about 10 different variations in the combination of fields (most are the same, but several change). I decided rather than making 10 separate pages, I would try to make it dynamic. Eventually this will pull the form setup from a database, but for now I'm just trying to get the dynamic part to work. The following code kinda works, but it's giving me a weird result.
private void AddTestControls()
{
var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);
var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);
newbox.ID = "SecondBox";
newbox.Text = "This is also dynamic";
newbox.CssClass = "step";
DynamicDiv1.Controls.Add(newbox);
}
I've stepped through it and all the properties are getting set correctly, but when the page finally renders, only the SecondBox control is visible. There is no trace of the FirstBox. If I change it so that SecondBox is its own object (newebox2 for example) then both are visible, but with how I was thinking that I would ultimately do the form from the database, this could complicate things. I don't understand why the textbox object has to be recreated in order to add it to the Div's collection of controls. Am I going about this all wrong, or just missing a step somewhere?
Your "SecondBox" are overwriting the "FirstBox" newbox since it's still holding a reference to it. Create a new TextBox for the second box:
var newbox = new TextBox();
newbox.ID = "FirstBox";
newbox.Text = "This is dynamic";
newbox.CssClass = "stepHeader";
DynamicDiv1.Controls.Add(newbox);
var newlit = new Literal();
newlit.ID = "FirstLit";
newlit.Text = ".<br/>.";
DynamicDiv1.Controls.Add(newlit);
// Create a new TextBox
var secondBox = new TextBox();
secondBox.ID = "SecondBox";
secondBox.Text = "This is also dynamic";
secondBox.CssClass = "step";
DynamicDiv1.Controls.Add(secondBox);
I'm not quite sure why this could complicate things, but what you could do is create a method for creating a textbox, if that's easier:
TextBox CreateTextBox(string id, string text, string cssClass)
{
var box = new TextBox();
box.ID = id;
box.Text = text;
box.CssClass = cssClass;
return box;
}
And then
var newBox = CreateTextBox("FirstBox", "This is dynamic", "stepHeader");
DynamicDiv1.Controls.Add(newBox);
What's how it suppose to work. newbox1 is a reference so after the first time it's added to DynamicDiv1, it's there and if you change its Text, then the Text will be changed. You may find this SO useful. This SO demostrates the same issue you are having.
I have this code :
foreach (MyObject object in MyObject)
{
Literal checkBoxStart = new Literal();
Literal checkBoxEnd = new Literal();
checkBoxStart.Text = "<div class=\"item\">";
checkBoxEnd.Text = "</div>";
CheckBox chb = new CheckBox();
chb.InputAttributes.Add("value", object.UniqueID);
chb.Text = object.Title;
panelLocalita.Controls.Add(checkBoxStart);
panelLocalita.Controls.Add(chb);
panelLocalita.Controls.Add(checkBoxEnd);
}
than, on cmdSend_Click(object sender, EventArgs e) method, I'd like to browse the panel, and check only Checkboxs. How can I do it?
Assuming I understand what you mean by browse, you could do something like:
foreach (CheckBox chb in panelLocalita.Controls.OfType<CheckBox>())
{
}
You must have a using System.Linq to make this work.
You can dig into the Linq tool box. There's a few methods that work on IEnumerable and since ControlsCollection implements that interface you can use them directly on the collection. One of the methods suits your needs very nicely.
The extension method OfType<TResult>() will iterate the collection and only return those elements that is of the provided type.
to get all the checkboxes you could do as follows:
var checkboxes = panelLocalita.Controls.OfType<CheckBox>();
and you can then either iterate via a foreach if you wish for side effects such as setting all to checked
foreach(var checkBox in checkboxes)
{
checkBox.Checked = true;
}
or if you need to grab information from them you can use more tools from the Linq tool box such as if you wanted to only grab those that are checked:
checkboxes.Where(c=>c.Checked)
or to check that all are checked
checkboxes.All(c=>c.Checked)