Can I set properties of a hyperlink in a repeater headertemplate??

G

Guest

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=password;")
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
 
T

Teemu Keiski

You can't access it directly in Page_Load because it is child control of the
repeater, you need to query/locate it via the repeater and the corresponding
RepeaterItem. You could do it by handling Repeater's ItemCreated event,
check for e.ItemType for being Header, then run
e.Item.FindControl("hypAuthors") to find the control. Ten you could access
it (cast to HyperLink first)

protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{
HyperLink hypAuthors =
(HyperLink)e.Item.FindControl("hypAuthors");
hypAuthors.NavigateUrl = "http://www.foo.com";
}
}
 
G

Guest

Thanks for the reply,
I've worked out a slightly different way, so that after binding to the
repeater I use findcontrol thus:

Dim hyp As HyperLink =
rptAuthors.Controls(0).FindControl("hypAuthors")

If Not (hyp Is Nothing)
hyp.NavigateUrl = "http://www.microsoft.com"
End If

Many thanks..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top