专注于.Net

享受编程的乐趣
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Repeater控件实现左侧快捷菜单

Posted on 2010-05-17 16:25  古道飘零客  阅读(344)  评论(0编辑  收藏  举报

<head runat="server">
    <title>无标题页</title>
<style type="text/css">
h3{height:28px;line-height:28px;width:182px;background-image:url(Images/menu_bg1.gif);background-repeat:no-repeat;font-size:13px;padding-left:30px;cursor:pointer;margin:0;}
td a:link,td a:visited{display:block;height:28px;line-height:28px;width:182px;background-image:url(Images/menu_bg1.gif);background-repeat:no-repeat;padding-left:60px;}
td a:hover{background-image:url(Images/menu_bg2.gif);background-repeat:no-repeat;}
div{margin:0;padding:0;}
</style>
<script type="text/javascript">
function ShowOrHide(obj)
{
if(obj.parentNode.childNodes(1).style.display == "none")
    obj.parentNode.childNodes(1).style.display = "block";
else
    obj.parentNode.childNodes(1).style.display = "none";
}
</script>
</head>
<body style="width:960px;margin:0 auto;padding:0;background-color:#FFFFFF;font-size:13px;">
    <form id="form1" runat="server">
    <div style="width:300px;float:left;">
      <div>
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
          <ItemTemplate>
            <div>
              <div onclick="ShowOrHide(this)">
                <h3><asp:Literal ID="Literal1" runat="server"></asp:Literal></h3>
              </div>
              <div style="margin:0;padding:0;display:none;">
                <table border="0" cellspacing="0">
                  <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                    <ItemTemplate>
                      <tr>
                        <td><asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink></td>
                      </tr>
                    </ItemTemplate>
                  </asp:Repeater>
                </table>
              </div>
            </div>
          </ItemTemplate>
          <AlternatingItemTemplate>
            <div>
              <div onclick="ShowOrHide(this)">
                <h3><asp:Literal ID="Literal1" runat="server"></asp:Literal></h3>
              </div>
              <div style="margin:0;padding:0;display:none;">
                <table border="0" cellspacing="0">
                  <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
                    <ItemTemplate>
                      <tr>
                        <td><asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink></td>
                      </tr>
                    </ItemTemplate>
                  </asp:Repeater>
                </table>
              </div>
            </div>
          </AlternatingItemTemplate>
        </asp:Repeater>
      </div>
      <div style="width:650px;float:right;">
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
      </div>
       
    </div>
    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MasterPage : System.Web.UI.MasterPage
{
    DB db = new DB();
    string Query = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Repeater1ItemDataBound();
            Repeater2ItemDataBound();
        }
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Literal l = e.Item.FindControl("Literal1") as Literal;
            l.Text = DataBinder.Eval(e.Item.DataItem, "title").ToString();
        }
    }

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HyperLink h = e.Item.FindControl("HyperLink1") as HyperLink;
            h.Text = DataBinder.Eval(e.Item.DataItem, "subTitle").ToString();
            h.NavigateUrl = DataBinder.Eval(e.Item.DataItem, "connectTo").ToString();
            h.Target = "_blank";
        }
    }

    private void Repeater1ItemDataBound()
    {
        Query = "SELECT * FROM [title]";
        DataTable table = db.GetDataTable(Query);
        Repeater1.DataSource = table;
        Repeater1.DataBind();
    }

    private void Repeater2ItemDataBound()
    {
        foreach (RepeaterItem item in Repeater1.Items)
        {
            Literal l = item.FindControl("Literal1") as Literal;
            Repeater Repeater2 = item.FindControl("Repeater2") as Repeater;
            Query = "SELECT A.* FROM [subTitle] A,[title] B WHERE A.[titleID]=B.[id] AND B.[title]='" + l.Text + "'";
            DataTable table = db.GetDataTable(Query);
            Repeater2.DataSource = table;
            Repeater2.DataBind();
        }
    }
}