Hello all,
I plan to migrate my current application which has some web tabular forms to the webgrid. However, my current application has dynamically generated hyper links for some columns items. So what do I need to do if I want the same functionality in the webgrid? Thank you very much.
The "value" of a cell should take whatever datatype is defined by the column. If you want to put hyperlinks in the cells of a given column, you should ensure that it's a string column. Unless you're somehow explicitly making a column of UltraGridCell objects, I don't see why the Value property of a cell should be coming through as an UltraGridCell type. This should be the same in 8.2 as it is in earlier versions of WebGrid.
If you're loading data from the database, this means you're likely databinding. In this case, you should use the InitializeRow event of the grid (which is raised once for every row created via databinding), rather than manually looping through all the rows of your grid. Most ikely, your grid doesn't have any columns defined at the time when you're trying to loop through the rows; this means that "Cells[0]" would return a null result, and trying to get its TargetURL property would result in your NullReferenceException.
Setting the TargetURL property of a cell is different from setting its Value to a hyperlink. If you're using the TargetURL property, just provide it the URL to which you want to navigate, not the whole HTML anchor tag. If you want to provide the whole HTML anchor tag, then set the Value of your cell accordingly.
A good practice is to add an unbound column containing data of type System.String, use InitializeRow to copy your hyperlink based on data from another existing column, and if you don't need that other column displayed, setting that other column to either be hidden or server-only.
Hello WombatEd,
Thank you for replying. I have read and tried your solutions. It seems that the version(8.2) I'm using is different. The value doesn't take string value. Instead, it takes some UltraGridCell object, and it doesn't execute InitializeRow method. Could it be some other method to initialize the rows?. What I am planning to do is loop through all rows in the ultrawebgrid, and generate hyperlink for the first cell which means UltraWebGrid1.Rows[i].Cell[0] by using the actual cell content plus some other non-cell content sources. So, I put the following code in the Page_Load, like :
for (int i = 0; i <= UltraWebGrid1.Rows.Count; i++) { UltraWebGrid1.Rows[i].Cells[0].TargetURL = "<a href='http://MyDomain.com/MyPage.aspx?OrderId=" + 123 + "'>click me " +
UltraWebGrid1.Rows[i].Cells[0].Value.ToString() + " </a>";
}
However, it reports "System.NullReferenceException: Object reference not set to an instance of an object." on line "UltraWebGrid1.Rows[i].Cells[0].TargetURL = ..." . That means it hasn't loaded any data from database yet? I got stuck on this point at this moment. Where am I supposed to add those lines ? Thank you very much.
Assuming cell(0).BaseColumnId is set to an "OrderId" data item which you want included in your URL:
private void UltraWebGrid1_InitializeRow(object sender, Infragistics.WebUI.UltraWebGrid.RowEventArgs e){e.Row.Cells[0].Value = "<a href='http://MyDomain.com/MyPage.aspx?OrderId=" & e.Row.Cells[0].Value.toString() & "'>Click Me</a>";}
This is derived from the Knowledge Base article at: http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=1383
Hello,
The scenario is like this: the content is not the raw hyper link, but the hyper link has seperate content. eg: a cell would display like this: click me. So how do I do that? Thank you for replying.
As long as the HTMLEncodeContent property of your column is set to true (which is its default value), you can also put the raw HTML that you want to display in your cell. If you have a hyperlink you want to show in your cell, with certain text and a certain target URL, this provides an alternate way to accomplish the same task.