• GridView中加入超连接


    大家好,本贴内容转自:http://bbs.csdn.net/topics/250047480

    Demo1:

    前台代码:

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>GridView2 Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID = "grdView2" runat = "server" AutoGenerateColumns="False" >
            <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="WebSite" DataNavigateUrlFormatString="http://www.{0}.com"
                    DataTextField="WebSite" HeaderText="WebSite"/>
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
            </Columns>
            </asp:GridView>
        </div>         
        </form> 
    </body>
    </html>

    后台代码:

    public partial class GridView2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[,] siteurl =
            {
                {"google","guge","America"},
                {"baidu","baidu","China"},
                {"yahoo","yahu", "America"}
            };
            // use grdView2
            BoundGV2(siteurl);
        }
     
        # region   Methods for grdView2
        void BoundGV2(string[,] siteurl)
        {
            this.grdView2.DataSource = GetTable2(siteurl);
            this.grdView2.DataBind();
        }
        DataTable GetTable2(string[,] siteurl)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("WebSite", typeof(string));
            dt.Columns.Add("Description", typeof(string));
            dt.Columns.Add("Country", typeof(string));
     
            for (int i = 0; i < siteurl.GetLength(0); i++)
            {
                int j = 0;
                DataRow row = dt.NewRow();
                row["WebSite"] = siteurl[i, j++];
                row["Description"] = siteurl[i, j++];
                row["Country"] = siteurl[i, j];
                dt.Rows.Add(row);
            }
            return dt;
        }
        #endregion
    }

    Demo2:

    前台代码:

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>GridView3 Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:GridView ID = "grd" runat = "server" >
        
        </asp:GridView> 
        </div>
        </form>
    </body>
    </html>

    后台代码:

    public partial class GridView3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string[,] siteurl =
            {
                {"google","guge","America"},
                {"baidu","baidu","China"},
                {"yahoo","yahu", "America"}
            };
            BoundGV3(siteurl);
        }
     
        void BoundGV3(string[,] siteurl)
        {
            this.grd.AutoGenerateColumns = false;
     
            HyperLinkField hlf = new HyperLinkField();
            string[] str = {"WebSite","Describe","Country"};
            hlf.DataNavigateUrlFields = str ;
            hlf.DataNavigateUrlFormatString = "http://www.{0}.com";
            hlf.DataTextField = "WebSite";
            hlf.HeaderText = "Site";
            this.grd.Columns.Add(hlf);
     
            BoundField bfDescribe = new BoundField();
            bfDescribe.DataField = "Describe";
            bfDescribe.HeaderText = "Description";
            this.grd.Columns.Add(bfDescribe);
     
            BoundField bfCountry = new BoundField();
            bfCountry.DataField = "Country";
            bfCountry.HeaderText = "Country";
            this.grd.Columns.Add(bfCountry);
     
            this.grd.DataSource = GetTable3(siteurl);
            this.grd.DataBind();
        }
     
        DataTable GetTable3(string[,] siteurl)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("WebSite",typeof (string));
            dt.Columns.Add("Describe",typeof (string));
            dt.Columns.Add("Country",typeof (string ));
     
            for (int iRow = 0; iRow < siteurl.GetLength(0); iRow++)
            {
                int j = 0;
                DataRow row = dt.NewRow();
                row["WebSite"] = siteurl[iRow, j++];
                row["Describe"] = siteurl[iRow, j++];
                row["Country"] = siteurl[iRow, j];
                dt.Rows.Add(row);
            }
            return dt;
        }
    }

    请支持原作者:Jueyoung 的总结,非常感谢!

  • 相关阅读:
    ASP.NET 2.0个性化配置(profile)
    03 创建虚拟机
    一些新的Blender的视频教程
    [转载]虚拟家庭存档文件修改方法
    [转载]游戏开发中常用的设计模式
    批处理for命令详解(转)
    [转载]高效软件开发团队的特征
    软件构架师的特点
    一点SICP(Structure and Interpretation of Computer Programs) 资料 (转载加整理)
    [译]游戏编程入门(by David Astle)
  • 原文地址:https://www.cnblogs.com/Martin_Q/p/3055087.html
Copyright © 2020-2023  润新知