By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,855 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,855 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.