查詢顯示全部數據

​在顯示全部數據的基礎上,增加了“查詢”功能,但是不分頁。

一、前臺代碼

<code>

<

%@

 

Page

 

Language

=

"C#"

 

AutoEventWireup

=

"true"

 

CodeBehind

=

"query.aspx.cs"

 

Inherits

=

"guolei.Web.query"

 %>

<

html

 

xmlns

=

"http://www.w3.org/1999/xhtml"

>

<

head

 

id

=

"Head1"

 

runat

=

"server"

>    

<

title

>Title

title

>    

<

style

 

type

=

"text/css"

>

       *       {            

margin

0

;            

padding

0

;       }        

table

.gridtable

      {            

width

80%

;            

font-family

: verdana,arial,sans-serif;            

font-size

11px

;            

color

#333333

;            

border-width

1px

;            

border-color

#666666

;            

border-collapse

: collapse;       }        

table

.gridtable

 

th

      {            

border-width

1px

;            

padding

8px

;            

border-style

: solid;            

border-color

#666666

;       }        

table

.gridtable

 

td

      {            

border-width

1px

;            

padding

8px

;            

border-style

: solid;            

border-color

#666666

;            

background-color

#ffffff

;            

text-align

: center;       }    

style

>

head

>

<

body

>    

<

form

 

id

=

"form1"

 

runat

=

"server"

>    

<

asp:TextBox

 

ID

=

"txtKeywords"

 

runat

=

"server"

 

CssClass

=

"keyword"

 />    

<

asp:LinkButton

 

ID

=

"lbtnSearch"

 

runat

=

"server"

 

CssClass

=

"btn-search"

 

OnClick

=

"btnSearch_Click"

>查詢

asp:LinkButton

>    

<

asp:Button

 

ID

=

"btn_all"

 

runat

=

"server"

 

OnClick

=

"btn_all_Click"

 

Text

=

"顯示所有"

 />    

<

div

>        

<

asp:Repeater

 

ID

=

"RepList1"

 

runat

=

"server"

>            

<

HeaderTemplate

>                

<

table

 

class

=

"gridtable"

>                    

<

thead

>                        

<

tr

>                            

<

th

 

style

=

"width: 100px;"

>                                順序號                            

th

>                            

<

th

 

style

=

"width: 100px;"

>                                ID                            

th

>                            

<

th

 

style

=

"width: 270px;"

>                                標題                            

th

>                            

<

th

 

style

=

"width: 270px;"

>                                時間                            

th

>                        

tr

>                    

thead

>            

HeaderTemplate

>            

<

ItemTemplate

>                

<

tbody

>                    

<

tr

>                        

<

td

>                            

<

%#this.RepList1.Items.Count

 + 

1

%>                        

td

>                        

<

td

>                            

<

%#Eval("id")%

>                        

td

>                        

<

td

>                            

<

%#Eval("title")%

>                        

td

>                        

<

td

>                            

<

%#Eval("add_time")%

>                        

td

>                    

tr

>            

ItemTemplate

>            

<

FooterTemplate

>                

tbody

table

>            

FooterTemplate

>        

asp:Repeater

>    

div

>    

<

div

 

class

=

"pull-right"

>    

div

>    

form

>

body

>

html

>/<code>

二、後臺代碼

<code>

using

 System;

using

 System.Collections.Generic;

using

 System.Linq;

using

 System.Web;

using

 System.Web.UI;

using

 System.Web.UI.WebControls;

namespace

 guolei.Web {    

public

 partial 

class

 

query

 : System.Web.UI.Page   {        

protected

 

void

 

Page_Load

(object sender, EventArgs e)

      {            

string

 keywords = Request.QueryString[

"keywords"

];                        

if

 (!Page.IsPostBack)           {                txtKeywords.Text =keywords;                RepList1.DataSource = 

new

 DAL.title().GetList(keywords);                RepList1.DataBind();                Response.Write(

new

 DAL.title().GetCount(keywords) );           }       }            

protected

 

void

 

btnSearch_Click

(object sender, EventArgs e)

      {            Response.Redirect(

"query.aspx?keywords="

 + txtKeywords.Text);       }        

protected

 

void

 

btn_all_Click

(object sender, EventArgs e)

      {            Response.Redirect(

"query.aspx"

);       }   } }/<code>

三、DAL數據訪問代碼

<code> 
       

public

 DataTable 

GetList

(

string

 strWhere)       {            StringBuilder strSql = 

new

 StringBuilder();            strSql.Append(

"select * from title where"

);            strSql.Append(CombSqlTxt(strWhere));            

return

 

new

 SqlHelper().ExecuteQuery(strSql.ToString(), CommandType.Text);       }                 

public

 

int

 

GetCount

(

string

 strWhere)       {            StringBuilder strSql = 

new

 StringBuilder();            strSql.Append(

"select count(*) from title where"

);            strSql.Append(CombSqlTxt(strWhere));            

return

 Convert.ToInt32(

new

 SqlHelper().ExecuteScalar(strSql.ToString(), CommandType.Text));       }/<code>
<code> 
       

protected

 

string

 

CombSqlTxt

(

string

 _keywords)       {            StringBuilder strTemp = 

new

 StringBuilder();            

if

 (!

string

.IsNullOrEmpty(_keywords))           {                strTemp.Append(

" title like '%"

 + _keywords + 

"%'"

);           }            

else

          {                strTemp.Append(

" 1=1"

);           }            

return

 strTemp.ToString();       }/<code>


分享到:


相關文章: