C# 将 HyperLinkColumn 添加到 GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/324682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Add HyperLinkColumn to GridView
提问by
I'm trying to add HyperLinkColumns dynamically to my GridView. I have the following code:
我正在尝试将 HyperLinkColumns 动态添加到我的 GridView。我有以下代码:
HyperLinkColumn objHC = new HyperLinkColumn();
objHC.DataNavigateUrlField = "title";
objHC.DataTextField = "Link text";
objHC.DataNavigateUrlFormatString = "id, title";
objHC.DataTextFormatString = "{2}";
GridView1.Columns.Add(objHC);
This doesn't work, so.. how can i add a HyperLinkColumn to my GridView?
这不起作用,所以.. 我怎样才能将 HyperLinkColumn 添加到我的 GridView?
回答by shahkalpesh
It seems you have got things mixed up. I don't know - how that code compiles?
看来你把事情搞混了。我不知道 - 该代码如何编译?
GridView's column collection can accept columns of type "DataControlField". I think you need to initialize HyperLinkField and set relevant properties (text, NavigateUrl, HeaderText, Target) and add it to the columns collection.
GridView 的列集合可以接受“DataControlField”类型的列。我认为您需要初始化 HyperLinkField 并设置相关属性(文本、NavigateUrl、HeaderText、Target)并将其添加到列集合中。
HyperLinkColumn class is meaningful when you are using DataGrid (not in case of GridView).
HyperLinkColumn 类在您使用 DataGrid 时是有意义的(不是在 GridView 的情况下)。
Hope that helps.
希望有帮助。
回答by CMS
You have to do it before the DataBinding takes place, check the GridView Events.
您必须在 DataBinding 发生之前执行此操作,请检查GridView Events。
回答by Patrick Desjardins
You might want to add it when the row is binded:
您可能希望在绑定行时添加它:
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
}
回答by Sachin Gaur
In case, if you just want to redirect to another URL then simple use HyperLink web control and push it in the desired cell of GridView Row at RowDataBound event.
OR
If you want to perform any server event before sending it to another URL, try this
1) Add LinkButton object at RowDataBound event of GridView.
2) Set the CommandName, CommandArgument property, if requried to pass any data to this object.
3) Capture this event by handling the RowCommand event of the GridView.
如果您只想重定向到另一个 URL,那么只需使用 HyperLink Web 控件并将其推送到 RowDataBound 事件中的 GridView Row 的所需单元格中。
或
如果您想在将其发送到另一个 URL 之前执行任何服务器事件,请尝试此
1) 在 GridView 的 RowDataBound 事件中添加 LinkButton 对象。
2) 设置 CommandName、CommandArgument 属性,如果需要将任何数据传递给该对象。
3)通过处理GridView的RowCommand事件来捕获这个事件。
回答by Patrick Desjardins
By the way, I just think that you can use the DataGridView and in the Designer select the Link column and your problem would be over. The DataGridView does have a link column, than you just need to add an event on "Click" and you will be able to have what you want. This solution works if you can switch to DataGridView.
顺便说一句,我只是认为您可以使用 DataGridView 并在设计器中选择链接列,您的问题就结束了。DataGridView 确实有一个链接列,而您只需要在“单击”上添加一个事件,您就可以拥有所需的内容。如果您可以切换到 DataGridView,则此解决方案有效。
回答by Patrick Desjardins
I think you should be using a HyperLinkField not a HyperLinkColumn.
我认为您应该使用 HyperLinkField 而不是 HyperLinkColumn。
回答by Sudhanshu Mishra
I know this thread is old but couldn't help adding my 2 cents. The procedure explained in the following tutorial worked perfectly for me: ASP Alliance
我知道这个线程很旧,但忍不住加了我的 2 美分。以下教程中解释的过程对我来说非常有效: ASP Alliance