1

我正在尝试将 HyperLinkColumns 动态添加到我的 GridView。我有以下代码:

HyperLinkColumn objHC = new HyperLinkColumn();
objHC.DataNavigateUrlField = "title";
objHC.DataTextField = "Link text";
objHC.DataNavigateUrlFormatString = "id, title";
objHC.DataTextFormatString = "{2}";

GridView1.Columns.Add(objHC);

这不起作用,所以.. 我怎样才能将 HyperLinkColumn 添加到我的 GridView?

4

7 回答 7

5

您可能希望在绑定行时添加它:

protected void yourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
        HyperLink hlControl = new HyperLink();
        hlControl.Text = e.Row.Cells[2].Text; //Take back the text (let say you want it in cell of index 2)
        hlControl.NavigateUrl = "http://www.stackoverflow.com";
        e.Row.Cells[2].Controls.Add(hlControl);//index 2 for the example
}
于 2008-11-27T22:02:47.137 回答
1

您必须在 DataBinding 发生之前执行此操作,检查GridView Events

于 2008-11-27T21:43:56.113 回答
1

我认为您应该使用 HyperLinkField 而不是 HyperLinkColumn。

于 2009-11-15T13:50:34.833 回答
0

如果您只想重定向到另一个 URL,那么只需使用 HyperLink Web 控件并将其推送到 RowDataBound 事件中所需的 GridView Row 单元格中。
或者
如果你想在发送到另一个 URL 之前执行任何服务器事件,试试这个
  1) 在 GridView 的 RowDataBound 事件中添加 LinkBut​​ton 对象。
  2) 设置 CommandName、CommandArgument 属性(如果需要将任何数据传递给此对象)。
  3)通过处理GridView的RowCommand事件来捕获这个事件。

于 2008-11-28T15:46:10.207 回答
0

顺便说一句,我只是认为您可以使用 DataGridView 并在设计器中选择 Link 列,您的问题就会结束。DataGridView 确实有一个链接列,而不是您只需要在“单击”上添加一个事件,您就可以获得所需的内容。如果您可以切换到 DataGridView,则此解决方案有效。

于 2008-11-28T15:48:43.323 回答
0

我知道这个线程很旧,但忍不住加了我的 2 美分。以下教程中解释的过程对我来说非常有效: ASP Alliance

于 2009-12-16T17:02:54.443 回答
-2

看来你把事情搞混了。我不知道 - 该代码是如何编译的?

GridView 的列集合可以接受“DataControlField”类型的列。我认为您需要初始化 HyperLinkField 并设置相关属性(文本、NavigateUrl、HeaderText、Target)并将其添加到列集合中。

HyperLinkColumn 类在您使用 DataGrid 时有意义(不适用于 GridView)。

希望有帮助。

于 2008-11-27T21:43:47.800 回答