带有从数据库获得的文本的超链接

时间:2014-10-21 06:49:25

标签: c#

我正在从数据库中读到一张表。我希望使用超链接生成所选文本变量,以将我重定向到另一个asp页面。怎么会发生?下面是我的代码:

foreach(var album in data)
{
    TableRow tr = new TableRow();

    tr.Cells.Add(new TableCell()
    { 
        Text = album.AlbumName,
    });

    tr.Cells.Add(new TableCell());
    tblData.Rows.Add(tr);

    var album_songs = bl.GetSongsByAlbum(album.Id_Album);

    Table childTbl = new Table();
    childTbl.CssClass = "pure-table-horizontal";

    foreach(var song in album_songs)
    {
        TableRow tr2 = new TableRow();
        tr2.Cells.Add(new TableCell()
        {
            Text = song.SongName
        });
        childTbl.Rows.Add(tr2); 
    }

    tr = new TableRow();
    TableCell cell = new TableCell();
    cell.Width = 100;

    tr.Cells.Add(cell);
    cell = new TableCell();
    cell.Style.Add(HtmlTextWriterStyle.Padding, "0");
    cell.Style.Add(HtmlTextWriterStyle.Margin, "0");
    cell.Controls.Add(childTbl);
    tr.Cells.Add(cell);
    tblData.Rows.Add(tr); 
}
}

请用超链接重写代码

1 个答案:

答案 0 :(得分:0)

enter image description here

 Table t = new Table();
 foreach(var album in data)
 {
   TableRow tr = new TableRow();
   tr.Cells.Add(new TableCell() { Text = album.AlbumName });        
   HyperLink link = new HyperLink![enter image description here][2]();
   link.ID = "HyperLink1";
   link.NavigateUrl = "~/files/aspx_pages/.aspx";//Give the name of the aspx page that you want to navigate
   link.Text = "Album1 Link";
   TableCell tc = new TableCell();
   tc.Controls.Add(link);
   tr.Cells.Add(tc);
   t.Rows.Add(tr);
 }
 Page.Controls.Add(t);

生成HyperLink,如下所示

HyperLink link = new HyperLink();
link.ID = "HyperLink1";
link.NavigateUrl = "~/files/aspx_pages/" + ValueFromYourDB + ".aspx"
link.Text= "Text from DB";

在此之后将link添加到您正在准备的表格