程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> Asp.net實現向上向下排序的例子

Asp.net實現向上向下排序的例子

編輯:關於ASP.NET

       工作中一些常用到的代碼記錄下來,方便自己查找也方便其他需要人士參考。

      廢話不多說,這是一個向上向下排序的功能,首先使用存儲過程 整好 如下:

      SQL:

      -- =============================================

      -- Author:

      -- Create date:

      -- Description:

      -- =============================================

      ALTER PROCEDURE [dbo].[sp_BannerOrder]

      -- Add the parameters for the stored procedure here

      (

      @tablename nvarchar(50), --表名

      @colname nvarchar(50), --排序字段

      @keyid nvarchar(50), --表主鍵字段

      @keyidvalue int, --表主鍵字段值1

      @order nvarchar(20), -- 列表默認的排序方式,asc或desc

      @orderDirection nvarchar(20), --排序方向,up或down

      @where nvarchar(2000) --查詢條件

      )

      AS

      BEGIN

      declare @ordertmp1 int; --臨時排序值id1

      declare @ordertmp2 int; --臨時排序值id2

      declare @tmpkeyidvaule nvarchar(50);

      declare @sql nvarchar(2000);

      DECLARE @ParmDefinition nvarchar(500);

      DECLARE @ParmDefinition2 nvarchar(500);

      if @order='asc'

      begin

      SET @sql = N'SELECT @ordertmp1OUT='+@colname+' from '+@tablename+' where '+@keyid+'='+cast(@keyidvalue as nvarchar(50));

      SET @ParmDefinition = N'@ordertmp1OUT nvarchar(20) OUTPUT';

      EXECUTE sp_executesql @sql, @ParmDefinition, @ordertmp1OUT=@ordertmp1 OUTPUT;

      if @orderDirection='up'

      begin

      SET @sql = N'SELECT top 1 @ordertmp2OUT='+@colname+',@tmpkeyidvauleOUT='+@keyid+' from '+@tablename+' where '+@colname+'<'+cast(@ordertmp1 as nvarchar(50))+' and '+@where+' order by '+@colname+' desc';

      end

      else

      begin

      SET @sql = N'SELECT top 1 @ordertmp2OUT='+@colname+',@tmpkeyidvauleOUT='+@keyid+' from '+@tablename+' where '+@colname+'>'+cast(@ordertmp1 as nvarchar(50))+' and '+@where+' order by '+@colname+' asc';

      end

      SET @ParmDefinition = N'@ordertmp2OUT nvarchar(20) OUTPUT, @tmpkeyidvauleOUT nvarchar(20) OUTPUT';

      EXECUTE sp_executesql @sql, @ParmDefinition, @ordertmp2OUT=@ordertmp2 OUTPUT, @tmpkeyidvauleOUT=@tmpkeyidvaule OUTPUT;

      end

      else

      begin

      SET @sql = N'SELECT @ordertmp1OUT='+@colname+' from '+@tablename+' where '+@keyid+'='+cast(@keyidvalue as nvarchar(50));

      SET @ParmDefinition = N'@ordertmp1OUT nvarchar(20) OUTPUT';

      EXECUTE sp_executesql @sql, @ParmDefinition, @ordertmp1OUT=@ordertmp1 OUTPUT;

      if @orderDirection='up'

      begin

      SET @sql = N'SELECT top 1 @ordertmp2OUT='+@colname+',@tmpkeyidvauleOUT='+@keyid+' from '+@tablename+' where '+@colname+'>'+cast(@ordertmp1 as nvarchar(50))+' and '+@where+' order by '+@colname+' asc';

      end

      else

      begin

      SET @sql = N'SELECT top 1 @ordertmp2OUT='+@colname+',@tmpkeyidvauleOUT='+@keyid+' from '+@tablename+' where '+@colname+'<'+cast(@ordertmp1 as nvarchar(50))+' and '+@where+' order by '+@colname+' desc';

      end

      SET @ParmDefinition = N'@ordertmp2OUT nvarchar(20) OUTPUT, @tmpkeyidvauleOUT nvarchar(20) OUTPUT';

      EXECUTE sp_executesql @sql, @ParmDefinition, @ordertmp2OUT=@ordertmp2 OUTPUT, @tmpkeyidvauleOUT=@tmpkeyidvaule OUTPUT;

      end

      set @sql = 'update '+@tablename+' set '+@colname+'='+cast(@ordertmp2 as nvarchar(50))+' where '+@keyid+'='+cast(@keyidvalue as nvarchar(50));

      set @sql = @sql + ' update '+@tablename+ ' set '+@colname+'='+cast(@ordertmp1 as nvarchar(50))+' where '+@keyid+'='+cast(@tmpkeyidvaule as nvarchar(50));

      --select @ordertmp1,@ordertmp2,@tmpkeyidvaule,@sql

      exec(@sql);

      END

      MODEL:

      public class Banner

      {

      public Banner()

      { }

      private int _id;

      private string _smallPic;

      private string _bigPic;

      private int _orderid;

      private string _url;

      private string _title;

      private string _descript;

      //字增量ID

      public int ID

      {

      get { return this._id; }

      set { this._id = value; }

      }

      //BANNER小圖

      public string SmallPic

      {

      get { return this._smallPic; }

      set { this._smallPic = value; }

      }

      ///

     

      /// BANNER大圖

      ///

     

      public string BigPic

      {

      get { return this._bigPic; }

      set { this._bigPic = value; }

      }

      ///

     

      /// 排序ID

      ///

     

      public int OrderId

      {

      get { return this._orderid; }

      set { this._orderid = value; }

      }

      ///

     

      /// URL地址

      ///

     

      public string Url

      {

      get { return this._url; }

      set { this._url = value; }

      }

      ///

     

      /// 標題

      ///

     

      public string Title

      {

      get { return this._title; }

      set { this._title = value; }

      }

      ///

     

      /// 描述

      ///

     

      public string Descript

      {

      get { return this._descript; }

      set { this._descript = value; }

      }

      }

      IDAL代碼:

      /// 排序

      ///

      /// 表名

      /// 排序字段

      /// 表主鍵字段

      /// 表主鍵字段值

      /// 列表默認的排序方式,asc或desc

      /// 排序方向,up或down

      /// 條件

      ///

      int Order(string table,string colname,string keyid,int keyidvalue,string order,string orderDirection,string whe);

      SQLDAL代碼:

      public int Order(string table,string colname,string keyid,int keyidvalue,string order,string orderDirection,string whe)

      {

      SqlParameter[] paras = {

      new SqlParameter("@tablename", table),

      new SqlParameter("@colname",colname),

      new SqlParameter("@keyid",keyid),

      new SqlParameter("@keyidvalue",keyidvalue),

      new SqlParameter("@order",order),

      new SqlParameter("@orderDirection",orderDirection),

      new SqlParameter("@where",whe)

      };

      return Convert.ToInt32( SqlHelper.ExecuteNonQuery(Configuration.ConnectionString, CommandType.StoredProcedure, "sp_BannerOrder", paras));

      BLL代碼:

      public int Order(string table, string colname, string keyid, int keyidvalue, string order, string orderDirection, string whe)

      {

      return bner.Order(table, colname, keyid, keyidvalue, order, orderDirection, whe);

      }

      WEB:

      aspx代碼:

      <%@ Page Language="C#" MasterPageFile="~/Admin/Masterpage/Page.Master" AutoEventWireup="true" CodeBehind="Banner.aspx.cs" Inherits="YXShop.Web.Admin.Article.Banner" %>

      <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

      <%@ Register assembly="FredCK.FCKeditorV2" namespace="FredCK.FCKeditorV2" tagprefix="FCKeditorV2" %>

      前台Banner管理

     

     

      標  題: 最多可填寫15個字   描  述: 最多可填寫20個字 上傳小圖:

     

      

      

      ErrorMessage="請選擇圖片!">

      

    上傳大圖:

     

      

      

      ErrorMessage="請選擇圖片!">

      

     

        排  序:

      

     

      

      

      ControlToValidate="txborder" ErrorMessage="不能為空!">

      

    鏈接地址:

     

      

      ControlToValidate="txbUrl" ErrorMessage="不能為空!">

      

      ControlToValidate="txbUrl" ErrorMessage="填寫的地址不符合規格"

      ValidationExpression="http(s)?://([w-]+.)+[w-]+(/[w- ./?%&=]*)?">

      

     

      

      onclick="btnOK_Click" />

      

      


     

     

      

     

      

     

     

      

     

     

      

      onrowdatabound="gvwBannner_RowDataBound" BackColor="White" DataKeyNames="ID"

      BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3"

      GridLines="Horizontal" onrowcancelingedit="gvwBannner_RowCancelingEdit"

      onrowdeleting="gvwBannner_RowDeleting" onrowediting="gvwBannner_RowEditing"

      onrowupdating="gvwBannner_RowUpdating" AllowSorting="True" Width="551px">

      

      

      

      

      

      

      

      

      

      

      

      

      

      

      <%--

      DataAlternateTextFormatString="這是{0}的圖" DataImageUrlField="smallPic"

      HeaderText="圖片">

      

      --%>

      

      

      

      

      

      

      

      Text="向上" onclick="Button1_Click" />

      

      

      

      

      

      Text="向下" OnClick="Button2_Click" />

      

      

      

      

      

      

      CommandName="Delete" Text="刪除" OnClientClick="return confirm('是否刪除?');" >

      

      

      

      

      

      

      

      

      

      

      

      CS代碼:

      protected void Button1_Click(object sender, EventArgs e)

      {

      int keyidvlue = Convert.ToInt32(gvwBannner.DataKeys[((GridViewRow)((Button)sender).NamingContainer).RowIndex].Value); //獲取主鍵值

      int row = Convert.ToInt32(((GridViewRow)((Button)sender).NamingContainer).RowIndex);//獲取行號

      if (row == 0)

      {

      YXShop.Common.alert.show("已經最前了!");

      }

      else

      {

      bll.Order("banner", "orderid", "id", keyidvlue, "desc", "up", "1=1");

      this.Bind();

      }

      }

      //向下

      protected void Button2_Click(object sender, EventArgs e)

      {

      //int keyidvlue = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;

      int keyidvlue = Convert.ToInt32(gvwBannner.DataKeys[((GridViewRow)((Button)sender).NamingContainer).RowIndex].Value);

      int row1 = Convert.ToInt32(((GridViewRow)((Button)sender).NamingContainer).RowIndex);

      if (row1 == this.gvwBannner.Rows.Count-1)

      {

      YXShop.Common.alert.show("已經最後了!");

      }

      else

      {

      bll.Order("banner", "orderid", "id", keyidvlue, "desc", "down", "1=1");

      this.Bind();

      }

      }

      至此完畢。

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved