c#中在DataGrid裡建立一個彈出式視窗——可實現原始碼

來源:互聯網
上載者:User
 

在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 Form設計器所必需的。
   //
   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 Form設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  } 
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
}
}

 
 
 
 

作者Blog:http://blog.csdn.net/abandonship/

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.