1、两个Repeater嵌套实现动态菜单
今天在实现一个增强web项目可扩展性的一个功能,把以前的静态菜单改成了动态的扩展、可维护的菜单
页面代码
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"> <ItemTemplate> <h1 class="type"><a href="javascript:void(0)"><%# DataBinder.Eval(Container.DataItem, "MT_name")%></a></h1> <asp:Repeater ID="Repeater2" runat="server"> <HeaderTemplate> <div class="content"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><img src="images/menu_topline.gif" width="182" height="5" /></td> </tr> </table> <ul class="MM"> </HeaderTemplate> <ItemTemplate> <li><a href="<%# DataBinder.Eval(Container.DataItem, "MI_page")%>" target="main"><%# DataBinder.Eval(Container.DataItem, "MI_name")%></a></li> </ItemTemplate> <FooterTemplate> </ul></div> </FooterTemplate> </asp:Repeater> </ItemTemplate> </asp:Repeater>
服务器端代码
public partial class left : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select MT_ID,MT_name from ModuleTopInfo"; SqlDbHelper db = new SqlDbHelper();//SqlHelper类你懂得 Repeater1.DataSource =db.ExecuteDataTable(sql); Repeater1.DataBind(); } } protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { string id = ((DataRowView)e.Item.DataItem).Row["MT_ID"].ToString(); Repeater rpt = (Repeater)e.Item.FindControl("Repeater2"); string sql = "select MI_ID,MI_page,MI_name from ModuleInfo where MT_ID=@id"; SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@id",id) }; SqlDbHelper db = new SqlDbHelper(); DataTable dt = new DataTable(); dt = db.ExecuteDataTable(sql,CommandType.Text,parameters); if (rpt != null) { rpt.DataSource = dt; rpt.DataBind(); } } }
实现效果截图
2、通过format输出带{}的字符
string s = string.Format("abc{{{0}}}{{{1}}}", "9999","123");
Response.Write(s);
//输出
abc{9999}{123}
3、弹出框样式
http://www.cnblogs.com/zengxiangzhan/archive/2011/08/27/2155335.html
4、LINQ To XML增删改查
http://www.cnblogs.com/mingmingruyuedlut/archive/2011/01/27/1946239.html
5、C# WinForm下DataGridView绘制背景图
http://archive.cnblogs.com/a/2201983/
6、数据控件行选择事件
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标在某行,更改背景色
e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#F9D79C'");
//当鼠标离开,还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
//鼠标变小手 ondblclick双击onClick单击
e.Row.Attributes["style"] = "Cursor:hand";
e.Row.Attributes.Add("onClick", "this");
string nodenumber = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes.Add("onClick", "window.open('manage/AddNodeB.aspx?nodenumber=" + nodenumber + "');window.parent.top.opener = null;window.open('','_self');parent.top.window.close()");
}
}
数据库连接字符串:http://www.connectionstrings.com/
var zNodes =[{"name":"所用","id":"001","pid":"001", nodes:[{"name":"所用1","id":"0011","pid":"001111"}]}, {"name":"核计划","id":"002","pid":"002", nodes:[{"name":"核计划1","id":"0021","pid":"002"}]}], --------------------------------------------------------------------- function showAllNode(thejson){ var flag = false;// 当前元素是否是数组 for(var e in thejson){ alert("e:"+e); if(e=="parentNode") continue; for(var ex in thejson[e]){ flag = true; break; } if(flag){ showAllNode(thejson[e]); }else if(thejson[e]){ alert(thejson[e]); } } if(!flag) { return; } }