By: Donny Mack
Level: Intermediate
Posted Date: 1/2/2002
Tested with ASP.NET Beta 2 (v.1.0.2914.16)
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 |
<%@ Page language="c#" AutoEventWireup="false" Inherits="HowTos.DataGrid.PopupWindow.WebForm1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <head> <LINK REL="stylesheet" TYPE="text/css" HREF="StyleSheet1.css"></LINK> </head> <body> <center> <form runat="server" ID="Form1"> <asp:datagrid id="DataGrid1" runat="server" Font-Size="12" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn DataField= "ProductID" HeaderText= "Product ID" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT" /> <asp:BoundColumn DataField="ProductName" HeaderText="ProductName" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT"/> <asp:hyperlinkcolumn DataTextFormatString="ShowDetails..." DataTextField="ProductID" DataNavigateUrlField="ProductID" DataNavigateUrlFormatString="javascript:varwin=window.open('WebForm2.aspx?ProductID={0}',null,'width=692,height=25');" HeaderText="See Details" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEHYPERLINK" /> </Columns> </asp:datagrid> </form> </center> </body> </HTML>
|
| 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.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
} } |