动态创建DataGrid 列排序功能的实现

动态创建的控件在回调的时候,将会被清空,所以事件更不能被执行,解决的方法是在LOAD事件里从新加载一次
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  WebApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for WebForm1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// Put user code to initialize the page here
InBlock.gif
InBlock.gif            
//            string f = @"\\10.1.22.77\test";
InBlock.gif            
//
InBlock.gif            
//            foreach (string s in System.IO.Directory.GetFiles(f))
InBlock.gif            
//            {
InBlock.gif            
//                Response.Write(s);
InBlock.gif            
//                Response.Write("<br>");
InBlock.gif            
//            }
InBlock.gif

InBlock.gif            
if(Page.IsPostBack)
InBlock.gif                CreateDataGrid();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            CreateDataGrid();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
protected string SortOrder 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ViewState["DataGrid1SortOrder"== null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    ViewState[
"DataGrid1SortOrder"= "ASC";
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return (string) ViewState["DataGrid1SortOrder"];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ ViewState["DataGrid1SortOrder"= value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt
=(( (DataGrid)source).DataSource) as DataTable ;
InBlock.gif            
string SortField;
InBlock.gif            
if(SortOrder == "ASC"
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SortField 
= e.SortExpression + " DESC";
InBlock.gif                SortOrder 
=  "DESC";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SortField 
= e.SortExpression + " ASC";
InBlock.gif                SortOrder 
=  "ASC";
ExpandedSubBlockEnd.gif            }

InBlock.gif            dt.DefaultView.Sort
=SortField;
InBlock.gif            DataGrid1.DataSource
=dt;
InBlock.gif            DataGrid1.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void CreateDataGrid()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt
=new DataTable ();
InBlock.gif            dt.Columns.Add(
"ID");
InBlock.gif            dt.Columns.Add(
"Name");
InBlock.gif            
for(int i=0;i<10;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataRow dr
=dt.NewRow();
InBlock.gif                dr[
0]=i;
InBlock.gif                dr[
1]="Name"+i;
InBlock.gif                dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif            }

InBlock.gif                     
InBlock.gif            BoundColumn dcID
=new BoundColumn();
InBlock.gif            dcID.HeaderText
="ID";
InBlock.gif            dcID.DataField
="ID";
InBlock.gif            dcID.SortExpression
="ID";
InBlock.gif            dcID.Visible
=true;
InBlock.gif
InBlock.gif        
InBlock.gif            
if(this.DataGrid1.Columns.Count<2)            
InBlock.gif            
this.DataGrid1.Columns.Add(dcID);            
InBlock.gif
InBlock.gif            DataGrid1.DataSource
=dt;
InBlock.gif            DataGrid1.DataBind();
InBlock.gif
ExpandedSubBlockEnd.gif            DataGrid1.SortCommand
+=new DataGridSortCommandEventHandler(DataGrid1_SortCommand);}

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif <% @ Page language = " c# "  Codebehind = " WebForm1.aspx.cs "  AutoEventWireup = " false "  Inherits = " WebApplication1.WebForm1 "   %>
None.gif
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
None.gif        
< meta name = " GENERATOR "  Content = " Microsoft Visual Studio .NET 7.1 " >
None.gif        
< meta name = " CODE_LANGUAGE "  Content = " C# " >
None.gif        
< meta name = " vs_defaultClientScript "  content = " JavaScript " >
None.gif        
< meta name = " vs_targetSchema "  content = " http://schemas.microsoft.com/intellisense/ie5 " >
None.gif    
</ HEAD >
None.gif    
< body MS_POSITIONING = " GridLayout " >
None.gif        
< form id = " Form1 "  method = " post "  runat = " server " >
None.gif            
< asp:DataGrid id = " DataGrid1 "  style = " Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 112px "
None.gif                runat
= " server "  AutoGenerateColumns = " False "  AllowSorting = " True " >
None.gif                
< Columns >
None.gif                    
< asp:BoundColumn DataField = " Name "  SortExpression = " Name "  HeaderText = " Name  " ></ asp:BoundColumn >
None.gif                
</ Columns >
None.gif            
</ asp:DataGrid >
None.gif            
< asp:Button id = " Button1 "  style = " Z-INDEX: 102; LEFT: 496px; POSITION: absolute; TOP: 128px "  runat = " server "
None.gif                Text
= " Button " ></ asp:Button >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >

转载于:https://www.cnblogs.com/mediar/archive/2007/07/06/808779.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值