在datagrid中的HyperLinkColumn上达到谈出一个窗口的效果

datagrid|perl

Creating a Popup Window Details Grid in a DataGrid  
This articles topic came from the suggestion of a true DotNetJunkie. He originally sent an email to us asking for an example illustrating how to make a HyperLinkColumn in a DataGrid spawn events that would pop up a new window with details of the row that the user clicked on. Before we could anwser his email he had already emailed us back explaining that he had found a way to do it and suggested a tutorial of his discovery. So, here it is! As with most of our articles, this simplifies the task, but easy examples of coding techniques is what gives developers ideas for more complex senerios.  
This example contains two WebForms and one external style sheet (All code is included in the download) - The first WebForm contains a DataGrid with a list of products from the Northwind database and a hyperlink that states "SeeDetails". Once this link is clicked the JavaScript Window.Open method is used to open a new window. Within the URL is a Query String parameter of the ProductID of the product the user wants the details for. In the second WebForm there is another DataGrid that shows the user all the details for the chosen product. The stylesheet is used just because its cleaner to use than inline styles. So lets take a look at WebForm1.aspx and 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

  }
}  
There isn't really anything out of the ordinary on here except for the details of DataNavigateUrlFormatString You'll notice that I actually have JavaScript window.open directly in it (Note: I could have just as easily created an external .js file or <script></script> within the WebForm - I used it inline for simplicity. This JavaScript code should look familiar to all so I won't go into discussion about it. Essentially, it will open a new browser with the page WebForm2.aspx with a query string parameter ProductID. This value is the ProductID from our data source. So let's look at WebForm2.aspx and 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 is also quite simple. The only object that resides on the page is a DataGrid which is bound to a SqlDataReader. The Reader gets the data for the product based on the query string parameter of the ProductID value. Let's quickly look at the CSS (Cascading Style Sheet) file and then below that contains a figure illustrating WebForm1.aspx when it is first rendered:  
StyleSheet1.css  
/* Style Sheet */
BODY
{
margin-left: 0;
margin-top:10;
}
.HEADERSTYLE
{
background-color: #3a6ea5;
color: #FFFFFF;
font-weight:bold;
}

.ITEMSTYLEDEFAULT
{
background-color: #C0C0C0;
color: #000000;
font-weight: bold;
}

.ITEMSTYLEHYPERLINK {
background-color: #C0C0C0;
color: #000000;
font-weight: bold;
}

A:LINK
{
color: #000000;
}

A:VISITED
{
color: #000000;
}

A:HOVER
{
color: #3a6ea5;
}

时间: 2024-09-17 04:22:18

在datagrid中的HyperLinkColumn上达到谈出一个窗口的效果的相关文章

android-Android中如何实现在任何界面手指向上滑动弹出一个窗口?

问题描述 Android中如何实现在任何界面手指向上滑动弹出一个窗口? 我现在要处理一件事,就是在手机的任何界面手指向由底部向上滑动的时候弹出一个窗口,里面是一些快捷方式,比如wifi.蓝牙的开关. 弹出窗口我使用的是PopuWindow,向上滑动使用的是GestureDetector,但是我发现两者都要绑定到一个view,那样的话就不能实现任何界面都有效了.请教下那位做过,能否给点提示? 再次先谢谢了! 解决方案 做一个浮动窗口,放在最底部,窗体里相应手势动作 解决方案二: 我之前做过的一个项

android-安卓开发中,点击卸载按钮,弹出一个提示框

问题描述 安卓开发中,点击卸载按钮,弹出一个提示框 解决方案 http://www.cnblogs.com/zealotrouge/p/3159772.html 解决方案二: 给按钮注册onCLick()事件,在里面实现弹出一个dialog就可以了很简单,很多书上都有参考程序 解决方案三: 你要的是这种效果吧: //packageName是要卸载的包名,比如百度贴吧是com.baidu.tieba Uri uri = Uri.parse("package:" + packageName

界面-如何使打开的其他程序或者文档在MFC窗口中显示,而不是新打开一个窗口

问题描述 如何使打开的其他程序或者文档在MFC窗口中显示,而不是新打开一个窗口 我想在程序界面中指定一个区域,用于显示被打开的其他程序或者文档的界面.可是我用ShellExecute函数打开指定的那个文档和程序的时候,总是会再启动另一个窗口,效果就像是直接点击打开那个文件一样. 解决方案 将mfc窗口设置为其它程序的父窗口 解决方案二: 参考:程序只允许打开唯一实例且所有关联文档都在同一实例内打开的一揽子解决办法http://blog.csdn.net/IfI/article/details/1

点击一个图片后,弹出一个窗口,并在窗口中完成ajax的实时提示

问题描述 点击一个图片后,弹出一个窗口,并在窗口中完成ajax的实时提示 点击一个链接后,弹出一个窗口,并在窗口中完成ajax的实时提示. 在此请教各位,如何实现? $.ajax({ type:'POST', url: 'hhWebsiteInfoController.do?website-getDt', dataType:'json', data:'', success:function(json){ alert(1); var dts = json.attributes.dts; $("#n

windows-nodejs上每敲出一个代码都会返回undefined

问题描述 nodejs上每敲出一个代码都会返回undefined 如题,我用windows的nodejs,在cmd下进入node,敲出一行代码都会返回undefined,这是为什么? 解决方案 因为返回值是undefined 解决方案二: 那是正常的亲,你用控制台也是一样的好么 解决方案三: 你自己到浏览器的console里面也是一样的

怎么让swing 下拉框JCombox 在鼠标放在一个选项上时闪出一个提示

问题描述 实现这样一个功能:鼠标放到下拉框JCombox的一个选项上时,闪出一个提示 解决方案 继承ListCellRenderer,可以是一个label或者其他component,然后通过JCombox.setRender()具体:http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

win7中打开文件夹会重新打开一个窗口的问题

 在网上查了些资料,一般用到dos环境设置和修改注册表居多,但这两种方式都比较麻烦,尤其对于对计算机不是很懂的同事来说更是火上浇油.其实这种问题都是IE出了问题,下面总结两     种非常简单易行的方法:     1 使用360查杀木马,完了后它会提示你修复相关项,修复完后按照操作重启即可解决问题.     2 在别人电脑拷贝一份C://programe files/Internet Explorer文件到你电脑相应位置也可以解决问题.

在java中 如何使按钮点击后 弹出一个新的对话框?

问题描述 package jlabel;import java.awt.Color;import javax.swing.JFrame;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.FileInputStream;import java.io.File;import java.io.InputStream;import javax

javascript 指上图片弹出一个层效果

www.111cn.net