代码之家  ›  专栏  ›  技术社区  ›  user1597398

无法将列元素设置为超链接

  •  0
  • user1597398  · 技术社区  · 12 年前

    我正在尝试将第一列元素设置为重定向到另一页的超链接。。但不知怎么的,不管我怎么做,它似乎都不起作用。

    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;
    
    1 回复  |  直到 12 年前
        1
  •  1
  •   nunespascal    12 年前

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

    此外,他们还提供了可以自定义的项目模板,包括链接、下拉列表等。

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

    例如:

     <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>