c#中在DataGrid里创建一个弹出式窗口------可实现源代码

在DataGrid中创建一个弹出式窗口

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

1@ Page language="c#" Codebehind="datagrid-open.aspx.cs" AutoEventWireup="false" Inherits="study.datagrid_open" 
 1<html>
 2<head>
 3<title>datagrid-open</title>
 4<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8</head>
 9<body>
10<center>
11<form id="Form1" runat="server">
12<asp:datagrid autogeneratecolumns="False" font-size="12" id="DataGrid1" runat="server">
13<columns>
14<asp:boundcolumn datafield="ProductID" headerstyle-cssclass="HEADERSTYLE" headertext="Product ID" itemstyle-cssclass="ITEMSTYLEDEFAULT"></asp:boundcolumn>
15<asp:boundcolumn datafield="ProductName" headerstyle-cssclass="HEADERSTYLE" headertext="Product Name" itemstyle-cssclass="ITEMSTYLEDEFAULT"></asp:boundcolumn>
16<asp:hyperlinkcolumn datagrid-show.aspx?productid='{0}",null,"width=700,height=200");"' datanavigateurlfield="ProductID" datanavigateurlformatstring="javascript:var win = window.open(" datatextfield="ProductID" datatextformatstring="Show Details..." headerstyle-cssclass="HEADERSTYLE" headertext="See Details" itemstyle-cssclass="ITEMSTYLEHYPERLINK"></asp:hyperlinkcolumn>
17</columns>
18</asp:datagrid>
19</form>
20</center>
21</body>
22</html>

datagrid-open.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace study
{
///

1<summary>   
2/// datagrid_open 的摘要说明。   
3/// </summary>

public class datagrid_open : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
#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=dwserver; DataBase=Northwind; User Id=sa; Password=123456"));
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)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

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

除了DataNavigateUrlFormatString外确实没什么困难的,你可以注意到我实际上直接使用了一个javascript片段(注:你也可以简单地创建一个.js文件或在WebForm中使用

1<script></script>

),javascript如此普及,所以这里不再详细讲解。功能上,它打开一个新的窗口,带ProductID查询字串的datagrid_show.aspx,ProductID的值来自我们的数据源。我们可以看这两个文件:
datagrid_show.aspx

1@ Page language="c#" Codebehind="datagrid-show.aspx.cs" AutoEventWireup="false" Inherits="study.datagrid_show" 
 1<html>
 2<head>
 3<title>datagrid-show</title>
 4<meta content="Microsoft Visual Studio 7.0" name="GENERATOR"/>
 5<meta content="C#" name="CODE_LANGUAGE"/>
 6<meta content="JavaScript" name="vs_defaultClientScript"/>
 7<meta content=" http://schemas.microsoft.com/intellisense/ie5 " name="vs_targetSchema"/>
 8</head>
 9<body>
10<p align="left">
11<asp:datagrid font-size="8" headerstyle-cssclass="HEADERSTYLE" height="50" id="DataGrid1" itemstyle-cssclass="ITEMSTYLEDEFAULT" runat="server" width="675"></asp:datagrid></p>
12<p align="center">
13<a href="JavaScript:window.close()">close window</a>
14</p>
15</body>
16</html>

datagrid_show.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace study
{
///

1<summary>   
2/// datagrid_show 的摘要说明。   
3/// </summary>

public class datagrid_show : 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=dwserver; DataBase=Northwind; User Id=sa; Password=123456"));
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)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

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

Published At
Categories with Web编程
Tagged with
comments powered by Disqus