asp.net Oracle資料庫左側目錄樹及右側資料繫結及分頁

暖楓無敵發表於2014-06-03

效果圖如下:

 
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="SLGL_List.aspx.cs" Inherits="SLGL_List" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
   <div id="warp">
         <div class="nyl">
    <div class="position"><img src="images/home2.png" />當前位置:水利管理 >> 
                        <span style="color: #0376B9;">
                            <asp:Label Text="" ID="lblPos" runat="server" /></span></div>
    <div class="sideBar">
     <div class="sideBar_title">
      <span><img src="images/noticico.jpg" /></span>
      <h1>水利管理</h1>             </div>
     <div class="sideBar_menu">
    <ul id="leftlist" runat="server">
             </ul>
    </div>
  </div>
    <div class="sidebar_r">
      <div class="ctllf_title"><asp:Label ID="lblMsg" runat="server" class="tab1_bg"></asp:Label></div>
   <div class="ctllf_txt2">
    <ul class="right_mid" id="detailslist" runat="server">
    </ul>
    <div class="pagination">
     共<asp:Label ID="lblTotal" runat="server"></asp:Label>條記錄 <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>/<asp:Label ID="lblPageNumber" runat="server"></asp:Label> 頁  
     <asp:LinkButton id="aFirst" runat="server" CausesValidation="false" OnCommand="aFirst_OnCommand">首頁</asp:LinkButton> 
     <asp:LinkButton id="aPre" runat="server" CausesValidation="false" OnCommand="aPre_OnCommand">上一頁</asp:LinkButton> 
     <asp:LinkButton id="aNext" runat="server" CausesValidation="false" OnCommand="aNext_OnCommand">下一頁</asp:LinkButton> 
     <asp:LinkButton id="aLast" runat="server"  CausesValidation="false" OnCommand="aLast_OnCommand">尾頁</asp:LinkButton> 
      第
     <asp:DropDownList ID="ddlPageNumber" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPageNumber_OnSelectedIndexChaged"></asp:DropDownList>
     頁
    </div>     
   </div>
    </div>
  </div>
  <div class="clear"></div>
</div>
<div class="conbotbg"></div>
<asp:HiddenField ID="hf" runat="server" Value="T1" />
<script type="text/javascript">
    $(function () {
        $("#ContentPlaceHolder1_leftlist >li").each(function (i) {
            if ($(this).attr("id") == $("#<%=hf.ClientID%>").val()) {
                $(this).find("a").css("color", "red");
            }
            else {
                $(this).find("a").css("color", "black");
            }
        });
    });
</script>
</asp:Content>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using USTC;
using System.Data;
using System.Text;
using System.Web.UI.HtmlControls;

public partial class SLGL_List : System.Web.UI.Page
{
    OracleDM dm = new OracleDM();

    public string TYPE = ""; //型別
    public int CURRENT_PAGE_INDEX = 1; //當前頁數
    public int PAGE_SIZE = 11; //每頁顯示記錄條數
    public int TOTAL_DATA = 0; //總記錄條數
    public int PAGE_NUMBER = 1; //頁數(計算值)

    /// <summary>
    /// 內容頁中找母版頁中的控制元件,並設定顏色
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void Page_LoadComplete(Object sender, EventArgs e)
    {
        (Master.FindControl("mT1") as HtmlAnchor).Attributes.Remove("class");
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindKSXX();//先動態繫結科室資訊
            string type = "";
            if (Request.QueryString["ID"] != null)
            {
                string Id = Request.QueryString["ID"].ToString();
                type = getType(Id);
            }
            if (Request.QueryString["type"] != null)
            {
                type = Server.UrlDecode(Request.QueryString["type"].ToString());
            }
            else
            {
                type = "水利管理";
            }
            if (Request.QueryString["pid"] != null)
            {
                hf.Value = Request.QueryString["pid"].ToString();
            }

            ViewState["TYPE"] = type;
            this.lblPos.Text = type;
            this.lblMsg.Text = type;
            ViewState["CURRENT_PAGE_INDEX"] = CURRENT_PAGE_INDEX;
            getTotalRecordNumbers(type);
            BindContentByMenuAndPageSize(type, PAGE_SIZE, 1); //預設顯示當前類別的第一頁資料
            this.lblTotal.Text = TOTAL_DATA.ToString(); //總記錄條數
            PAGE_NUMBER = (int)Math.Ceiling(((decimal)TOTAL_DATA) / ((decimal)PAGE_SIZE));
            ViewState["PAGE_NUMBER"] = PAGE_NUMBER;
            this.lblPageNumber.Text = PAGE_NUMBER.ToString(); //總記錄頁數
            this.lblCurrentPage.Text = "1";
            if (TOTAL_DATA == 0)
            {
                this.lblPageNumber.Text = "1";
            }
            if (this.lblCurrentPage.Text == this.lblPageNumber.Text)
            {
                //就一頁的話,四個按鈕均不可用
                this.aFirst.Enabled = false;
                this.aPre.Enabled = false;
                this.aNext.Enabled = false;
                this.aLast.Enabled = false;
            }
            BindDropDownList();
        }
    }


    public string getType(string Id)
    {
        string type = "";
        string sql = "select * from T_SMALLCLASS where BIGID=(select ID from T_BIGCLASS where  TITLE='水利管理') and ID='" + Id + "'";
        DataSet ds = dm.getsql(sql);
        if (ds.Tables[0].Rows.Count > 0 && ds != null)
        {
            type = ds.Tables[0].Rows[0]["TITLE"].ToString().Trim();
        }
        else
        {
            type = "水利管理";
        }
        return type;
    }

    /// <summary>
    /// 防汛抗旱子目錄選單動態繫結
    /// </summary>
    public void BindKSXX()
    {
        StringBuilder sb = new StringBuilder();
        string sql = "select * from T_SMALLCLASS where BIGID=(select ID from T_BIGCLASS where  TITLE='水利管理')";
        DataSet ds = dm.getsql(sql);
        int index = 1;
        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRowView drv in ds.Tables[0].DefaultView)
            {
                sb.Append("<li id=\"T" + index + "\" runat=\"server\"><a title=\"" + drv["TITLE"].ToString().Trim() + "\" href=\"SLGL_List.aspx?type=" + Server.UrlEncode(drv["TITLE"].ToString().Trim()) + "&mId=mT7&pid=T" + index + "\">" + drv["TITLE"].ToString().Trim() + "</a></li>");
                index++;
            }
        }
        else
        {

        }
        this.leftlist.InnerHtml = sb.ToString();
    }

    /// <summary>
    /// 獲取總的記錄條數
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public int getTotalRecordNumbers(string type)
    {
        string sql = @"select ID, TITLE,S_Content,ReleaseTime from (select  a.ID, a.SMALLID,a.Title,a.RELEASETIME RELEASETIME , b.Title bTitle,b.s_Content  from  t_leastclass  a left join t_smallclass   b on a.SMALLID=b.ID and b.Title='" + type + @"' where b.ID in(select a.id from  t_smallclass a left join t_bigclass b on a.BigID=b.id where b.Title='水利管理') order by a.RELEASETIME desc)";
        DataSet ds = dm.getsql(sql);
        TOTAL_DATA = ds.Tables[0].Rows.Count;
        return TOTAL_DATA;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="type"></param>
    /// <param name="pageSize"></param>
    /// <param name="pageIndex"></param>
    public void BindContentByMenuAndPageSize(string type, int pageSize, int pageIndex)
    {
        StringBuilder sb = new StringBuilder();
        string sql = @"select ID, TITLE,S_Content,ReleaseTime,orderNumber  from (select  a.ID, a.SMALLID,a.Title,a.RELEASETIME RELEASETIME , b.Title bTitle,b.s_Content,row_number() over(order by a.RELEASETIME desc) orderNumber  from         
   t_leastclass  a left join t_smallclass   b on a.SMALLID=b.ID and b.Title='" + type + @"' where b.ID in(select a.id from  t_smallclass a left    
             join t_bigclass b on a.BigID=b.id where b.Title='水利管理') order by a.RELEASETIME desc)where orderNumber between " + ((pageIndex - 1) * pageSize + 1) + " and  " + pageIndex * pageSize + "";
        DataSet ds = dm.getsql(sql);
        foreach (DataRowView drv in ds.Tables[0].DefaultView)
        {
            sb.Append("<li><a title=\"" + drv["TITLE"].ToString().Trim() + "\" href=\"lashgc.aspx?ID=" + drv["ID"] + "&mId=mT7\" target=\"_blank\"><div class=\"f_time\">" + DateTime.Parse(drv["ReleaseTime"].ToString()).ToString("yyyy-MM-dd") + "</div>" + drv["TITLE"].ToString() + "</a></li>");
        }
        this.detailslist.InnerHtml = sb.ToString();
    }


    /// <summary>
    /// 首頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void aFirst_OnCommand(object sender, CommandEventArgs e)
    {
        this.aFirst.Enabled = false;
        this.aPre.Enabled = false;
        this.aLast.Enabled = true;
        this.aNext.Enabled = true;

        BindContentByMenuAndPageSize(ViewState["TYPE"].ToString(), PAGE_SIZE, int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()));
        this.lblCurrentPage.Text = "1";
        ViewState["CURRENT_PAGE_INDEX"] = "1";
    }

    /// <summary>
    /// 上一頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void aPre_OnCommand(object sender, CommandEventArgs e)
    {

        ViewState["CURRENT_PAGE_INDEX"] = int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()) - 1;
        this.lblCurrentPage.Text = int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()).ToString();
        BindContentByMenuAndPageSize(ViewState["TYPE"].ToString(), PAGE_SIZE, int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()));
        if (int.Parse(this.lblCurrentPage.Text) == int.Parse(this.lblPageNumber.Text))
        {
            this.aLast.Enabled = false;
            this.aNext.Enabled = false;
        }
        else
        {
            this.aLast.Enabled = true;
            this.aNext.Enabled = true;
        }
        if (Convert.ToInt32(lblCurrentPage.Text) == 1)
        {
            this.aPre.Enabled = false;
            this.aFirst.Enabled = false;
        }
        else
        {
            this.aPre.Enabled = true;
            this.aFirst.Enabled = true;
        }
        this.ddlPageNumber.ClearSelection();
        this.ddlPageNumber.Items.FindByText(this.lblCurrentPage.Text.Trim()).Selected = true;
    }

    /// <summary>
    /// 下一頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void aNext_OnCommand(object sender, CommandEventArgs e)
    {

        ViewState["CURRENT_PAGE_INDEX"] = int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()) + 1;
        this.lblCurrentPage.Text = int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()).ToString();
        BindContentByMenuAndPageSize(ViewState["TYPE"].ToString(), PAGE_SIZE, int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()));
        if (int.Parse(this.lblCurrentPage.Text) == int.Parse(this.lblPageNumber.Text))
        {
            this.aLast.Enabled = false;
            this.aNext.Enabled = false;
        }
        else
        {
            this.aLast.Enabled = true;
            this.aNext.Enabled = true;
        }
        if (Convert.ToInt32(lblCurrentPage.Text) + 1 > 1)
        {
            this.aPre.Enabled = true;
            this.aFirst.Enabled = true;
        }
        else
        {
            this.aPre.Enabled = false;
            this.aFirst.Enabled = false;
        }
        this.ddlPageNumber.ClearSelection();
        this.ddlPageNumber.Items.FindByText(this.lblCurrentPage.Text.Trim()).Selected = true;
    }

    /// <summary>
    /// 尾頁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void aLast_OnCommand(object sender, CommandEventArgs e)
    {
        ViewState["CURRENT_PAGE_INDEX"] = ViewState["PAGE_NUMBER"];
        this.aLast.Enabled = false;
        this.aNext.Enabled = false;
        this.aFirst.Enabled = true;
        this.aPre.Enabled = true;
        BindContentByMenuAndPageSize(ViewState["TYPE"].ToString(), PAGE_SIZE, int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()));
        this.lblCurrentPage.Text = int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()).ToString();
    }

    public string MyContent3(string input)
    {
        string ret = "";

        if (string.IsNullOrEmpty(input))
        {
            ret = "";
        }
        else
        {
            if (input.Length > 8)
            {
                ret = input.Substring(0, 10);
            }
            else
            {
                ret = input;
            }
        }
        return ret;
    }

    public void BindDropDownList()
    {
        int page = int.Parse(ViewState["PAGE_NUMBER"].ToString());
        if (page == 0)
        {
            //沒有記錄
            ddlPageNumber.Items.Add(new ListItem("1"));
        }
        else
        {
            //有記錄
            for (int i = 1; i <= page; i++)
            {
                ddlPageNumber.Items.Add(new ListItem(i.ToString()));
            }
        }

    }

    public void ddlPageNumber_OnSelectedIndexChaged(object sender, EventArgs e)
    {
        ViewState["CURRENT_PAGE_INDEX"] = (sender as DropDownList).SelectedItem.Text;
        this.lblCurrentPage.Text = int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()).ToString();
        if (int.Parse(this.lblCurrentPage.Text) == int.Parse(this.lblPageNumber.Text))
        {
            this.aLast.Enabled = false;
            this.aNext.Enabled = false;
        }
        else
        {
            this.aLast.Enabled = true;
            this.aNext.Enabled = true;
        }
        if (Convert.ToInt32(lblCurrentPage.Text) + 1 > 1)
        {
            this.aPre.Enabled = true;
            this.aFirst.Enabled = true;
        }
        else
        {
            this.aPre.Enabled = false;
            this.aFirst.Enabled = false;
        }
        BindContentByMenuAndPageSize(ViewState["TYPE"].ToString(), PAGE_SIZE, int.Parse(ViewState["CURRENT_PAGE_INDEX"].ToString()));
    }
}

 

 

 

 

相關文章