国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

在datagrid中的HyperLinkColumn上達(dá)到談出一個窗口的效果

2024-07-21 02:17:00
字體:
供稿:網(wǎng)友
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" />
//其實(shí)整個文章只要看這么一句就可以了。。。點(diǎn)睛之筆就是它了
            </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;
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 贵州省| 赤城县| 章丘市| 阳山县| 萨嘎县| 灵川县| 扬中市| 灵山县| 彝良县| 屯留县| 常州市| 安吉县| 洛浦县| 文安县| 普洱| 工布江达县| 察哈| 玉龙| 南汇区| 屏边| 镇赉县| 汉沽区| 宜黄县| 平塘县| 柳河县| 利辛县| 宁强县| 福贡县| 万载县| 兴宁市| 保靖县| 府谷县| 连城县| 闵行区| 河北省| 射阳县| 鹤峰县| 玉树县| 南昌县| 长兴县| 于田县|