同一個頁面內根據分類查詢

Proj.aspx:


 <asp:ListView ID="ListView1" runat="server"
                    GroupItemCount="6" style="text-align: center"
                    >

        <LayoutTemplate >
        <table id="Table1" runat ="server" style ="width :98%; height :auto ">
        <tr runat ="server" id="groupPlaceholder" style ="width :50%; height :auto "></tr>
        </table>
        </LayoutTemplate>

        <GroupTemplate >
        <tr id="Tr1" runat ="server">
        <td runat ="server" id="itemPlaceholder"></td>
        </tr>
        </GroupTemplate>

        <ItemTemplate >
        <td id="Td1" runat ="server" >
        <table style="border: 1px ; height: 113px; width: 100%; text-align: center;">
        <tr>
            <td colspan="2" style="width: 174px; vertical-align :middle ;text-align: center; ">
                <img alt="截圖" height="110" src="image/pic/<%#Eval("圖片") %>" width="100" style="padding: 2px" /></td>
                </tr>
                <tr>
             <td colspan="2"> <%# Intercept(Eval("名稱").ToString ())%></td></tr>
             <td><a href ="<%#Eval("主頁地址")%>" style="border: 2px solid #FF00FF; background-color: #CCCCCC;">轉到>></a></td>
             <td><a href ="Proj_body.aspx?id=<%#Eval("貨號")%>" style="border: 2px solid #FF00FF; background-color: #CCCCCC;">詳情>></a></td> 
        </tr>
          </table>
       </td>
        </ItemTemplate>

                </asp:ListView>


                <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1"
                    PageSize="30">
                    <Fields>
                        <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                            ShowNextPageButton="False" />
                        <asp:NumericPagerField ButtonType="Button" />
                        <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
                            ShowPreviousPageButton="False" />
                    </Fields>
                </asp:DataPager>

 

 

Class1.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data .OleDb ;
using System.Data ;


namespace Mane
{
    public class Class1
    {
        public  OleDbConnection strcon(string path)
        {
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path );
            return con;
        }

        public DataTable selectTable(string sqltext, OleDbConnection con)
        {
            DataTable dt = new DataTable();
            OleDbDataAdapter adp = new OleDbDataAdapter(sqltext, con);
            adp.Fill(dt);
            return dt;
        }

        public void selectcmd(string sqltext, OleDbConnection con)
        {
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandText = sqltext;
           try
           {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new SystemException(e.Message);
            }
            finally
            {
                con.Close();
            }
           
        }
    }
}

 

Proj.aspx.cs: 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mane;
using System.Data.OleDb;
using System.Data;

namespace Luck28
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string qstr = Request.QueryString["id"];
            if (qstr != null && qstr != "")
            {
                //如果點擊了分類鏈接並傳遞了字符串則查詢
                Class1 cs = new Class1();
                OleDbConnection con = cs.strcon(@Server.MapPath("/App_Data/Proj.mdb"));
                string sql = "SELECT * FROM Proj  WHERE 所屬分類='" + qstr + "' ORDER BY 排名競價 DESC";
                DataTable dt = cs.selectTable(sql, con);
                ListView1.DataSource = dt;
                ListView1.DataBind();
            }
            else
            {
                //如果未點擊鏈接直接進入該網頁
                Class1 cs = new Class1();
                OleDbConnection con = cs.strcon(@Server.MapPath("/App_Data/Proj.mdb"));
                string sql = "SELECT * FROM Proj ORDER BY 排名競價 DESC";
                DataTable dt = cs.selectTable(sql, con);
                ListView1.DataSource = dt;
                ListView1.DataBind();
            }
        }

      

        //將長度大於4的字符串截短
        protected string Intercept(string sInput)
        {
            if (sInput != null && sInput != string.Empty)
            {
                if (sInput.Length > 4)
                    return sInput = sInput.Substring(0, 4) + "...";
                else
                    return sInput;
            }
            return "";
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章