Hi,
This is one of those things I thought should e easy...
I'm programatically trying to set the navigateURL property in a hyperlink in
the headertemplate of a repeater, but always get the following error "Object
reference not set to an instance of an object." when referencing the
hypermink in the following line..
hypAuthors.NavigateUrl = "http://www.microsoft.com"
Am I able to do this? I know that you cannot databind in a header but I'm
not trying to do that..
I have provided a basic example below which looks at the pubs DB for
illustration.
Many thanks in advance!
Andy
PAGE:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="headertemplate.aspx.vb" Inherits="Examples.headertemplate"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>headertemplate</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Repeater ID="rptAuthors" Runat="server">
<HeaderTemplate>
<table width="156" border="1" cellpadding="0" cellspacing="0">
<tr>
<td><asp:HyperLink ID="Hyperlink1" Runat=server
NavigateUrl="http://msdn.microsoft.com">In Header: MSDN</asp:HyperLink></td>
</tr>
<tr>
<td><asp:HyperLink ID="hypAuthors" Runat=server>In Header:
Microsoft</asp:HyperLink></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Container.DataItem("au_lname")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</HTML>
CODEBEHIND:
Imports System.Data.SqlClient
Public Class headertemplate
Inherits System.Web.UI.Page
Protected WithEvents rptAuthors As System.Web.UI.WebControls.Repeater
Protected WithEvents hypAuthors As System.Web.UI.WebControls.HyperLink
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
End Sub
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim cn As SqlConnection = New SqlConnection("Data
Source=5.15.143.25;DATABASE=PUBS;UID=sa;Password=p assword;")
Try
cn.Open()
Dim objCmd As SqlCommand = New SqlCommand("select top 10
au_lname from authors", cn)
objCmd.CommandType = CommandType.Text
Dim dtrAuthors As SqlDataReader
dtrAuthors = objCmd.ExecuteReader()
If dtrAuthors.HasRows Then
Dim s As String = ""
rptAuthors.DataSource = dtrAuthors
rptAuthors.DataBind()
'Try to programatically set the hyperlink property
'### THE FOLLOWING LINE FAILS WITH "Object reference not set
to an instance of an object."
hypAuthors.NavigateUrl = "http://www.microsoft.com"
End If
Catch ex As Exception
Response.Write(ex.Message)
Finally
End Try
End Sub
End Class