I'm using the following code to change a hyperlink attributes of a hyperlink which is located in an itemtemplate of a datalist.
When I debug it the attributes are changed and everything is fine, but the end result is that all of the datalist items have the same hyperlink attributes.
Somehow it goes to the last iteration of the for loop, and uses the values that are generated there.
What am I missing ? I've been working on it for two days now, so if anyone has the slightest hint ...
My code :
* SqlExecuter.GetTableCond is simply a function I use to access the database, which returns a datatable and row is a row in that datatable.
* Link and NewWindowText are columns in my database.
Expand|Select|Wrap|Line Numbers
- Protected Sub DataList1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataList1.PreRender
- Dim newsTable As DataTable = SqlExecuter.GetTableCond("NewsTemplate", "PageId", Request.QueryString("PageId"))
- Dim row As DataRow = newsTable.Rows(newsTable.Rows.Count - 1)
- For Each item As DataListItem In DataList1.Items
- Dim titleHL As HyperLink = TryCast(item.FindControl("TitleLink"), HyperLink)
- titleHL.Target() = Convert.ToString(row("NewWindowText"))
- If IsNumeric(Convert.ToString(row("Link"))) Then
- titleHL.NavigateUrl() = "~/Pages/PresentPage.aspx?PageId=" & Convert.ToString(row("Link"))
- Else
- titleHL.NavigateUrl() = Convert.ToString(row("Link"))
- End If
- Dim a As String = titleHL.NavigateUrl()
- Next
- End Sub