473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2412
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Philip Townsend | last post by:
I have a repeater control that writes an HTML table. Itv needs to display a header in the first row, then a list of records produced from a SQL table. The header text is also produced from the SQL...
5
by: Martin Dew | last post by:
Having some problems getting a hyperlink object to work in my repeater control, It displays the text I have asked it to for the hyperlink, but it does not act as a link. My repeater code is below...
2
by: George Durzi | last post by:
Here's my asp:Repeater <asp:Repeater ID="rptHelpDeskSystemStatus" Runat="server"> <HeaderTemplate> <table width="100%" border="0" class="datatable"> <tr class="item"> </HeaderTemplate>...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
0
by: Keith | last post by:
I have a repeater control that contains a HeaderTemplate and an ItemTemplate. Each item contains a checkbox with an ID of chkReconciled, and the header contains a single checkbox with an ID of...
0
by: Keith | last post by:
I have a repeater control that contains a HeaderTemplate and an ItemTemplate. Each item contains a checkbox with an ID of chkReconciled, and the header contains a single checkbox with an ID of...
4
by: Brad Baker | last post by:
I'm going a little crazy :) I'm trying to bind a repeater control to a dataset on page load using the following code: if (Request.QueryString != null) { string customerid = Request.QueryString;...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
3
by: Leon Mayne | last post by:
If I have a datatable which looks like this: Record Id, Group Id, Name 1, 1, Test 1 2, 1, Test 2 3, 2, Test 3 4, 3, Test 4 Is it possible to use nested repeaters to group the information by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.