2012-08-17 61 views
0

我试图将第一列元素设置为超链接重定向到另一个页面..但不知怎的,无论我尝试什么,它都不起作用。无法将列元素设置为超链接

reportTable.Rows[i].Cells[1].Text = report.reportId.ToString(); 
       TableCell tCell = new TableCell(); 

       tCell.Controls.Add(new LiteralControl(" report.reportId.ToString  ()")); 
       // Create Hyperlink Web Server control and add to cell 
       System.Web.UI.WebControls.HyperLink h = new HyperLink(); 
       h.Text = reportTable.Rows[i].Cells[1].Text = report.reportId.ToString(); 
       h.NavigateUrl = "~/manage.aspx"; 
       tCell.Controls.Add(h); 

       reportTable.Rows[i].Cells[2].Text = bench.mechId; 
       reportTable.Rows[i].Cells[3].Text = bench.elecId; 
       reportTable.Rows[i].Cells[4].Text = bench.name; 
+0

以何种方式是不工作?你有没有更多的代码来显示你在这张桌子上做什么,以及在哪里? – 2012-08-17 11:59:37

+0

这有点奇怪'新的LiteralControl(“report.reportId.ToString()”)',你可以展开什么不工作? – V4Vendetta 2012-08-17 12:03:52

+0

列元素没有被设置为超链接,尽管我已经将它们添加到了项目模板中。这是从数据源中获取reportid的。名称空间已经被编译并添加到引用程序集中。 – user1597398 2012-08-17 12:13:17

回答

1

asp.net网格和数据控件非常适合数据绑定。

此外,他们提供了可定制,包括链接项目模板,下拉列表等

不要通过手动创建的所有控件填充您的网格。这完全违背了使用电网的目的。

e.g:

<asp:GridView ID="ReportsGridView" 
    DataSourceID="ReportsDataSource" 
    AllowPaging="true" 
    AutoGenerateColumns="false" 
    runat="server"> 
    <Columns> 
    <asp:TemplateField> 
     <ItemTemplate> 
     <asp:LinkButton runat="server" 
      ID="RedirectButton" 
      CommandName="Manage" 
      PostBackUrl="~/manage.aspx" 
      Text="Manage" /> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:BoundField DataField="Name" 
     HeaderText="Report Name"/> 
    </Columns> 
</asp:GridView> 
+0

这是我第一次尝试,但然后我读了超链接是一个地方更好的选择,但它们都不起作用 – user1597398 2012-08-17 12:14:12

+0

链接按钮将呈现给呈现给浏览器的HTML超链接。 – nunespascal 2012-08-17 12:23:10