首页 > 编程 > Perl > 正文

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

2024-07-21 02:17:00
字体:
来源:转载
供稿:网友
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;
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表