Asp.net實(shí)現(xiàn)向上向下排序的例子
來源:易賢網(wǎng) 閱讀:1814 次 日期:2014-08-28 10:32:06
溫馨提示:易賢網(wǎng)小編為您整理了“Asp.net實(shí)現(xiàn)向上向下排序的例子”,方便廣大網(wǎng)友查閱!

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

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

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), -- 列表默認(rèn)的排序方式,asc或desc

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

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

)

AS

BEGIN

declare @ordertmp1 int; --臨時(shí)排序值id1

declare @ordertmp2 int; --臨時(shí)排序值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; }

}

///

 

/// 標(biāo)題

///

 

public string Title

{

get { return this._title; }

set { this._title = value; }

}

///

 

/// 描述

///

 

public string Descript

{

get { return this._descript; }

set { this._descript = value; }

}

}

IDAL代碼:

/// 排序

///

/// 表名

/// 排序字段

/// 表主鍵字段

/// 表主鍵字段值

/// 列表默認(rèn)的排序方式,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管理

 

 

  標(biāo)  題: 最多可填寫15個(gè)字

  描  述: 最多可填寫20個(gè)字

上傳小圖: 

 

ErrorMessage="請選擇圖片!">

上傳大圖: 

 

ErrorMessage="請選擇圖片!">

 

  排  序:

 

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

鏈接地址: 

 

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

ControlToValidate="txbUrl" ErrorMessage="填寫的地址不符合規(guī)格"

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("已經(jīng)最前了!");

}

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("已經(jīng)最后了!");

}

else

{

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

this.Bind();

}

}

至此完畢。

更多信息請查看IT技術(shù)專欄

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:Asp.net實(shí)現(xiàn)向上向下排序的例子
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)