📄 treeview.cs
字号:
return new XmlTextReader(stream);
}
//
// ReadTreeNodeTypeXmlSrc()
//
// Reads the XML file specified in TreeNodeTypeSrc and creates TreeNodeTypes accordingly. The
// file is assumed to be valid XML with a <TREENODETYPES> outer container and <TREENODETYPE>
// inner containers.
//
internal void ReadTreeNodeTypeXmlSrc()
{
TreeView tv = ReadXmlSrc(TreeNodeTypeSrc, String.Empty, "TREENODETYPES");
if (tv != null)
{
TreeNodeTypeCollection newTypes = new TreeNodeTypeCollection(this);
((IStateManager)newTypes).TrackViewState();
newTypes.Clear();
if (tv.TreeNodeTypes != null)
{
for (int i = 0; i < tv.TreeNodeTypes.Count; i++)
{
newTypes.Add(tv.TreeNodeTypes[i]);
tv.TreeNodeTypes[i].SetViewStateDirty();
}
}
_TreeNodeTypes = newTypes;
}
}
/// <summary>
/// Raises the PreRender event.
/// </summary>
/// <param name="e">The event data.</param>
protected override void OnPreRender(EventArgs e)
{
if ((SelectedNodeIndex == "" || SelectedNodeIndex == String.Empty) && Nodes.Count > 0)
SelectedNodeIndex = "0";
else
SelectedNodeIndex = SelectedNodeIndex; // verify current index
// We only check HoverNodeIndex for validity here, because it's possible to have had many events
// stacked up on the client if AutoPostBack=false. The HoverNodeIndex might not validate until
// after these events are processed, so we have to wait until now to check.
if (!IsValidIndex(HoverNodeIndex))
throw new Exception(String.Format(Util.GetStringResource("TreeInvalidIndexFormat"), HoverNodeIndex));
base.OnPreRender(e);
}
/// <summary>
/// Renders the uplevel version of the TreeView.
/// </summary>
/// <param name="output">The HtmlTextWriter that will receive the output.</param>
protected override void RenderUpLevelPath(HtmlTextWriter output)
{
// render uplevel
output.Write("<?XML:NAMESPACE PREFIX=TVNS />\n<?IMPORT NAMESPACE=TVNS IMPLEMENTATION=\""
+ AddPathToFilename("treeview.htc") + "\" />");
output.WriteLine();
AddAttributesToRender(output);
string cssText = DefaultStyle.CssText;
if (cssText != String.Empty)
output.AddAttribute("defaultStyle", cssText);
cssText = HoverStyle.CssText;
if (cssText != String.Empty)
output.AddAttribute("hoverStyle", cssText);
cssText = SelectedStyle.CssText;
if (cssText != String.Empty)
output.AddAttribute("selectedStyle", cssText);
if (ImageUrl != String.Empty)
output.AddAttribute("imageUrl", ImageUrl);
if (SelectedImageUrl != String.Empty)
output.AddAttribute("selectedImageUrl", SelectedImageUrl);
if (ExpandedImageUrl != String.Empty)
output.AddAttribute("expandedImageUrl", ExpandedImageUrl);
if (ChildType != String.Empty)
output.AddAttribute("childType", ChildType);
if (Target != String.Empty)
output.AddAttribute("target", Target);
output.AddAttribute("selectedNodeIndex", SelectedNodeIndex);
output.AddAttribute("HelperID", HelperID);
output.AddAttribute("systemImagesPath", SystemImagesPath);
if (HoverNodeIndex != String.Empty)
output.AddAttribute("HoverNodeIndex", HoverNodeIndex);
if (ShowLines == false)
{
output.AddAttribute("showLines", "false");
output.AddAttribute("indent", Indent.ToString());
}
if (ShowPlus == false)
output.AddAttribute("showPlus", "false");
if (ShowToolTip == false)
output.AddAttribute("showToolTip", "false");
if (SelectExpands == true)
output.AddAttribute("selectExpands", "true");
if (AutoSelect == true)
output.AddAttribute("autoSelect", "true");
if (_bFocused)
output.AddAttribute("Focused", "true");
if (_scrollTop >= 0)
{
output.AddAttribute("__scrollTop", _scrollTop.ToString());
}
if (_scrollLeft >= 0)
{
output.AddAttribute("__scrollLeft", _scrollLeft.ToString());
}
if (_bFocused)
{
if (_parentTop >= 0)
{
output.AddAttribute("__parentTop", _parentTop.ToString());
}
if (_parentLeft >= 0)
{
output.AddAttribute("__parentLeft", _parentLeft.ToString());
}
}
if (Page != null)
{
output.AddAttribute("onexpand", "javascript:" + " if (this.clickedNodeIndex != null) this.queueEvent('onexpand', this.clickedNodeIndex)");
output.AddAttribute("oncollapse", "javascript:" + " if (this.clickedNodeIndex != null) this.queueEvent('oncollapse', this.clickedNodeIndex)");
output.AddAttribute("oncheck", "javascript:" + " if (this.clickedNodeIndex != null) this.queueEvent('oncheck', this.clickedNodeIndex)");
output.AddAttribute("onselectedindexchange", "javascript:" + " if (event.oldTreeNodeIndex != event.newTreeNodeIndex) this.queueEvent('onselectedindexchange', event.oldTreeNodeIndex + ',' + event.newTreeNodeIndex)");
if (AutoPostBack == true)
{
string str = Page.GetPostBackEventReference(this, "");
str = str.Replace("'", "\\'");
str = "javascript: window.setTimeout('" + str + "', 0, 'JavaScript')";
output.AddAttribute("onfirequeuedevents", str);
}
}
output.RenderBeginTag("tvns:treeview");
base.RenderUpLevelPath(output);
output.RenderEndTag();
}
/// <summary>
/// Renders the downlevel version of the TreeView.
/// </summary>
/// <param name="output">The HtmlTextWriter that will receive the output.</param>
protected override void RenderDownLevelPath(HtmlTextWriter output)
{
HtmlTextWriter newWriter = new HtmlTextWriter(output);
ControlStyle.AddAttributesToRender(newWriter);
AddAttributesToRender(newWriter);
newWriter.RenderBeginTag("DIV");
output.AddAttribute("CELLSPACING", "0");
output.AddAttribute("CELLPADDING", "0");
output.AddAttribute("BORDER", "0");
// AddAttributesToRender(output);
output.RenderBeginTag("TABLE");
base.RenderDownLevelPath(output);
output.RenderEndTag();
newWriter.RenderEndTag();
}
/// <summary>
/// Renders the TreeView in the designer (the downlevel version).
/// </summary>
/// <param name="output">The HtmlTextWriter that will receive the output.</param>
protected override void RenderDesignerPath(HtmlTextWriter output)
{
RenderDownLevelPath(output);
}
private void ReadTreeNodeXmlSrc()
{
if (TreeNodeSrc != String.Empty)
{
TreeView tv = ReadXmlSrc(TreeNodeSrc, TreeNodeXsltSrc, "TREENODES");
if (tv != null)
{
TreeNodeCollection col = tv.Nodes;
CopyXmlNodesIntoTree(col, this);
}
}
}
//
// CopyXmlNodesIntoTree
//
// To make sure databound nodes are recreated on postback with redatabinding, the
// TreeNodeCollections they're in must be recreated dynamically so the actions of
// adding each node can be stored.
//
internal void CopyXmlNodesIntoTree(TreeNodeCollection col, Object dest)
{
TreeNodeCollection destNodes = (dest is TreeView ? ((TreeView)dest)._Nodes : ((TreeNode)dest)._Nodes);
destNodes.Clear();
if (col != null)
{
while (col.Count > 0)
{
TreeNode node = col[0];
col.RemoveAt(0);
destNodes.Add(node);
}
}
destNodes.SetViewStateDirty();
}
private TreeNodeCollection GetNodeCollection(Object obj)
{
if (obj is TreeView)
return ((TreeView)obj).Nodes;
else if (obj is TreeNode)
return ((TreeNode)obj).Nodes;
else
throw new Exception(Util.GetStringResource("TreeInvalidObject"));
}
internal object GetStateVar(String att)
{
if (att.EndsWith("Style"))
return typeof(TreeView).InvokeMember (att, BindingFlags.Default | BindingFlags.GetProperty, null, this, new object [] {});
else
return ViewState[att];
}
/// <summary>
/// Renders the contents of the TreeView.
/// </summary>
/// <param name="output">The HtmlTextWriter that will receive the output.</param>
protected override void RenderContents(HtmlTextWriter output)
{
if (IsUpLevelBrowser)
{
foreach (TreeNodeType tnt in TreeNodeTypes)
{
tnt.Render(output, RenderPathID.UpLevelPath);
}
}
foreach (TreeNode node in Nodes)
{
node.Render(output, RenderPath);
}
}
/// <summary>
/// Loads the control's previously saved view state.
/// </summary>
/// <param name="savedState">An object that contains the saved view state values for the control.</param>
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] state = (object[])savedState;
base.LoadViewState(state[0]);
((IStateManager)DefaultStyle).LoadViewState(state[1]);
((IStateManager)HoverStyle).LoadViewState(state[2]);
((IStateManager)SelectedStyle).LoadViewState(state[3]);
((IStateManager)TreeNodeTypes).LoadViewState(state[4]);
((IStateManager)Nodes).LoadViewState(state[5]);
}
_bCreated = true;
}
/// <summary>
/// Saves the changes to the control's view state to an Object.
/// </summary>
/// <returns>The object that contains the view state changes.</returns>
protected override object SaveViewState()
{
object[] state = new object[]
{
base.SaveViewState(),
((IStateManager)DefaultStyle).SaveViewState(),
((IStateManager)HoverStyle).SaveViewState(),
((IStateManager)SelectedStyle).SaveViewState(),
((IStateManager)TreeNodeTypes).SaveViewState(),
((IStateManager)Nodes).SaveViewState(),
};
// Check to see if we're really saving anything
foreach (object obj in state)
{
if (obj != null)
{
return state;
}
}
return null;
}
/// <summary>
/// Instructs the control to track changes to its view state.
/// </summary>
protected override void TrackViewState()
{
base.TrackViewState();
((IStateManager)Nodes).TrackViewState();
((IStateManager)TreeNodeTypes).TrackViewState();
((IStateManager)DefaultStyle).TrackViewState();
((IStateManager)HoverStyle).TrackViewState();
((IStateManager)SelectedStyle).TrackViewState();
}
/// <summary>
/// Color of the text within the control.
/// </summary>
[Browsable(false)]
public override System.Drawing.Color ForeColor
{
get { return base.ForeColor; }
set { base.ForeColor = value; }
}
/// <summary>
/// The font used for text within the control.
/// </summary>
[Browsable(false)]
public override FontInfo Font
{
get { return base.Font; }
}
/// <summary>
/// The tooltip displayed when the mouse is over the control.
/// </summary>
[Browsable(false)]
public override string ToolTip
{
get { return base.ToolTip; }
set { base.ToolTip = value; }
}
/// <summary>
/// Value to indicate whether child values need to recalculate
/// </summary>
internal bool AlwaysCalcValues
{
get { return IsDesignMode; }
}
/// <summary>
/// Value to indicate whether we're running in an up-level browser
/// </summary>
internal bool IsUpLevel
{
get { return IsUpLevelBrowser; }
}
/// <summary>
/// Forces the node to redatabind immediately, even if it isn't expanded.
/// Child nodes are only bound if they're expanded.
/// </summary>
public override void DataBind()
{
if (TreeNodeTypeSrc != String.Empty)
ReadTreeNodeTypeXmlSrc();
if (TreeNodeSrc != String.Empty)
{
ReadTreeNodeXmlSrc();
foreach (TreeNode node in Nodes)
{
node.OnInit();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -