呵呵 我这也有MENU控件数据库绑定的例子 就是太麻烦了 现在帖出来给大家看看
最好能找到简洁的办法:
protected string GetPopMenu(string AlternatingImage, int RepeatColumns, bool Flow)
{
if (Cache["PopMenu"] != null)
{
return Cache["PopMenu"].ToString();
}
else
{
DataTable MainMenuTable = new DataTable(), PopMenuTable = new DataTable();
SqlConnection Connection = new SqlConnection(DataProvider.ConnectionString);
// 加载菜单数据
SqlCommand Command = new SqlCommand();
Command.Connection = Connection;
Command.CommandText = "Select * From PopMenu Where Parentid=0 Order By Orderid";
SqlDataAdapter DataAdapter = new SqlDataAdapter();
DataAdapter.SelectCommand = Command;
DataAdapter.Fill(MainMenuTable);
Command.CommandText = "Select * From PopMenu Where Parentid>0 Order By Parentid,Orderid"; ;
DataAdapter.Fill(PopMenuTable);
// 清理数据库连接资源
Connection.Dispose();
Command.Dispose();
DataAdapter.Dispose();
// 获取子菜单数据
int Child = 0;
StringBuilder ChildMenu = new StringBuilder();
ChildMenu.Append("\r\n<div id=\"popmenu\" class=\"menuskin\" style=\"z-index:100\" onmouseover=\"clearhidemenu(); highlightmenu(event,'on')\" onmouseout=\"highlightmenu(event,'off'); dynamichide(event)\"></div>\r\n");
ChildMenu.Append("<script language=\"javascript\" type=\"text/javascript\">\r\n");
ChildMenu.Append("<!--\r\n");
for (int i = 0; i < MainMenuTable.Rows.Count; i++)
{
for (int j = 0; j < PopMenuTable.Rows.Count; j++)
{
if (Convert.ToInt32(PopMenuTable.Rows[j]["Parentid"]) == Convert.ToInt32(MainMenuTable.Rows[i]["Menuid"]))
{
if (Child == 0)
{
ChildMenu.Append("\t" + "linkset[" + MainMenuTable.Rows[i]["Menuid"] + "] = new Array();" + Environment.NewLine);
}
ChildMenu.Append("\t" + "linkset[" + MainMenuTable.Rows[i]["Menuid"] + "][" + Child + "] = \"<div class='menuitems'><a" + (PopMenuTable.Rows[j]["MenuUrl"] != DBNull.Value ? " href='" + PopMenuTable.Rows[j]["MenuUrl"] + "'" : null) + " title='" + PopMenuTable.Rows[j]["MenuAlt"] + "'" + (Convert.ToBoolean(PopMenuTable.Rows[j]["Target"]) ? " target='_blank'" : null) + ">" + PopMenuTable.Rows[j]["MenuName"] + "</a></div>\";" + Environment.NewLine);
Child++;
}
}
if (Child > 0)
{
MainMenuTable.Rows[i]["MenuName"] = "<a onmouseover=\"showmenu(event," + MainMenuTable.Rows[i]["Menuid"] + ",1,false)\" onmouseout=\"delayhidemenu()\"" + (MainMenuTable.Rows[i]["MenuUrl"] != DBNull.Value ? " href=\"" + MainMenuTable.Rows[i]["MenuUrl"] + "\"" : null) + " title=\"" + MainMenuTable.Rows[i]["MenuAlt"] + "\"" + (Convert.ToBoolean(MainMenuTable.Rows[i]["Target"]) ? " target=\"_blank\"" : null) + ">" + MainMenuTable.Rows[i]["MenuName"] + "</a>";
Child = 0;
}
else
{
MainMenuTable.Rows[i]["MenuName"] = "<a" + (MainMenuTable.Rows[i]["MenuUrl"] != DBNull.Value ? " href=\"" + MainMenuTable.Rows[i]["MenuUrl"] + "\"" : null) + " title=\"" + MainMenuTable.Rows[i]["MenuAlt"] + "\"" + (Convert.ToBoolean(MainMenuTable.Rows[i]["Target"]) ? " target=\"_blank\"" : null) + ">" + MainMenuTable.Rows[i]["MenuName"] + "</a>";
}
}
ChildMenu.Append("\t" + "jsdone = true;" + Environment.NewLine);
ChildMenu.Append("// -->\r\n");
ChildMenu.Append("</script>" + Environment.NewLine);
// 获取主菜单数据
StringBuilder MainMenu = new StringBuilder();
MainMenu.Append("<script language=\"javascript\" type=\"text/javascript\" src=\"JScript/PopMenu.js\"></script>" + Environment.NewLine);
if (Flow)
{
MainMenu.Append("<span id=\"PopMenuList\">" + Environment.NewLine);
if (AlternatingImage != null && AlternatingImage != "")
{
AlternatingImage = "\t<span><img alt=\"\" src=\"" + AlternatingImage + "\" /></span>" + Environment.NewLine;
}
for (int i = 0; i < MainMenuTable.Rows.Count - 1; i++)
{
MainMenu.Append("\t<span>" + MainMenuTable.Rows[i]["MenuName"] + "</span>" + Environment.NewLine);
if ((i + 1) % RepeatColumns == 0)
{
MainMenu.Append("\t<br />" + Environment.NewLine);
}
else
{
MainMenu.Append(AlternatingImage);
}
}
// Edit By AlphaC 2006-3-21 18:20
//MainMenu.Append("\t<span>" + MainMenuTable.Rows[MainMenuTable.Rows.Count - 1]["MenuName"] + "</span>" + Environment.NewLine);
if(MainMenuTable.Rows.Count > 0)
MainMenu.Append("\t<span>" + MainMenuTable.Rows[MainMenuTable.Rows.Count - 1]["MenuName"] + "</span>" + Environment.NewLine);
MainMenu.Append("</span>" + Environment.NewLine);
}
else
{
MainMenu.Append("<table id=\"PopMenuList\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">" + Environment.NewLine);
MainMenu.Append("\t<tr>" + Environment.NewLine);
if (AlternatingImage != null && AlternatingImage != "")
{
AlternatingImage = "\t\t<td align=\"center\"><img alt=\"\" src=\"" + AlternatingImage + "\" /></td>" + Environment.NewLine;
}
for (int i = 0; i < MainMenuTable.Rows.Count - 1; i++)
{
MainMenu.Append("\t\t<td align=\"center\">" + MainMenuTable.Rows[i]["MenuName"] + "</td>" + Environment.NewLine);
if ((i + 1) % RepeatColumns == 0)
{
MainMenu.Append("\t</tr>" + Environment.NewLine);
MainMenu.Append("\t<tr>" + Environment.NewLine);
}
else
{
MainMenu.Append(AlternatingImage);
}
}
MainMenu.Append("\t\t<td align=\"center\">" + MainMenuTable.Rows[MainMenuTable.Rows.Count - 1]["MenuName"] + "</td>" + Environment.NewLine);
for (int i = 0; i < MainMenuTable.Rows.Count % RepeatColumns; i++)
{
MainMenu.Append(AlternatingImage);
MainMenu.Append("\t\t<td align=\"center\"> </td>" + Environment.NewLine);
}
MainMenu.Append("\t</tr>" + Environment.NewLine);
MainMenu.Append("</table>" + Environment.NewLine);
}
// 建立缓存
Cache["PopMenu"] = MainMenu.ToString() + ChildMenu.ToString();
return MainMenu.ToString() + ChildMenu.ToString();
}
}