472,128 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

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

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
Jul 20 '06 #1
2 2331
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";
}
}

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
"Andy" <An**@discussions.microsoft.comwrote in message
news:69**********************************@microsof t.com...
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

Jul 20 '06 #2
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..
Jul 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Philip Townsend | last post: by
5 posts views Thread by Martin Dew | last post: by
2 posts views Thread by George Durzi | last post: by
8 posts views Thread by I am Sam | last post: by
4 posts views Thread by Brad Baker | last post: by
3 posts views Thread by Leon Mayne | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.