GridView

单击gridview某一列弹出详细信息

DataGridView单击一行可以获得改行的详细信息,但是GridView不能直接单击获取改行信息,需要在绑定数据时,为每一行定义单击事件,功能实现如下:

1.在gridview的RowDataBound()事件中定义某一列的单击事件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)        

{            

if (e.Row.RowType == DataControlRowType.DataRow)//判断单击行是否为数据行            

{   DataRowView row = (DataRowView)e.Row.DataItem;                

e.Row.ID = GridView1.DataKeys[e.Row.RowIndex]["PKID"].ToString();//PKID为唯一能够标识一条记录的字段                

if (e.Row.ID != null && row != null)                

{                    

e.Row.Cells[1].Attributes.Add("id", "billid");                    

string url = "../revFreight/FreightBillView.aspx?billid=" + e.Row.Cells[3].Text;//第三列为订单号,唯一标识一条记录的键值                    

e.Row.Cells[1].Attributes.Add("onclick", "openview('" + url + "')");                

}            

}  }

2.在javascript中定义openview(url)

function openview(url) {        

window.open(url, '__scmhubCloseWindow', 'width=950,height=670, top=50,left=50, toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no, status=no');//打开一个新页面  或者:  

window.showModalDialog(url, '__scmhubCloseWindow', 'width=950,height=670, top=50,left=50, toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no, status=no');//弹出新窗体    

}

GridView用编辑或详细链接提取选中行数据

方法一:
1、在GridView中添加一个TemplateField字段,把他的ShowHeader改为False。(或在HTML代码中加上  【DataKeyNames="newsId"】这一句,其中【newsId】为GridView绑定的数据库的主码。)
2、在GridView的编辑模板中添加一个LinkButton,修改CommandName=Edit,CausesValidation=False,Text="编辑"。
3、在GridView的【RowEditing】事件中加上如下代码:
    Session["id"] = Convert.ToInt32(gvNews.DataKeys[e.NewEditIndex].Value);     Response.Redirect   ("ModifyNews.aspx");
4、在接受选中行数据的页面(即【ModifyNews.aspx】)中,添加如下代码:
   int id = Convert.ToInt32(Session["id"]);
   var news= XLzt.Business.NewNoticeManage.News.GetNewById(id);
   this.txtNewsTtitle.Text= news.title;
   this.txtModifyTimes.Value= news.publishTime.Value.ToShortDateString();
   ……
方法二:
1、在GridView中添加一个HyperLinkField可用字段,修改Text属性为“详细”或“编辑”
2、修改DataNavigateUrlFields属性为GridView要绑定的数据库的主键
3、修改DataNavigateUrlFormatString属性为要跳转的页面,并设置id值:
      ProductInformation.aspx?productInforId={0}(productInforId为数据库主键或Model层得主键映射)
4、在详细或编辑页面的Page_Load()中增加如下代码:
 if (!IsPostBack)
            {
                if (Request.QueryString["productInforId"] != null)
                {
                    int productID = Convert.ToInt32(Request.QueryString["productInforId"].ToString());
                    var member = CRM.Business.Product.GetNewById(productID);
                    if (member != null)
                    {
                        this.lblModel.Text = member.model;
                        this.lblType.Text = member.type;
                        this.lblBrand.Text = member.brand;
                        this.lblInnerDiameter.Text = member.innerDiameter.ToString();
                        this.lblExternalDiameter.Text = member.externalDiameter.ToString();
                        this.lblHeight.Text = member.height.ToString();
                        this.lblLimitSpeed.Text = member.limitSpeed.ToString();
                        this.lblReferenceSpeed.Text = member.referenceSpeed.ToString();
                        this.imgResult.ImageUrl = member.image;                        
                    }
                }
            }

 

 

 

GridView删除提示

只需在GridView的【RowDataBound】事件中添加如下代码:
 if (e.Row.RowType == DataControlRowType.DataRow) 
  { 
    if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) 
      { 
         ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除吗?')"); 
       } 
  } 
 其中,e.Row.Cells[5].Controls[0],中的【5】为删除按钮/链接在GridView中列的位置(从左向右由0开始计算)
一般按钮的删除提示
 lnkbtnDelete.Attributes.Add("OnClick", "javascript:return confirm('确定删除吗?')");
 //放在Page_Load里面,其中lnkbtnDelete为删除按钮或超链接的ID号。

 

GridView的Sorting排序

功能介绍:单击gridview的某一列列头,可以对该列进行排序。
实现方法:
1.设置AllowSorting="True"
2.在现实的字段中添加SortExpression属性,值为改字段的DataField
  如:
<asp:BoundField DataField="BILLCODE" HeaderText="单据号" SortExpression="BILLCODE" />
<asp:BoundField DataField="CODE" HeaderText="货号" SortExpression="CODE" />
3.添加GridView的Sorting方法
  现在Page_Load()方法中定义一个当前GV的排序顺序,并给出一个默认值:
  ViewState["sortdirection"] = "ASC";
  Sorting()方法中的代码如下:
        protected void grvPro_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (publicDS != null)
            {
                DataView dv = new DataView(publicDS);//publicDS为绑定GV的数据源
                if (ViewState["sortdirection"].ToString() == "ASC")
                {
                    dv.Sort = e.SortExpression + " DESC";
                    ViewState["sortdirection"] = "DESC";
                }
                else
                {
                    dv.Sort = e.SortExpression + " ASC";
                    ViewState["sortdirection"] = "ASC";
                }
                grvPro.DataSource = dv;
                grvPro.DataBind();
            }
        }

 

 

posted @ 2013-12-06 11:37  简单就好。。。  阅读(363)  评论(0编辑  收藏  举报