日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

如何创建一个用弹出窗口来查看详细信息的超链接列

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何创建一个用弹出窗口来查看详细信息的超链接列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?

如何創建一個用彈出窗口來查看詳細信息的超鏈接列
出處:www.dotnetjunkie.com

????? 這篇文章來自于一位忠實的DotNetJunkie的建議,他最初發了一封email給我們,
要求我們給出一個例子來說明如何在DataGrid中設置一個當用戶點擊時能夠彈出
顯示其詳細信息的新窗口的超鏈接列。在我們給他回復之前,他已經給我們提供了
一種方法,并建議我們寫篇教程。于是有了這篇文章。這篇文章包含了兩個webforms和一個css第一個webform包含了一個DataGrid,它顯示了Northwind數據庫中的一列產品還有寫著"SeeDetails"的超鏈接。只要點擊了這個鏈接,就會調用JavaScript的Window.Open方法來打開一個新的窗口。在一個Url中包含了用戶想詳細了解的產品的ProductId的Query String 參數。在第二個Webform里,是另一個DataGrid,它顯示了用戶選擇的產品的所有詳細信息。現在讓我們來看看WebForm1.aspx和WebForm1.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

? }
}?

其實這兩個文件沒有什么難點,除了DataNavigateUrlFormatString,你注意到我直接把JavaScript window.open寫在里面(注意:我也可以建一個js文件或者在WebForm中使用<script></script>,我直接寫是為了簡單)這個Javascript代碼應該對大家來說很熟悉,所以我也不深入討論它。它的作用就是在新窗口中打開WebForm2.aspx使用ProductId參數。這個值是來自我們的數據源。我們來看看webform2.aspx和webform2.aspx.cs。

WebForm2.aspx?
<%@Page language="c#" AutoEventWireup="false" Inherits="HowTos.DataGrid.PopupWindow.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
? <head>
??? <title>Product Details</title>
??? <LINK REL="stylesheet" TYPE="text/css" HREF="StyleSheet1.css"></LINK>
? </head>
??? <body>
????? <asp:DataGrid HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT" runat="server" id="DataGrid1" Font-Size="8" Height="50" Width="675"></asp:DataGrid>
????? <p align="center">
????? <a href="JavaScript:window.close()">close window</a>
????? </p>
??? </body>
</html>
?
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。SqlDataReader通過Query string參數獲得產品的數據。

總結

以上是生活随笔為你收集整理的如何创建一个用弹出窗口来查看详细信息的超链接列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。