在DataGrid中创建一个弹出式Details窗口 (转)

在DataGrid中创建一个弹出式Details窗口 (转)[@more@]

By: do.NETjunkies.com/authors.ASPx?authorid=2">Donny Mack
Level: Intermediate
Posted Date: XML:namespace prefix = st1 ns = "urn:schemas-microsoft-com:Office:smarttags" />1/2/2002
Tested with ASP.net Beta 2 (v.1.0.2914.16)
DOWNLOADs/2221/20020201T0201.zip" rel="nofollow"> Click here to download sample code

DataGrid中创建一个弹出式Details窗口
这篇文章来自DotNetJunkie的提议。他最初写信要求我们提供一个关于如何创建在DataGrid 中使用HyperlinkColumn的例子,可以在用户点击这一列后打开一个新窗口,显示出此列的详细内容。在此之前我们曾经通过eMail回答他们,他建议我们将这个方法加入他们的指南中,于是,就有了这篇文章。像我们原来的文章一样,它很简单,但是简单的包含代码的方法例可以更有效地启发开发者。
这个例子包含两个webForms和一个css文件(所有的代码都可以下载)--第一个WebForm包含一个展示从Northwind库中读出的产品列表的DataGrid,hyperlink的states设为“SeeDetails”,一旦这个链接被点击,Javascript片段 Window.Open方法就会被调用.用户想获得的关于产品的ProductID做为参数包含在URL中.包含另一个DataGrid的第二个Webforms向用户列示他选中产品的所有具体细节。让我们来看一下WebForm1.asp 和WebFiorm1.aspx.cs.

WebForm1.aspx C#" AutoEventWireup="false" Inherits="HowTos.DataGrid.PopupWindow.WebForm1" %>
A href="tag-88-1.html">w3c//DTD HTML 4.0 Transitional//EN" >

 


 
 
 
 

  RM runat="server" ID="Form1">
 
 
 
 
  Javascript:varwin=window.open('WebForm2.aspx?ProductID={0}',null,'width=692,height=25');" HeaderText="See Details" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEHYPERLINK" />
 
 
 
 

 


WebForm1.aspx.cs using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data. sqlClient ;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

  namespace HowTos.DataGrid.PopupWindow
  {

  public class WebForm1 : System.Web.UI.Page
  {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;

    #region User Defined Code

  private void Page_Load( object sender, System.EventArgs e)
  {

  if ( ! this.IsPostBack ) 
  this.BindData();

  }

  protected void BindData()
  {

  SqlCommand cmd = new SqlCommand( " select TOP 10 ProductID, ProductName FROM Products", con("Server=LocalHost; DataBase=Northwind; TRUSTED_CONNECTION=TRUE")); 
  this.DataGrid1.Data source = cmd.ExecuteReader(Comman dbehavior.CloseConnection);
  this.DataGrid1.DataBind();

  } 

  protected SqlConnection con(System.String ConnectionString )
  {

  SqlConnection c = new SqlConnection( ConnectionString );
  c.Open(); 
  return c;

  }

  #endregion

  #region Web Form Designer generated code

  overr ide protected void OnInit(EventArgs e)
  {
 
  InitializeComponent();
  base.OnInit(e);
 
  }

  private void InitializeComponent()
  { 
 
  this.Load += new System.EventHandler(this.Page_Load);
 
  }

#endregion

  }
}

除了DataNavigateUrlFormatString外确实没什么困难的,你可以注意到我实际上直接使用了一个javascript片段(注:你也可以简单地创建一个.js文件或在WebForm中使用),javascript如此普及,所以这里不再详细讲解。功能上,它打开一个新的窗口,带ProductID查询字串的WebForm2.aspx,ProductID的值来自我们的数据源。我们可以看这两个文件:

WebForm2.aspx


 


  Product Details
 
 
 
 
 


  close window
 


 

WebForm2.aspx.cs using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HowTos.DataGrid.PopupWindow
{

  public class WebForm2 : System.Web.UI.Page
  {
  protected System.Web.UI.WebControls.DataGrid DataGrid1;

  #region User Defined Code

  private void Page_Load(object sender, System.EventArgs e)
  {
  if ( ! this.IsPostBack ) 
  this.BindData();
  }

  protected void BindData()
  {
  SqlCommand cmd = new SqlCommand( "SELECT * FROM Products WHERE ProductID = @ProductID", con("Server=LocalHost; DataBase=Northwind; TRUSTED_CONNECTION=TRUE")); 
  cmd.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.VarChar, 200));
  cmd.Parameters["@ProductID"].Value = Request["ProductID"].ToString();
  this.DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  this.DataGrid1.DataBind();
  } 

  protected SqlConnection con(System.String ConnectionString )
  {

  SqlConnection c = new SqlConnection( ConnectionString );
  c.Open(); 
  return c;

  }

  #endregion

  #region Web Form Designer generated code

  override protected void OnInit(EventArgs e)
  {

  InitializeComponent();
  base.OnInit(e);

  }

  private void InitializeComponent()
  { 

  this.Load += new System.EventHandler(this.Page_Load);

  }

  #endregion

  }
}

WebForm2.aspx也很简单,只有一个绑定了SqlDataReader的DataGrid对象,这个Reader从产品库中得到数据,css文件如下:

 

StyleSheet1.css

/* Style Sheet */
BODY
{
margin-left: 0;
margin-top:10;
}
.HEADERSTYLE
{
background-color: #3a6ea5;
color: #FFFFFF;
font-weight:bold;
}

.ITEMSTYLEDEFAULT
{
background-color: #C0C0C0;
color: #000000;
font-weight: bold;
}

.ITEMSTYLEHYPERLINK {
background-color: #C0C0C0;
color: #000000;
font-weight: bold;
}

A:LINK
{
color: #000000;
}

A:VISITED
{
color: #000000;
}

A:HOVER
{
color: #3a6ea5;
}

20020201T0201.gif 

这个指向details的超链在最右边一列,如图所示:

20020201T0202.gif 

好了,就是这样,很简单吧。下次再见!Donny Mack.


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752019/viewspace-979884/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752019/viewspace-979884/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值