在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
<%@ Page language="c#" Codebehind="datagrid-open.aspx.cs" AutoEventWireup="false" Inherits="study.datagrid_open" %>
<HTML>
<HEAD>
<title>datagrid-open</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</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="Product Name" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT" />
<asp:hyperlinkcolumn DataTextFormatString="Show Details..." DataTextField="ProductID" DataNavigateUrlField="ProductID" DataNavigateUrlFormatString="javascript:var win = window.open("datagrid-show.aspx?ProductID={0}",null,"width=700,height=200");" HeaderText="See Details" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEHYPERLINK" />
</Columns>
</asp:datagrid>
</form>
</center>
</body>
</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
{
/// <summary>
/// datagrid_open 的摘要说明。
/// </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);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
除了DataNavigateUrlFormatString外确实没什么困难的,你可以注意到我实际上直接使用了一个javascript片段(注:你也可以简单地创建一个.js文件或在WebForm中使用<script></script>),javascript如此普及,所以这里不再详细讲解。功能上,它打开一个新的窗口,带ProductID查询字串的datagrid_show.aspx,ProductID的值来自我们的数据源。我们可以看这两个文件:
datagrid_show.aspx
<%@ Page language="c#" Codebehind="datagrid-show.aspx.cs" AutoEventWireup="false" Inherits="study.datagrid_show" %>
<HTML>
<HEAD>
<title>datagrid-show</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<P align="left">
<asp:DataGrid HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT" runat="server" id="DataGrid1" Font-Size="8" Height="50" Width="675"></asp:DataGrid></P>
<p align="center">
<a href="JavaScript:window.close()">close window</a>
</p>
</body>
</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
{
/// <summary>
/// datagrid_show 的摘要说明。
/// </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);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
分享到:
相关推荐
C# datagrid基本使用 源代码 ,VS2005环境下运行
C#160使用对象ArrayList填充DataGrid 源代码
NULL 博文链接:https://songwensheng.iteye.com/blog/628997
主要介绍了C#实现改变DataGrid某一行和单元格颜色的方法,主要涉及DataGrid控件的添加与使用、数据源的绑定、单元格与行的获取等操作。需要的朋友可以参考下
c# datagrid更新实例
主要介绍了C#实现给DataGrid单元行添加双击事件的方法,较为详细的分析了C#给DataGrid单元添加双击事件的步骤及相关实现代码,具有一定参考借鉴价值,需要的朋友可以参考下
c# DATAGRID使用方法c# DATAGRID使用方法c# DATAGRID使用方法c# DATAGRID使用方法
C# Wince中设置DataGrid的Cell的格式、颜色 可以根据条件任意设置Cell的颜色边框等,甚至可以重画, 并且也有DataGrid动态绑定的功能,值得拥有。
c#实现御览、打印datagrid中的数据
Visual C++源代码 143 如何在DataGrid单元格中添加组合框Visual C++源代码 143 如何在DataGrid单元格中添加组合框Visual C++源代码 143 如何在DataGrid单元格中添加组合框Visual C++源代码 143 如何在DataGrid单元格...
C# winform 开发中 DataGrid设计带有筛选功能!含源代码,值得一看!
结合实例很好的讲解了DataGrid中数据绑定,与数据操作即增删改查,还有分页功能。适合初学者快速的掌握DataGrid 的典型应用
WPF中DataGrid里面的Checkbox实现单选关键代码, 文档里面的代码复制粘贴即可实现!本人也是项目需要写的一个功能11行代码实现此功能! 希望可以帮助到你们!
通过这个简单的DataGrid数据绑定实例,可有效帮助新手学会C#最基本的数据库操作,这些操作主要有使用DBConnection连接数据源或数据库,使用DataSet设定数据对象,使用DataGrid绑定数据并显示,这些都是基础的知识,...
Visual C++源代码 144 如何在DataGrid单元格设置输入提示Visual C++源代码 144 如何在DataGrid单元格设置输入提示Visual C++源代码 144 如何在DataGrid单元格设置输入提示Visual C++源代码 144 如何在DataGrid单元格...
c#在DataGrid中显示GIF动画
Flex使用弹出窗口(titlewindos)为DataGrid添加新数据
jquery easy ui datagrid完整项目源代码,非常值得学习
C#读取XML文件并将其内容显示在DataGrid组件中。可以学习一下如何使用C#读取XML中的节点数据,并将数据内容捆绑在DataGrid列表控件中,本例虽然简单,但在C#应用中是相当广泛的。
C# WPF DataGrid控件实现三级联动 博客原文:http://blog.csdn.net/weixinhum/article/details/49449973