Using a HyperlinkColumn with a Label Control or just basic Text
I have a situation where I currently have a HyperLinkColumn control that I would like to modify to have a Label o开发者_StackOverflow社区r simple text appear in the same column as the hyperlink. How do I acheive this please? Any Idea?
If this is DataGrid then you can handle the ItemCreated event and add code that adds a new control. For example:
<asp:DataGrid ... OnItemCreated="OnMyDataGridItemCreated" ... />
private void OnMyDataGridItemCreated(object sender, DataGridItemEventArgs e) {
Label textLabel = new Label();
textLabel.Text = "Hello!";
e.Item.Cells[3].Controls.Add(textLabel);
// Instead of "3" you might need to pick a different column
}
If you are using a GridView you could use a TemplateField. In there, you can have a hyperlink and a label, which are both databound. In the OnRowDataBound() Event you can decide which of the two controls should be visible to the user.
Edit: I've written an article about different ways to bind data to a GridView and decided to add an example of a HyperLink and Label, whose properties are set in the RowDataBound-Event: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns
精彩评论