最近比较忙,工作几乎无法喘息,突然闲置了一会,想想也好久没有发博客了,得写几篇了,最近好像灵感突然消失了,不管了,先发一篇吧,这篇肯定对大伙要帮助,好的话,您给我顶一个,不好,你也不要保留,直接告诉我,相互切磋才能进步嘛!不说了,上博客!

目录

MasterPage:... 2

How to begin?. 2

How to get control on master page from content page?. 2

How to use a strongly typed reference to master page in content page?. 2

How to add HtmlMeta from content page?. 4

How to add CSS or JavaScript from content page dynamically?. 4

How to change the master page at runtime?. 5

How to use nested master page?. 6

Themes and Skins. 7

What are themes and skins?. 7

How to change themes dynamically?. 7

Navigation controls. 8

How to access all TreeNodes in a TreeView control?. 8

How to expand or collapse all of TreeNodes with JavaScript?. 9

How to check/uncheck TreeNodes checkbox?. 11

How to generate a TreeView based on the folder structure?. 16

How to add a confirm dialog for each TreeNode?. 20

How to add Master Pages on the existing web forms?. 23

How to build a TreeView by using AJAX?. 23

How to use multiple sitemap files in ASP.NET 2.0?. 23

MasterPage:

How to begin?

There is a great article about master page which introduces how the master page works and the common problems developers face with and the tips and tricks to use master pages to their fullest potential.

http://www.odetocode.com/Articles/450.aspx

How to get control on master page from content page?

We can use the following code to get it,

Master page markup code:

<form id="form1" runat="server">

<asp:Label ID="lblMessage" runat="server" Text="I am a MasterPage"></asp:Label>

<div>

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

</div>

</form>

Content page code-behind file:

(Page.Master.FindControl("lblMsg") as Label).Text = "I Love ASP.NET!";

Related threads,

http://forums.asp.net/p/1371287/2867278.aspx

http://forums.asp.net/p/1367012/2843903.aspx

http://forums.asp.net/p/1353270/2776218.aspx

http://forums.asp.net/p/1324661/2641317.aspx

How to use a strongly typed reference to master page in content page?

We can place a @ MasterType directive in our content page to do this.

For example, see the following markup code of master page

:

<form id="form1" runat="server">

<asp:Label ID="lblMessage" runat="server" Text="I am a MasterPage"></asp:Label>

<div>

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

</div>

</form>

Code-behind:

public string Message

{

get { return lblMessage.Text; }

set { lblMessage.Text = value; }

}

Markup code of content page:

<%@ Page Title="" Language="C#" %>

<%@ MasterType VirtualPath="~/MasterPage.master" %>

Code-behind:

Master.Message = "I Love ASP.NET!";

We need to set this line in Web.config file if we use above code,

<pages masterPageFile="~/MasterPage.master">

We must use the following markup code in the content page if we do not assign any master page in the Web.config,

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>

<%@ MasterType VirtualPath="~/MasterPage.master" %>

Related threads,

http://forums.asp.net/t/915582.aspx

http://forums.asp.net/t/1267232.aspx

http://forums.asp.net/t/1217417.aspx

http://forums.asp.net/p/1174398/1980954.aspx

http://forums.asp.net/p/609559/620839.aspx

http://forums.asp.net/p/1358205/2794157.aspx

How to add HtmlMeta from content page?

See the following code,

HtmlMeta metaTag = new HtmlMeta();

metaTag.Name = "keywords";

metaTag.Content = "ASP.NET,C#,VB";

Header.Controls.Add(metaTag);

Now run the application and open the source code in the client side, we will see the bellow line,

<meta name="keywords" content="ASP.NET,C#,VB" />

Related threads,

http://forums.asp.net/p/1385135/2942780.aspx

http://forums.asp.net/p/1007704/1381203.aspx

http://forums.asp.net/p/1340962/2715956.aspx

http://forums.asp.net/p/1385201/2943162.aspx

http://forums.asp.net/p/1357221/2789468.aspx

http://forums.asp.net/p/1217546/2170120.aspx

How to add CSS or JavaScript from content page dynamically?

Page class contains a public property named Header.  we can convert HTML head tag to the corresponding server control by adding runat=”server” to its definition and then access the head tag in code behind. Now see the code,

HtmlLink cssLink = new HtmlLink();

cssLink.Href = "~/css.css";

cssLink.Attributes.Add("rel", "stylesheet");

cssLink.Attributes.Add("type", "text/css");

Header.Controls.Add(cssLink);

HtmlLink jsLink = new HtmlLink();

jsLink.Href = "~/js.js";

jsLink.Attributes.Add("language", "javascript");

jsLink.Attributes.Add("type", "text/javascript");

Header.Controls.Add(jsLink);

Now run the application and view the html source of the page, we will see,

<link href="css.css" rel="stylesheet" type="text/css" />

<link href="js.js" language="javascript" type="text/javascript" />

Related threads,

http://forums.asp.net/t/1377840.aspx

http://forums.asp.net/t/1274180.aspx

http://forums.asp.net/t/812415.aspx

http://forums.asp.net/t/1167113.aspx

http://forums.asp.net/t/1023605.aspx

http://forums.asp.net/p/1259642/2350917.aspx

http://forums.asp.net/p/974674/2185721.aspx

http://forums.asp.net/p/1011033/2899195.aspx

http://forums.asp.net/p/1063197/1537104.aspx

http://forums.asp.net/p/1207405/2129938.aspx

How to change the master page at runtime?

We can use the following code to do this,

protected void Page_PreInit(object sender, EventArgs e)

{

if (Session["user"] == null)

this.Page.MasterPageFile = "~/MasterPage2.master";

else

this.Page.MasterPageFile = "~/MasterPage1.master";

}

We must put the code in Page_PreInit which will be executed just before the Render event.

Related threads,

http://forums.asp.net/t/1170081.aspx

http://forums.asp.net/t/1260175.aspx

http://forums.asp.net/t/986565.aspx

http://forums.asp.net/t/1294163.aspx

http://forums.asp.net/p/1140254/1832376.aspx

How to use nested master page?

See the following complete code, this code show us how to nested master page step by step.

parent.master code:

<% @ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML

1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html>

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="Form1" runat="server">

<div>

<h1>

Parent Master</h1>

<p style="font: color=red">

This is parent master content.</p>

<asp:ContentPlaceHolder ID="MainContent" runat="server" />

</div>

</form>

</body>

</html>

child.master code,

<%@ Master Language="C#" MasterPageFile="~/parent .master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<asp:Panel runat="server" ID="panelMain" BackColor="lightyellow">

<h2>

Child master</h2>

<asp:Panel runat="server" ID="panel1" BackColor="lightblue">

<p>

This is child master content.</p>

<asp:ContentPlaceHolder ID="ChildContent1" runat="server" />

</asp:Panel>

<asp:Panel runat="server" ID="panel2" BackColor="pink">

<p>

This is child master content.</p>

<asp:ContentPlaceHolder ID="ChildContent2" runat="server" />

</asp:Panel>

<br />

</asp:Panel>

</asp:Content>

child.aspx code:

<%@ Page Language="C#" MasterPageFile="~/child.master" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ChildContent1" runat="server">

<asp:label runat="server" id="Label1" text="Child label1" font-bold="true" />

<br />

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ChildContent2" runat="server">

<asp:label runat="server" id="Label2" text="Child label2" font-bold="true" />

</asp:Content>

Related threads,

http://forums.asp.net/t/1059255.aspx

http://forums.asp.net/t/1270231.aspx

http://forums.asp.net/t/923612.aspx

http://forums.asp.net/p/1338093/2701349.aspx

http://forums.asp.net/p/1345334/2733082.aspx

Themes and Skins

What are themes and skins?

Refer this link,

http://msdn.microsoft.com/en-us/library/ykzx33wh.aspx

How to change themes dynamically?

We can programmatically set the theme for a page, but the Theme property must be set in the PreInit event of a page. This is because the skins in a theme are applied after the PreInit event fires but before the Init event fires.

protected void Page_PreInit(object sender, EventArgs e)

{

string thm;

thm = (string)Session["themeSelected"];

if (thm != null)

{

Page.Theme = thm;

DropDownList1.Text = thm;

}

else

{

Session["themeSelected"] = DropDownList1.Text;

Page.Theme = "Blue";

}

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

{

Session["themeSelected"] = DropDownList1.Text;

Server.Transfer(Request.FilePath);

}

Related threads,

http://forums.asp.net/t/1094690.aspx

http://forums.asp.net/t/879808.aspx

http://forums.asp.net/t/1138835.aspx

http://forums.asp.net/p/1106977/1695768.aspx

http://forums.asp.net/p/1383293/2936429.aspx http://forums.asp.net/p/1363437/2822860.aspx

Navigation controls

How to access all TreeNodes in a TreeView control?

We can use a recursive function to implement it, please reference below sample code:

protected void Page_Load(object sender, EventArgs e)

{

foreach (TreeNode node in tvDemo.Nodes)

{

SetNode(node);

}

}

void SetNode(TreeNode node)

{

if (node.Depth != 0)

{

//some code

}

if (node.ChildNodes.Count > 0)

{

foreach (TreeNode childnode in node.ChildNodes)

{

SetNode(childnode);

}

}

}

Related threads,

http://forums.asp.net/t/1378432.aspx

http://forums.asp.net/p/1374742/2885991.aspx

http://forums.asp.net/p/1362784/2820103.aspx

How to expand or collapse all of TreeNodes with JavaScript?

We can expand and collapse all TreeNodes in server side, but it needs a post back. For avoiding this unnecessary behavior, we can use JavaScript to do this task.

In the below sample code, the function “TreeviewExpandCollapseAll” is used to demonstrate how to expand or collapse a TreeView. The function takes two parameters, the first parameter “treeViewId” is the TreeView control’s ID, and the second one “expandAll” is used to determine whether or not to expand the TreeView control.

See the below complete code,

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>TreeView Demo</title>

<script language="javascript" type="text/javascript">

function TreeviewExpandCollapseAll(treeViewId, expandAll) {

var displayState = (expandAll == true ? "none" : "block");

var treeView = document.getElementById(treeViewId);

if (treeView) {

var treeLinks = treeView.getElementsByTagName("a");

var nodeCount = treeLinks.length;

for (i = 0; i < nodeCount; i++) {

if (treeLinks[i].firstChild.tagName) {

if (treeLinks[i].firstChild.tagName.toLowerCase() == "img") {

var currentToggleLink = treeLinks[i];

var childContainer = GetParentByTagName("table", currentToggleLink).nextSibling;

if (childContainer.style.display == displayState) {

eval(currentToggleLink.href);

}

}

}

}

}

}

function GetParentByTagName(parentTagName, childElementObj) {

var parent = childElementObj.parentNode;

while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {

parent = parent.parentNode;

}

return parent;

} </script>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TreeView ID="TreeViewDemo" runat="server" ExpandDepth="1">

<Nodes>

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A" Value="A">

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A1" Value="A1">

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A11" Value="A11"></asp:TreeNode>

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A12" Value="A12"></asp:TreeNode>

</asp:TreeNode>

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A2" Value="A2">

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A21" Value="A21"></asp:TreeNode>

<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A22" Value="A22"></asp:TreeNode>

</asp:TreeNode>

</asp:TreeNode>

</Nodes>

</asp:TreeView>

<a href="javascript:TreeviewExpandCollapseAll('<%=TreeViewDemo.ClientID%>', true)">Expand

All</a> <a href="javascript:TreeviewExpandCollapseAll('<%=TreeViewDemo.ClientID%>', false)">

Collapse All</a>

</div>

</form>

</body>

</html>

Related threads,

http://forums.asp.net/p/1355481/2779144.aspx

http://forums.asp.net/p/1359189/2799350.aspx

How to check/uncheck TreeNodes checkbox?

When we set ShowCheckBoxes="All", we would like to provide a feature if people select the Root Node’s checkbox, then it’s all child nodes’ checkboxes are checked automatically.

Basically, when the parent node is checked, all the child nodes should be checked automatically; when one child node is unchecked, the parent node should be unchecked.

It is easy to do this in server side. For some reason, we do not want to post back, so, for preventing this behavior, we can use JavaScript to accomplish this task.

In the below functions:

·          OnTreeClick: used to check parent and child node;

·          CheckUncheckChildren: used to check or uncheck the child node;

·          CheckUncheckParents: used to check or uncheck the parent node;

·          AreAllSiblingsChecked:used to check all siblings node;

·          GetParentByTagName: used to get parent tag name.

See the below complete code,

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

TreeViewDemo.Attributes.Add("onclick", "OnTreeClick(event)");

}

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>TreeView</title>

<script language="javascript" type="text/javascript">

//************************** Treeview Parent-Child check behavior ****************************//

function OnTreeClick(evt)

{

var src = window.event != window.undefined ? window.event.srcElement : evt.target;

var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");

if(isChkBoxClick)

{

var parentTable = GetParentByTagName("table", src);

var nxtSibling = parentTable.nextSibling;

if(nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node

{

if(nxtSibling.tagName.toLowerCase() == "div") //if node has children

{

//check or uncheck children at all levels

CheckUncheckChildren(parentTable.nextSibling, src.checked);

}

}

//check or uncheck parents at all levels

CheckUncheckParents(src, src.checked);

}

}

function CheckUncheckChildren(childContainer, check)

{

var childChkBoxes = childContainer.getElementsByTagName("input");

var childChkBoxCount = childChkBoxes.length;

for(var i = 0; i<childChkBoxCount; i++)

{

childChkBoxes[i].checked = check;

}

}

function CheckUncheckParents(srcChild, check)

{

var parentDiv = GetParentByTagName("div", srcChild);

var parentNodeTable = parentDiv.previousSibling;

if(parentNodeTable)

{

var checkUncheckSwitch;

if(check) //checkbox checked

{

var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);

if(isAllSiblingsChecked)

checkUncheckSwitch = true;

else

return; //do not need to check parent if any child is not checked

}

else //checkbox unchecked

{

checkUncheckSwitch = false;

}

var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");

if(inpElemsInParentTable.length > 0)

{

var parentNodeChkBox = inpElemsInParentTable[0];

parentNodeChkBox.checked = checkUncheckSwitch;

//do the same recursively

CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);

}

}

}

function AreAllSiblingsChecked(chkBox)

{

var parentDiv = GetParentByTagName("div", chkBox);

var childCount = parentDiv.childNodes.length;

for(var i=0; i<childCount; i++)

{

if(parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node

{

if(parentDiv.childNodes[i].tagName.toLowerCase() == "table")

{

var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];

//if any of sibling nodes are not checked, return false

if(!prevChkBox.checked)

{

return false;

}

}

}

}

return true;

}

//utility function to get the container of an element by tagname

function GetParentByTagName(parentTagName, childElementObj)

{

var parent = childElementObj.parentNode;

while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())

{

parent = parent.parentNode;

}

return parent;

}

</script>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TreeView ID="TreeViewDemo" runat="server" ShowCheckBoxes="All">

<Nodes>

<asp:TreeNode Text="My Computer">

<asp:TreeNode Text="Favorites">

<asp:TreeNode Text="News">

<asp:TreeNode Text="MSN" NavigateUrl="http://www.msn.com" />

<asp:TreeNode Text="MSNBC News" NavigateUrl="http://www.msnbc.msn.com" />

</asp:TreeNode>

<asp:TreeNode Text="Technology">

<asp:TreeNode Text="Microsoft" NavigateUrl="http://www.microsoft.com" />

<asp:TreeNode Text="ASP.NET" NavigateUrl="http://www.asp.net" />

<asp:TreeNode Text="GotDotNet" NavigateUrl="http://www.gotdotnet.com" />

<asp:TreeNode Text="MSDN" NavigateUrl="http://msdn.microsoft.com" />

</asp:TreeNode>

<asp:TreeNode Text="Shopping">

<asp:TreeNode Text="MSN Shopping" NavigateUrl="http://shopping.msn.com" />

<asp:TreeNode Text="MSN Autos" NavigateUrl="http://autos.msn.com" />

</asp:TreeNode>

</asp:TreeNode>

<asp:TreeNode Text="City Links">

<asp:TreeNode Text="MapPoint" NavigateUrl="http://www.mappoint.com" />

<asp:TreeNode Text="MSN City Guides" NavigateUrl="http://local.msn.com" />

</asp:TreeNode>

<asp:TreeNode Text="Music Links">

<asp:TreeNode Text="MSN Music" NavigateUrl="http://music.msn.com" />

</asp:TreeNode>

</asp:TreeNode>

</Nodes>

</asp:TreeView>

</div>

</form>

</body>

</html>

Related threads,

http://forums.asp.net/t/1367074.aspx

http://forums.asp.net/t/976122.aspx

http://forums.asp.net/t/1088627.aspx

How to generate a TreeView based on the folder structure?

Sometimes, we need to generate a TreeView based on the folders structure dynamically.

In the below methods,

·          SetNodes: used to change the navigation URL by iterating through all nodes;

·          OutputDirectory: used to output a directory to a node;

·          ConvertFileToRelativePaths: used to convert the path;

·          GenerateDirectoryTree: used to generate a directory tree;

·          BindDirectoryToDropDownList: used to bind directories to the   DropDownList control.

See the below complete code,

Markup code (MyWebsiteTreeView.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyWebsiteTreeView.aspx.cs"

Inherits="MyWebsiteTreeView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TreeView ID="TreeView1" runat="server">

</asp:TreeView>

<asp:DropDownList ID="ddlRootDirectory" runat="server">

</asp:DropDownList>

<asp:Button ID="btnGenerateDirectoryTree" runat="server" Text="Ggenerate Directory Tree"

onclick="btnGenerateDirectoryTree_Click" />

</div>

</form>

</body>

</html>

Behind code (MyWebsiteTreeView.aspx.cs):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

public partial class MyWebsiteTreeView : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

BindDirectoryToDropDownList(ddlRootDirectory);

}

}

/// <summary>

/// Change the tree

/// </summary>

/// <param name="node"></param>

void SetNodes(TreeNode node)

{

string ab = node.NavigateUrl;

node.NavigateUrl = node.NavigateUrl.Replace("FAQ\\", "");//FAQ is the directory name

node.NavigateUrl = node.NavigateUrl.Replace("\\", "/");//For Firefox and IE

if (node.ChildNodes.Count > 0)

{

foreach (TreeNode childNode in node.ChildNodes)

{

SetNodes(childNode);

}

}

}

/// <summary>

///  Output Directory to a node

/// </summary>

/// <param name="directory"></param>

/// <param name="parentNode"></param>

/// <returns></returns>

TreeNode OutputDirectory(System.IO.DirectoryInfo directory, TreeNode parentNode)

{

// validate param

if (directory == null) return null;

// create a node for this directory

TreeNode DirNode = new TreeNode(directory.Name);

// get subdirectories of the current directory

System.IO.DirectoryInfo[] SubDirectories = directory.GetDirectories();

// output each subdirectory

for (int DirectoryCount = 0; DirectoryCount < SubDirectories.Length; DirectoryCount++)

{

OutputDirectory(SubDirectories[DirectoryCount], DirNode);

}

// output the current directories files

System.IO.FileInfo[] Files = directory.GetFiles();

for (int FileCount = 0; FileCount < Files.Length; FileCount++)

{

//if (Files[FileCount].Extension == ".htm")

//{

string filename = ConvertFileToRelativePaths(Files[FileCount].FullName, "FAQ");//FAQ is the directory name

DirNode.ChildNodes.Add(new TreeNode(Files[FileCount].Name, Files[FileCount].Name, "", filename, "_blank"));

//}

}

// if the parent node is null, return this node

// otherwise add this node to the parent and return the parent

if (parentNode == null)

{

return DirNode;

}

else

{

parentNode.ChildNodes.Add(DirNode);

return parentNode;

}

}

/// <summary>

/// Convert File To Relative Paths

/// </summary>

/// <param name="fileName"></param>

/// <param name="rootName"></param>

/// <returns></returns>

string ConvertFileToRelativePaths(string fileName, string rootName)

{

return fileName.Substring(fileName.LastIndexOf(rootName));

}

/// <summary>

/// Generate Directory Tree

/// </summary>

/// <param name="tv"></param>

void GenerateDirectoryTree(TreeView tv)

{

tv.Nodes.Clear();

System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/" + ddlRootDirectory.SelectedValue + "/"));

//set the root as "~/"

if (ddlRootDirectory.SelectedValue == "Root")

{

RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));

}

// output the directory into a node

TreeNode RootNode = OutputDirectory(RootDir, null);

// add the output to the tree

TreeView1.Nodes.Add(RootNode);

foreach (TreeNode node in tv.Nodes)

{

SetNodes(node);

}

}

/// <summary>

/// bind data to DropDownList

/// </summary>

/// <param name="ddl"></param>

void BindDirectoryToDropDownList(DropDownList ddl)

{

ddl.Items.Clear();

System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));

System.IO.DirectoryInfo[] SubDirectories = RootDir.GetDirectories();

ddl.Items.Add("Root");

foreach (DirectoryInfo dir in SubDirectories)

{

ddl.Items.Add(dir.Name);

}

}

/// <summary>

/// Generate the tree

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void btnGenerateDirectoryTree_Click(object sender, EventArgs e)

{

GenerateDirectoryTree(TreeView1);

}

}

Related threads,

http://forums.asp.net/p/1378432/2920430.aspx

http://forums.asp.net/p/1364377/2828536.aspx

How to add a confirm dialog for each TreeNode?

When we click on a TreeNode, it will navigate to another page directly.  But sometimes, we need to popup a dialog to ask the user to confirm to access the page or not.

See the below complete code,

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

string confirmMessage = "Yes/No?";

// write a function for popup a confirm dialog

string script = @"function treeNodeConfirmation(mEvent, text)

{

var o;

// Internet Explorer

if (mEvent.srcElement)

{

o = mEvent.srcElement;

}

// Netscape and Firefox

else if (mEvent.target)

{

o = mEvent.target;

}

if(o.tagName == 'A' || o.tagName == 'a')

{

return confirm (text);

}

}";

// regist the function

ScriptManager.RegisterClientScriptBlock(myTreeView, typeof(TreeView), "treeNodeClickConfirm", script, true);

// add the function to the TreeView’s Attributes

// it means when the client users click the TreeNodes, it will popup a confirm // dialog.

myTreeView.Attributes.Add("onclick", "javascript:return treeNodeConfirmation(event, '" + confirmMessage + "')");

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Demo</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:TreeView ID="myTreeView" runat="server">

<Nodes>

<asp:TreeNode Text="My Computer">

<asp:TreeNode Text="Favorites">

<asp:TreeNode Text="News">

<asp:TreeNode Text="MSN" NavigateUrl="http://www.msn.com" />

<asp:TreeNode Text="MSNBC News" NavigateUrl="http://www.msnbc.msn.com" />

</asp:TreeNode>

<asp:TreeNode Text="Technology">

<asp:TreeNode Text="Microsoft" NavigateUrl="http://www.microsoft.com" />

<asp:TreeNode Text="ASP.NET" NavigateUrl="http://www.asp.net" />

<asp:TreeNode Text="GotDotNet" NavigateUrl="http://www.gotdotnet.com" />

<asp:TreeNode Text="MSDN" NavigateUrl="http://msdn.microsoft.com" />

</asp:TreeNode>

<asp:TreeNode Text="Shopping">

<asp:TreeNode Text="MSN Shopping" NavigateUrl="http://shopping.msn.com" />

<asp:TreeNode Text="MSN Autos" NavigateUrl="http://autos.msn.com" />

</asp:TreeNode>

</asp:TreeNode>

<asp:TreeNode Text="City Links">

<asp:TreeNode Text="MapPoint" NavigateUrl="http://www.mappoint.com" />

<asp:TreeNode Text="MSN City Guides" NavigateUrl="http://local.msn.com" />

</asp:TreeNode>

<asp:TreeNode Text="Music Links">

<asp:TreeNode Text="MSN Music" NavigateUrl="http://music.msn.com" />

</asp:TreeNode>

</asp:TreeNode>

</Nodes>

</asp:TreeView>

</div>

</form>

</body>

</html>

Related threads,

http://forums.asp.net/t/1020789.aspx

http://forums.asp.net/t/1355481.aspx

http://forums.asp.net/p/1353925/2771697.aspx

http://forums.asp.net/p/1353925/2790985.aspx

How to add Master Pages on the existing web forms?

Refer this link,

http://forums.asp.net/t/1244510.aspx

How to build a TreeView by using AJAX?

Refer this link,

http://www.codeproject.com/KB/aspnet/TreeViewAjax.aspx

How to use multiple sitemap files in ASP.NET 2.0?

Refer this link,

http://www.codeproject.com/KB/aspnet/MutlipleSiteMap.aspx

转载于:https://www.cnblogs.com/OceanChen/archive/2009/02/18/1393291.html

【原】母版页、皮肤、导航 那点事 Master Pages Themes and Navigation Controls FAQ相关推荐

  1. C# 母版页页面导航

    C# 母版页&页面导航 母版页master 页面导航 站点地图 SiteMapDataSource控件 TreeView控件 Menu控件 ASP.NET常用对象 Response对象 Req ...

  2. 母版页(Master Pages)--轉載

    母版页(Master Pages) http://blog.csdn.net/iiboy/ ASP.NET 1.x中最突出的缺点之一是它缺少对页面模板的支持.欠缺的是定义其他页面可以继承的" ...

  3. 母版页(Master Pages)

    ASP.NET 1.x中最突出的缺点之一是它缺少对页面模板的支持.欠缺的是定义其他页面可以继承的"母版页"能力.开发人员通过使用用户控件(它们可以容易地在页面之间复制)创建页面来弥 ...

  4. ASP.NET MVC Tip #31: 给 Master Pages 和 User Controls 传递数据

    原文地址:ASP.NET MVC Tip #31 – Passing Data to Master Pages and User Controls 原文作者:swalther 本文译者:QLeelul ...

  5. ASP.net 2.0 Migrating系列 - Master Pages 感触

    ASP.net 2.0 Migrating系列 - Master Pages范维肖 在Visual Web Developer 2005里的新建里多了一个Master Pages,在微软的VWD200 ...

  6. 了解Master Pages库

    aspnet forums界面的最关键的问题首先在于它使用了MetaBuilders的Master Pages 控件. 到http://www.metabuilders.com/Tools/Maste ...

  7. MOSS 2007基础:WSS 3.0 中的母版页(Master Pages)和内容页(Content Pages)

    原文地址:http://robgarrett.com/cs/blogs/software/archive/2006/07/03/2065.aspx 这篇很短的文章展示了ASP.NET2.0中的母板页和 ...

  8. [转]微软代码示例:ASP.NET 2.0 三层架构应用程序教程系列

    本文转自:http://www.codeusing.com/hi/uephee.wen/resource/view/170.aspx 资源分类:微软代码示例               更新日期:20 ...

  9. WebForm 母版页使用

    首先来说一下什么是母版页: 母版页可以为应用程序中的页创建一致的布局.单个母版页可以为应用程序中的所有页(或一组页)定义所需的外观和标准行为. 母版页的使用与普通页面类似,可以在其中放置文件或者图形. ...

最新文章

  1. Elgg网站迁移指南
  2. Android 注册登入界面完美设计
  3. 如何自定义Tableau 调色板
  4. #10017 「一本通 1.2 练习 4」传送带+三分套三分
  5. 让Terminal显示git分支
  6. Java破碎重组_Java代码重组
  7. 亡命逃窜(nyoj523广搜)
  8. 说了这么多 5G,最关键的技术在这里
  9. mysql5.7 jmeter_JMeter5连接Mysql数据库
  10. (十九)债券定价与债券收益率的计算
  11. new与delete动态分配和释放内存
  12. CrossAir CA-C03 2.4G贴片天线使用步骤
  13. 华钜同创:亚马逊中常见的广告类型及其专业名词解释
  14. 炫云全新支持优化渲染质量了
  15. 群体结构分析:用 phylip 构建进化树
  16. 计算机修改users用户名,笔记本电脑更改用户名_笔记本电脑更改user
  17. xCode 编写C++程序
  18. 搜狐网络评论系统–畅言!与多说、友言、灯鹭、新浪评论简单评测
  19. 常见的5种项目管理问题类型
  20. 利用C语言实现数字倒序输出

热门文章

  1. 无需用户输入,Adobe提出自动高质量图像合成新方法
  2. 实例分割的进阶三级跳:从 Mask R-CNN 到 Hybrid Task Cascade
  3. 利用Python写俄罗斯方块游戏
  4. 总结 | 深度学习之Pytorch入门教程
  5. 人工智能学习书单推荐
  6. 精选 26 个 Python 实用技巧,想秀技能先 Get 这份技术列表!
  7. 深度学习与TensorFlow:FCN论文学习笔记
  8. 两种贝塞尔曲线选点方法的对比
  9. modelsim和matlab联合仿真,Modelsim与Matlab联合仿真
  10. mysql 正则regrx_正则表达式