gridview 排序 分页

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;
using  System.Drawing;

public   partial   class  _Default : System.Web.UI.Page 
{
    DataSet ds 
= new DataSet();
    
protected void Page_Load(object sender, EventArgs e)
    
{
        TextBox1.Focus();
        
if (!IsPostBack)
        
{
            ViewState[
"sortExp"= "フィールドID";
            ViewState[
"sortDir"= "Desc";
        }
       
    }

    
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    
{
        
string txt;
        
if (RadioButtonList1.Items[0].Selected == true)
        
{
            
if (TextBox1.Text.ToString() == "")
            
{
                Response.Write(
"<script language='jscript'>alert('Please input search data!');window.history.go(-1);</script>");    
            }

            
else
            
{
                txt 
= TextBox1.Text.ToString();
                
int k = RadioButtonList2.SelectedIndex;
                
switch (k)
                
{
                    
case 0:
                        ViewState[
"parm1"= 1;
                        ViewState[
"parm2"= 1;
                        ViewState[
"txtS"= txt;
                        GetData(txt, 
11);
                        
break;
                    
case 1:
                        ViewState[
"parm1"= 1;
                        ViewState[
"parm2"= 0;
                        ViewState[
"txtS"= txt;
                        GetData(txt, 
10);
                        
break;
                }



            }

        }

        
else if(RadioButtonList1.Items[1].Selected==true)
        
{
            
if (TextBox1.Text.ToString() == "")
            
{
                Response.Write(
"<script language='jscript'>alert('Please input search data!');window.history.go(-1);</script>");
            }

            
else
            
{
                txt 
= TextBox1.Text.ToString();
                ViewState[
"parm1"= 0;
                ViewState[
"parm2"= 0;
                ViewState[
"txtS"= txt;
                GetData(txt, 
00);
            }

         }

         
    }


    
private void GetData(string txtString,int m,int n)
    
{
        
string str = "data source=172.17.254.34;initial catalog=META_DWH;persist security info=False;user id=test;pwd=test";
        SqlConnection cn 
= new SqlConnection(str);
        SqlCommand cmd 
= new SqlCommand("dwh_Search_columns",cn);
        cmd.CommandType 
= CommandType.StoredProcedure;
        cmd.Parameters.Add(
"@FiledName", SqlDbType.VarChar).Value =txtString;
        cmd.Parameters.Add(
"@SFLG", SqlDbType.Int).Value = m;
        cmd.Parameters.Add(
"@LFLG", SqlDbType.Int).Value = n;
        
try
        
{
            cn.Open();
            SqlDataAdapter da 
= new SqlDataAdapter(cmd);
            da.Fill(ds);
            DataView dv 
= new DataView(ds.Tables[0]);
            dv.Sort 
= ViewState["sortExp"+ " " + ViewState["sortDir"];

             
// DataGridColumnCollection dgc = GridView1.Columns;
            if (ds.Tables[0].Rows.Count == 0)
            
{
                 ds.Tables[
0].Rows.Add(ds.Tables[0].NewRow());
                 GridView1.DataSource 
= ds;
                 GridView1.DataBind();
                 
//int columnCount = GridView1.Rows[0].Cells.Count;
                 
//GridView1.Rows[0].Cells.Clear();
                 
//GridView1.Rows[0].Cells.Add(new TableCell());
                 
//GridView1.Rows[0].Cells[0].ColumnSpan = columnCount;
                  GridView1.Rows[0].Cells[0].Text = "No Records Found.";
            }

            
else
            
{
                GridView1.DataSource 
= dv;
                GridView1.DataBind();
            }


        }

        
catch (Exception ex)
        
{
            cn.Close();
            Response.Write(
"<script language='jscript'>alert("+ex.Message+"');window.history.go(-1);</script>");

        
        }

      
    }


    
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    
{
        
string sPage = e.SortExpression;
        
if (ViewState["sortExp"].ToString() == sPage)
        
{
            
if (ViewState["sortDir"].ToString() == "Desc")
                ViewState[
"sortDir"= "ASC";
            
else
                ViewState[
"sortDir"= "Desc";
        }

        
else
        
{
            ViewState[
"sortExp"= e.SortExpression;
        }

        
string s = ViewState["txtS"].ToString();
        
int m, n;
        m 
= Convert.ToInt16(ViewState["parm1"].ToString());
        n 
= Convert.ToInt16(ViewState["parm2"].ToString()); 
        GetData(s, m, n);

 }


    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            e.Row.Attributes.Add(
"onmouseover""currentcolor=this.style.backgroundColor;this.style.backgroundColor='LightSkyBlue',this.style.fontWeight='';");
            e.Row.Attributes.Add(
"onmouseout""this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }


    }


    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        
string s = ViewState["txtS"].ToString();
        
int m, n;
        m 
= Convert.ToInt16(ViewState["parm1"].ToString());
        n 
= Convert.ToInt16(ViewState["parm2"].ToString());
        GetData(s, m, n);
    }


    
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    
{
        
if (RadioButtonList1.Items[1].Selected == true)
        
{
            RadioButtonList2.Items[
1].Selected = true;
            RadioButtonList2.Items[
0].Selected = false;
            
this.RadioButtonList2.Enabled = false;
        }

        
else
        
{
            RadioButtonList2.Items[
1].Selected = false;
            RadioButtonList2.Items[
0].Selected = true;
            
this.RadioButtonList2.Enabled = true;
        }

     

    }


    
protected void button_click(object sender, EventArgs e)
    
{

        
try
        
{
            GridViewRow BottomPagerRow 
= GridView1.BottomPagerRow;
            
int k = Convert.ToInt16((BottomPagerRow.Cells[0].FindControl("textBox_1"as TextBox).Text.ToString());
            
// Response.Write(k);
            GridView1.PageIndex = k - 1;
            
string s = ViewState["txtS"].ToString();
            
int m, n;
            m 
= Convert.ToInt16(ViewState["parm1"].ToString());
            n 
= Convert.ToInt16(ViewState["parm2"].ToString());
            GetData(s, m, n);
        }

        
catch (Exception ex)
        
{

            
return;
        }


    }

}


 前台

 

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " FiledSearch.aspx.cs "  Inherits = " _Default "   %>

<% @ Register Assembly = " System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 "
    Namespace
= " System.Web.UI "  TagPrefix = " asp "   %>

<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > フィールド検索 </ title >
    
< meta http - equiv = " Page-Enter "  content = " revealTrans(duration=8, transition=5) " />
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< table style = " width: 722px " >
            
< tr >
                
< td style = " width: 1px; height: 30px " >
                    
< asp:Label ID = " Label1 "  runat = " server "  Font - Bold = " True "  Font - Size = " 28pt "  ForeColor = " Orange "
                        Text
= " DataWareHouse "  Width = " 251px " ></ asp:Label ></ td >
                
< td style = " width: 53px; height: 30px " >
                
</ td >
            
</ tr >
            
< tr >
                
< td style = " width: 1px " >
                
</ td >
                
< td style = " width: 53px " >
                    
< asp:Label ID = " Label2 "  runat = " server "  Font - Bold = " True "  Font - Size = " 28pt "  ForeColor = " Orange "
                        Text
= " フィールド検索 "  Width = " 251px " ></ asp:Label ></ td >
            
</ tr >
        
</ table >
        
< hr  />
        
< table style = " width: 897px " >
            
< tr >
                
< td style = " width: 2481px; height: 72px; " >
                    
< asp:ScriptManager ID = " ScriptManager1 "  runat = " server " >
                    
</ asp:ScriptManager >
                  
</ td >
                
< td style = " width: 62px;vertical-align:top; height: 72px; " >
                   
< asp:UpdatePanel ID = " UpdatePanel1 "  runat = " server " >
                        
< ContentTemplate >
                            
< asp:RadioButtonList ID = " RadioButtonList1 "  runat = " server "  AutoPostBack = " True "  OnSelectedIndexChanged = " RadioButtonList1_SelectedIndexChanged "
                                Width
= " 144px " >
                                
< asp:ListItem Selected = " True "  Value = " 1 " > フィールド名 </ asp:ListItem >
                                
< asp:ListItem Value = " 2 " > 類型 </ asp:ListItem >
                            
</ asp:RadioButtonList >
                        
</ ContentTemplate >
                       
                    
</ asp:UpdatePanel >
                
</ td >
                
< td style = " width: 29px; vertical-align:middle; height: 72px; " >
                        
< asp:TextBox ID = " TextBox1 "  runat = " server "  Height = " 21px "  Width = " 129px " ></ asp:TextBox >
                    
& nbsp;  & nbsp;  & nbsp; & nbsp; </ td >
                
< td style = " width: 145px;vertical-align:top; height: 72px; " >
                   
< asp:UpdatePanel ID = " UpdatePanel2 "  runat = " server " >
                        
< ContentTemplate >
                   
< asp:RadioButtonList ID = " RadioButtonList2 "  runat = " server "  Width = " 108px "  Height = " 40px " >
                            
< asp:ListItem Selected = " True " > 日本語 </ asp:ListItem >
                            
< asp:ListItem > 英語 </ asp:ListItem >
                        
</ asp:RadioButtonList >
                        
                            
< div style = " z-index: 101; left: 834px; width: 38px; position: absolute; top: 126px;
                                height: 64px " >
                                 < asp:ImageButton ID = " ImageButton1 "  runat = " server "  Height = " 42px "  ImageUrl = " ~/images/button.bmp "
                            Width
= " 93px "  OnClick = " ImageButton1_Click "   />
                            
</ div >
                        
</ ContentTemplate >
                         
< Triggers >
                        
< asp:PostBackTrigger ControlID = " ImageButton1 "   />
                         
</ Triggers >
                        
                    
</ asp:UpdatePanel >
                
</ td >
                
< td style = " width: 381px;vertical-align:middle; height: 72px; " >
                        
</ td >
            
</ tr >
        
</ table >
        
< hr  />
        
< table style = " width: 1186px " >
            
< tr >
                
< td style = " width: 100px; height: 173px " >
                   
</ td >
                
< td style = " width: 1080px; height: 173px; " >
                   
< asp:GridView ID = " GridView1 "  runat = " server "  BackColor = " White "  BorderColor = " #CCCCCC "
                        BorderStyle
= " None "  BorderWidth = " 1px "  CellPadding = " 3 "  OnSorting = " GridView1_Sorting "
                        Width
= " 1080px "  AllowSorting = " True "  AutoGenerateColumns = " False "  PageSize = " 30 "  EmptyDataText = " null "  OnRowDataBound = " GridView1_RowDataBound "  AllowPaging = " True "  OnPageIndexChanging = " GridView1_PageIndexChanging " >
                        
< Columns >
                            
< asp:BoundField HeaderText = " フィールドID "  SortExpression = " フィールドID "  DataField = " フィールドID " >
                            
</ asp:BoundField >
                            
< asp:BoundField HeaderText = " フィールド名 "  SortExpression = " フィールド名 "  DataField = " フィールド名 "   >
                             
</ asp:BoundField >
                            
< asp:BoundField HeaderText = " タイプ "   DataField = " タイプ "  SortExpression = " タイプ "   >
                            
</ asp:BoundField >
                            
< asp:BoundField HeaderText = " データ長 "   DataField = " データ長 "   >
                            
</ asp:BoundField >
                            
< asp:BoundField HeaderText = " テーブルID "   DataField = " テーブルID "   >
                             
</ asp:BoundField >
                            
< asp:BoundField HeaderText = " テーブル名 "  DataField = " テーブル名 "   >
                            
</ asp:BoundField >
                        
</ Columns >
                        
< HeaderStyle BackColor = " #006699 "  Font - Bold = " True "  ForeColor = " White "  HorizontalAlign = " Left "  VerticalAlign = " Middle "  Width = " 1080px "   />
                         
< PagerTemplate >
                            
< asp:LinkButton ID = " LinkButtonFirstPage "  runat = " server "  CommandArgument = " First "  CommandName = " Page "  
                            Visible
= " <%# ((GridView)Container.NamingContainer).PageIndex != 0 %> " > First </ asp:LinkButton >  
                            
< asp:LinkButton ID = " LinkButtonPreviousPage "  runat = " server "  CommandArgument = " Prev "  CommandName = " Page "  
                            Visible
= " <%# ((GridView)Container.NamingContainer).PageIndex != 0 %> " > Previous </ asp:LinkButton >  
                            
< asp:LinkButton ID = " LinkButtonNextPage "  runat = " server "  CommandArgument = " Next "  CommandName = " Page "  
                            Visible
= " <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %> " > Next </ asp:LinkButton >  
                            
< asp:LinkButton ID = " LinkButtonLastPage "  runat = " server "  CommandArgument = " Last "  CommandName = " Page "  
                            Visible
= " <%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %> " > Last </ asp:LinkButton >  
                            
< asp:TextBox  ID = " textBox_1 "  runat = " server "  Width = " 30px "   ></ asp:TextBox >
                            
< asp:Button ID = " button_1 "  runat = " server "  Width = " 40px "  Text = " GO "  OnClick = " button_click "   />
                            
</ PagerTemplate >
                    
</ asp:GridView >
                   
< i > You are viewing page
                    
<%= GridView1.PageIndex  +   1 %>
                    of
                    
<%= GridView1.PageCount %>
                    
</ i >
             
</ td >
            
</ tr >
        
</ table >
         
</ div >
    
</ form >
</ body >
</ html >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值