惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
云风的 BLOG
云风的 BLOG
Microsoft Security Blog
Microsoft Security Blog
WordPress大学
WordPress大学
GbyAI
GbyAI
C
Check Point Blog
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
Jina AI
Jina AI
博客园 - 【当耐特】
U
Unit 42
月光博客
月光博客
腾讯CDC
Y
Y Combinator Blog
小众软件
小众软件
博客园_首页
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
T
Tailwind CSS Blog

博客园 - 胖在一方

ora-12557(win7 64bit oracle database + 32 bit oracle client) sp2-00027 错误解决 设置双网卡同时访问2个网络(转) oracle pl/sql developer 测试存储过程 日期为 yyyy-mm-dd hh24:mi:ss asp.net2.0学习记录笔记一 ora01991 oracle 更新多字段 Oracle 函数 oracle 9.2.01 wrap 问题 - 胖在一方 安装IE8 asp.net 不能调试 - 胖在一方 combo box 绑定Oracle 数据源显示为 datarowview BT用户,密码中含有@符号 - 胖在一方 - 博客园 .net 访问Oracle的连接字符串 (果然BT) 系统提示"System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本"解决办法 Javascript(二十三) MouseEvent Javascript(二十二)IE与DOM事件的区别 Javascript(二十一)IE与DOM事件的相似性 - 胖在一方 - 博客园 Javascript(二十) 事件对象 JavaScript(十九) Dom Event 二
ASP.NET2.0学习笔记二 - 胖在一方 - 博客园
胖在一方 · 2010-07-24 · via 博客园 - 胖在一方

Posted on 2010-07-24 10:10  胖在一方  阅读(258)  评论()    收藏  举报

GridView

DataKeys ,感觉在项目中没有使用的太多,可以使用很多变通的方法实现。

代码

//设定主键
 this.GridView1.DataKeyNames = new  string[]  {"xh"};
       
//显示选定GridView行的主键   
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
this.Label1.Text = "DataKeys = " + this.GridView1.DataKeys[this.GridView1.SelectedIndex].Values["xh"]
                
+ ";Index=" + this.GridView1.SelectedIndex.ToString();
    }
//后台添加绑定列,排序的话,需要设置 SortExpression属性值
 protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        
if (ViewState["sort"].ToString() == "asc")
        {
            ViewState[
"sort"= "desc";
             
        }
        
else
        {
            ViewState[
"sort"= "asc";
             
        }
        
this.Label1.Text = "SortExpression=" + e.SortExpression + ";SortDirection = " + e.SortDirection.ToString();

        DataSet ds 

= ReturnDataSet();
        ds.Tables[
0].DefaultView.Sort = e.SortExpression + " " + ViewState["sort"].ToString();
        
this.GridView1.DataSource = ds.Tables[0].DefaultView;
        
this.GridView1.DataBind();
    }
//分页
 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        
this.GridView1.PageIndex = e.NewPageIndex;
        
this.GridView1.DataSource = ReturnDataSet().Tables[0].DefaultView;
        
this.GridView1.DataBind();
    }


自定义分页

存储过程代码

代码

  /******************************
  *Author      : Stpangpang
  *Created     : 2010-07-04
  *Purpose     : 根据条件,返回结果集的总数,对于查询的分页功能使用
  **********************************
*/
function Query_KK_VEHICLE_Count( cCondition varchar2return number
is
  cSQL 
varchar2(500);
  iCount 
number(6);
begin
  cSQL :
= 'select count(xh) from kk_vehicle ' || cCondition ;
  
  
execute immediate cSQL into iCount;
  
  
return iCount;
end

//分页存储过程

代码

 /******************************
  *Author      : Stpangpang
  *Created     : 2010-07-4
  *Purpose     : 查询车辆通过数据
  **********************************
*/
procedure Query_KK_Vehicle  (cCondition varchar2,
                             iPageSize 
number,
                             iPageNumber 
number,
                             ds out myRcType
                             )
is
   cSql 
varchar2(1000);
   iMinSize 
number(6);
   iMaxSize 
number(6);
begin
        
    
--显示的最大记录数
    iMaxSize := iPageSize * iPageNumber;
    iMinSize :
= 1 + (iPageNumber-1* iPageSize;
    
--根据分页范围返回结果集
    cSQL := 'select * from ( select rownum rn ,t.* from ( select xh, kkxh, kkmc, hpzl, hphm, jgsj, scsj, xsfx, jgdd, xssd, zpy, zpj, bjlx, cdh, hpys, cllx, cwkc, dkbj, bj, zpt, bz from kk_vehicle '|| cCondition || ')  t where rownum <='|| iMaxSize ||' ) where rn >= '|| iMinSize ;
    
open ds for cSQL;
end Query_KK_Vehicle ;

//自定义分页代码

代码

using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;/*
 在GridView中显示字段
 * GridView控件支持以下类型的字段
 * ------------------------------------------
 * BoundField       将数据显示为文本
 *     在使用DataFormatString属性的时候,HtmlEncode设置为false,
 *     DataField , HeaderText , NullDisplayText ShowHeader 
 * 
 * CheckBoxField    将数据显示为复选框
 * CommandField     使用链接来支持编辑、删除、或者选中行
 * ButtonFiled      将数据项显示为按钮(image按钮、link按钮或者是push按钮)
 * HyperLinkField   将数据显示为超链接
 * ImageField       将数据项显示为图片
 * TemplateField    用来自定义数据项的定义
 * 
 * GridViewRow对象常用属性
 * Cells        表示绑定行中的网格集合
 * DataItem     绑定行相关的数据项  RowDataBound事件中 DataBinder.Eval(e.Row.DataItem ,"xh")
 * DataItemIndex    绑定行在数据源的索引
 * RowIndex     绑定行的索引
 * RowState     绑定行的状态
 * RowType      绑定行的类型,可以取值为 DataRow Footer Header NullRow Pager Separator.
 
*/protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            
//查询总记录数
               int iCount = PageCount( );
             
this.Label1.Text = string.Format ("总共记录为{0}条 ", iCount.ToString()) ;
             
//按照PageSize计算出总共有几页
             int iPageCount = (int)(iCount / this.GridView1.PageSize ) + 1;
             
//在无数据的时候,GridView的显示内容
             this.GridView1.EmptyDataText = "无数据";
            
//绑定GridView 参数含义为 页大小,第几页
             BindGridView(this.GridView1.PageSize, 1);//添加总页数的DropDownList
             for (int i = 1; i <=iPageCount    ; i++)
            {
                DropDownList1.Items.Add(
new ListItem(string.Format("第{0}页", i ), i.ToString()));
            }
        }
    }
//显示查询记录总数
protected int PageCount( )
{
    
//列出关键代码
     oraCmd.CommandType = CommandType.StoredProcedure;
        oraCmd.CommandText 
= "kk_op_blacklist.query_kk_vehicle_count";
       
        oraParameterResult.OracleDbType 
= OracleDbType.Int32;
        oraParameterResult.Value 
= DBNull.Value;
        oraParameterResult.Direction 
= ParameterDirection.ReturnValue;
        oraCmd.Parameters.Add(oraParameterResult);
        
int iResult =  (int)oraParameterResult.Value  ;
        
return iResult;
}
//读取数据绑定GridView
protected void BindGridView(int iPageSize, int iPageIndex)
{
    
//简化一些代码,列出关键的
    oraCmd.CommandType = CommandType.StoredProcedure;
        oraCmd.CommandText 
= "kk_op_blacklist.query_kk_vehicle";
     

 Oracle.DataAccess.Client.OracleParameter oraParamDs 

= new      
Oracle.DataAccess.Client.OracleParameter();
        oraParamDs.OracleDbType 
= Oracle.DataAccess.Client.OracleDbType.RefCursor;
        oraParamDs.Direction 
= ParameterDirection.Output;
        oraParamDs.Value 
= DBNull.Value;
        oraCmd.Parameters.Add(oraParamDs);
}
//选择显示第几页
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    
int PageIndex = Int32.Parse(this.DropDownList1.SelectedValue);
    BindGridView(
this.GridView1.PageSize, PageIndex );
}
=====================================
//测试代码,数据绑定时,给复选框的Tooltip增加一个值记录主键。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        CheckBox chk;
        
if (e.Row.RowType == DataControlRowType.DataRow)
        {
            chk 
= (CheckBox)e.Row.FindControl("chk");
            
//1.1中,我是这么写的 (DataRowView)e.Row.DataItem
            
//2.0中,用以下代码,效果一样。可能效率更高一些吧
            chk.ToolTip = DataBinder.Eval(e.Row.DataItem ,"xh").ToString();
        }
    }
//测试代码,显示选中GridView复选框中记录的主键值
     protected void Button1_Click(object sender, EventArgs e)
    {
        CheckBox chkSel;
        
for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            chkSel 
= (CheckBox)GridView1.Rows[i].FindControl("chk");
            
if (chkSel.Checked)
            {
                
this.Label2.Text = string.Format("{0},{1}"this.Label2.Text, chkSel.ToolTip);
                
            }
        }
    }

页面 MaintainScrollPositionOnPostback属性,可以使野蛮无论在什么时候会发都定位到同样的位置

<%@Page MaintainScrollPositionOnPostback="true" %>