How to expand .NET TreeView node by clicking its text instead of +/- - asp.net

I've been using hardcoded hyperlinks for my web app navigation, but the app has grown since and managing it is becoming a real pain. I've decided to replace what I have with the TreeView control, however I want to make several changes to the way it looks.
Is there any property that needs to be set, that would allow user to expand the TreeView node by clicking its text instead of +/- ?
I've already set ShowExpandColapse to 'false'.
I want my final result to end up as something similar to the TreeView on the left of the MSDN site.
Could anyone point me at the right direction please?

Set TreeNode.SelectAction to either Expand, or SelectExpand.

you can use xml data source or direct binding from db to treview
in the TreeView DataBound event we can write d recursive function as below to fetch each node and assign expand action to them.
protected void TreeView1_DataBound(object sender, EventArgs e)
{
foreach (TreeNode node in TreeView1.Nodes)
{
node.SelectAction = TreeNodeSelectAction.Expand;
PrintNodesRecursive(node);
}
}
public void PrintNodesRecursive(TreeNode oParentNode)
{
// Start recursion on all subnodes.
foreach(TreeNode oSubNode in oParentNode.ChildNodes)
{
oSubNode.SelectAction = TreeNodeSelectAction.Expand;
PrintNodesRecursive(oSubNode);
}
}

I think you just have to do this in code: handle the Click event, determine the currently-selected tree node, and toggle its Expanded property (I think that's what it's called here).

You can do this only this way! http://geekswithblogs.net/rajiv/archive/2006/03/16/72575.aspx
With respect,
Alexander

Related

How to skip DRAG_DETECTED Events from TableHeaderRow (resizing TableColumns)

In order to use a TableView (with resizable columns) as drag source,
I have attached an onDragDetected handler on that TableView with the effect, that resizing TableColumns does not work anymore: If the user clicks into the TableHeaderRow for dragging the column separators, these mouse events are consumed by my handler too.
The handler is attached to the whole TableView and I do not see a way to distinguish between events from TableRows and events from the TableHeaderRow.
Attaching the handler to the data-rows only is not appropriate in my case, since I need multiline selection (and do not want to have dependencies from the RowFactory to the application data model).
Any suggestions?
best Hans
I found the solution myself: I had to look up the TableHeaderRow instance with the lookup-method, and discard the DRAG_DETECTED event there.
Pane header = (Pane) mainTableView.lookup("TableHeaderRow");
header.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
System.out.println("ignoring drag operation from table header.");
event.consume();
}
});
After that, resising of table columns works. Maybe there is something wrong with my drag+drop event handling that initially caused the problem!? Somebody voted down without comment...
The solution by Hans does work, but only after the window is shown. I added a window.setOnShown((WindowEvent e) listener and then I was able to do the lookup and header.setOnDragDetected.
Here is how I added it:
window.setOnShown((WindowEvent e) -> {
Pane header = (Pane) mainTableView.lookup("TableHeaderRow");
....
});
Otherwise, lookup returns null.

Disabling every input on a page programmatically in ASP.NET

I have tried searching around for this, but what I found was mainly for disabling a single input type.
What I want to do is disable every input type on a single page. Everything. Textboxes, checkboxes the whole lot.
I couldnt figure out how to modify the loops I found, which is why I am asking here, beacause it's likely one of you has a piece of code laying around that can do it.
Thank you in advance.
try below if you like to do it in javascript/jquery
$(document).ready(function(){
$('input').attr('disabled','disabled');
});
OR try below if you want in asp.net
ach control has child controls, so you'd need to use recursion to reach them all:
protected void DisableControls(Control parent, bool State) {
foreach(Control c in parent.Controls) {
if (c is DropDownList) {
((DropDownList)(c)).Enabled = State;
}
DisableControls(c, State);
}
}
Then call it like so:
protected void Event_Name(...) {
DisableControls(Page,false); // use whatever top-most control has all the dropdowns or just the page control
} // divs, tables etc. can be called through adding runat="server" property

verifying when a property is set for a custom control

I'm hoping there is a pattern for verifying when a property is set for a custom control in asp.net.
Considering that there is a page life cycle, we keep having issues where the control can get into an invalid state. The best thing we can do is raise an exception with an instructional message for things like, setting values selected before adding data.
Please note, ideally the component wouldn't rely on things like ordering of when a property is set. Unfortunately I can only move the company to better practices one step at a time. There are too many components to re-write from scratch and is an unrealistic expectation.
That said, here's an example.
We have two properties. SelectedValues which will set the values that match a comma separated list and InsertAll which will insert "All" at the top of a list.
Potential issue: The developer sets the SelectedValues in the Page's PreInit event, but the InsertAll property, if true, will add the "All" value and select it during the control's Init event. The trick is, SelectedValues will directly set the values when set, not later during the life cycle. Which means, when they see the page, they think there is a bug in the component because they didn't set All to be selected, but it is.
Page:
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
listBoxSelection.SelectedValues = "value1,value2";
listBoxSelection.InsertAll = true;
}
Control:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.InsertAll)
{
ListBoxItem allItem = new ListBoxItem()
{
Text = "All",
Value = "0"
Selected = true
};
this.Items.Insert(0, allItem);
}
}
How can I ensure that the developer knows they goofed when using this control? This is a simple example, but I see it all the time and in many forms.

'Databinding complete' event for Silverlight 4.0 DataGrid?

I have a DataGrid that I have bound to a property:
<cd:DataGrid
Name="myDataGrid"
ItemsSource="{Binding Mode=OneWay,Path=Thingies}"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
...
When the Thingies property changes, once all rows in the DataGrid have been populated with the new contents of Thingies, I want the DataGrid to scroll to the bottom row.
In WinForms, I would have done this by subscribing to the DataBindingComplete event. MSDN Forums contains several suggestions on how to do this with Silverlight 4.0 but they range from completely evil to just plain fugly:
start a 100ms timer on load, and scroll when it elapses
count rows as they're added, and scroll to the bottom when the number of added rows equals the number of entities in the data source
Is there an idiomatic, elegant way of doing what I want in Silverlight 4.0?
I stumbled upon this while searching for a resolution to the same problem. I was finding that when I attempted to scroll the selected item into view after filter and sort changes that I frequently received a run time error (index out of bounds). I knew instinctively that this was because the grid was not populated at that particular moment.
Aaron's suggestion worked for me. When the grid is defined, I add an event listener:
_TheGrid.LayoutUpdated += (sender, args) => TheGrid.ScrollIntoView(TheGrid.SelectedItem, TheGrid.CurrentColumn);
This solved my problem, and seems to silently exit when the parameters are null, too.
Why not derive from DataGrid and simply create your own ItemsSourceChanged event?
public class DataGridExtended : DataGrid
{
public delegate void ItemsSourceChangedHandler(object sender, EventArgs e);
public event ItemsSourceChangedHandler ItemSourceChanged;
public new System.Collections.IEnumerable ItemsSource
{
get { return base.ItemsSource; }
set
{
base.ItemsSource = value;
EventArgs e = new EventArgs();
OnItemsSourceChanged(e);
}
}
protected virtual void OnItemsSourceChanged(EventArgs e)
{
if (ItemSourceChanged != null)
ItemSourceChanged(this, e);
}
}
Use the ScrollIntoView method for achieving this.
myDataGrid.ItemSource = Thingies;
myDataGrid.UpdateLayout();
myDataGrid.ScrollIntoView(MyObservableCollection[MyObservableCollection.Count - 1], myDataGrid.Columns[1]);
You don't need to have any special event for this.
I think the nice way to do it, in xaml, is to have the binding NotifyOnTargetUpdated=true, and then you can hook the TargetUpdated to any event of your choice.
<ThisControl BindedProperty="{Binding xxx, NotifyOnTargetUpdated=true}"
TargetUpdated="BindingEndedHandler">

asp.net TreeView Expand question

hope you help me with I think a simple TreeView Expand problem.
I have a TreeView control in my MasterPage and my default depth is 2 and I see that when I click on the deeper node it keeps expanded.. But when I redirected into another page, the node collapsed.
I have a problem with my code which suppose to keep the node expanded.
TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
if (thisNode != null)
{
thisNode.Selected = true;
thisNode.Expand();
thisNode.Select();
thisNode.Expanded = true;
lbl.Text = "valupath: " + Session["SelectedCIDValPath"].ToString();
}
as you can see, I tried all the possible properties and methods to keep the deeper node expanded.. but it doesn't work.
Please help me? Thank you so much
It happens to be the case (and I find it just a bit frustrating) that expanding a node does not also cause parent nodes to expand. In order to ensure a node expands, it is necessary to also ensure that the parent nodes expand. I keep an extension method handy for this purpose:
public static void EnsureExpanded(this TreeNode node)
{
if (node != null)
{
EnsureExpanded(node.Parent);
node.Expand();
}
}
You can employ the extension like so:
TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
thisNode.EnsureExpanded();

Resources