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;                       
                    }
                }
            }

posted @ 2011-04-23 09:42  Alec-Yin  阅读(2487)  评论(0编辑  收藏  举报
更多前端博客,访问:http://lhyin.com