<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
写网站首页程序的导航,要用asp.net2.0 的下menu控件,因为绑定数据库十分方便,不过有个问题,就是我采用的iframe来跳转页面,不想整个首页都跳转,不过发现menu控件没有控制回发的属性,每单击就回发一次,挺麻烦的。后来想到一个曲折来实现的办法,在text属性上做文章,代码如下:
index.aspx页面:
.....
< scriptlanguage = javascript >
functionOpenNewWindow(strUrl)
{
document.all.UrlRedirect.src = strUrl;
}
</ script >
< asp:MenuID = " Menuindex " runat = " server " Orientation = " Horizontal " >
</ asp:Menu >
....
< iframeid = " UrlRedirect " name = " UrlRedirect " ></ iframe >
.....
index.aspx.cs
......
protected void Page_Load( object sender,EventArgse)
{
InitNavigationTree(Menuindex.Items, " 0 " );
}
private void InitNavigationTree(MenuItemCollectionmenuItemCollection, string sParentID)
{
DataViewdvw = new DataView();
MenuItemnodTemp;
dvw.Table = getDataAll( " select*fromF_menuwhereF_PARENTMENUGUID=' " + sParentID + " 'orderbyF_ORDER " );
foreach (DataRowViewdrv in dvw)
{
nodTemp = new MenuItem();
nodTemp.Value = drv[ " F_MENUGUID " ].ToString();
nodTemp.Text = GetMenuText(drv[ " F_LINKPAGE " ].ToString(),drv[ " F_MENUNAME " ].ToString());
menuItemCollection.Add(nodTemp);
InitNavigationTree(nodTemp.ChildItems,nodTemp.Value);
}
}
private string GetMenuText( string linkTxt, string nameTxt)
{
string temp = " <astyle='cursor:hand'onclick=OpenNewWindow(' " + linkTxt + " ')> " + nameTxt + " </a> " ;
return temp;
}
..........
.....
< scriptlanguage = javascript >
functionOpenNewWindow(strUrl)
{
document.all.UrlRedirect.src = strUrl;
}
</ script >
< asp:MenuID = " Menuindex " runat = " server " Orientation = " Horizontal " >
</ asp:Menu >
....
< iframeid = " UrlRedirect " name = " UrlRedirect " ></ iframe >
.....
index.aspx.cs
......
protected void Page_Load( object sender,EventArgse)
{
InitNavigationTree(Menuindex.Items, " 0 " );
}
private void InitNavigationTree(MenuItemCollectionmenuItemCollection, string sParentID)
{
DataViewdvw = new DataView();
MenuItemnodTemp;
dvw.Table = getDataAll( " select*fromF_menuwhereF_PARENTMENUGUID=' " + sParentID + " 'orderbyF_ORDER " );
foreach (DataRowViewdrv in dvw)
{
nodTemp = new MenuItem();
nodTemp.Value = drv[ " F_MENUGUID " ].ToString();
nodTemp.Text = GetMenuText(drv[ " F_LINKPAGE " ].ToString(),drv[ " F_MENUNAME " ].ToString());
menuItemCollection.Add(nodTemp);
InitNavigationTree(nodTemp.ChildItems,nodTemp.Value);
}
}
private string GetMenuText( string linkTxt, string nameTxt)
{
string temp = " <astyle='cursor:hand'onclick=OpenNewWindow(' " + linkTxt + " ')> " + nameTxt + " </a> " ;
return temp;
}
..........
这样menu就不回发了,而只会调用菜单文本上的那个onclick事件。
注:getDataAll 方法是获得菜单表数据的方法,在此省略了.
本文介绍如何使用 ASP.NET 2.0 的 Menu 控件结合 JavaScript 实现不触发页面回发的导航功能。通过 iframe 和自定义 JavaScript 函数 OpenNewWindow 实现了点击菜单项时仅加载指定 URL 到 iframe 中,避免了整个页面的重新加载。
2365

被折叠的 条评论
为什么被折叠?



