Accessing Server Controls inside a Repeater?

J

John Kotuby

Hi all,
I am using a Repeater in conjunction with a SQLDatasource and SQL Server.
One of the controls in the repeater is a HyperlLink as follows:

<asp:HyperLink NavigateUrl='Search.aspx?page=base&amp;searchid=<%
Eval("sequence")%>' .....

As you can see I am trying to pass a QueryString evaluated at runtime. All
the other Evals of DataFields in the Repeater are working just fine. However
this NavigateUrl is resolving as "request.querystring("searchid") = "<%" at
the destination page. I thought that to solve the problem I would then
simply set the string in the code-behind on Page_Load.

However the documentaion is unclear giving me 2 problems.

In pseudo-code here is what I'm trying to do. I know the syntax is wrong,
but I m just trying to display my intention.....

Dim strNav as String
strNav = "Search.aspx?page=base&amp;searchid=" & Eval("sequence")
Repeater.HyperLink.NavigateURL = strNav

How do I access the HyperLink control in the Repeater?
Do I need to use the SearchControl method to find the HyperLink in the
Repeater?

Also, will a simple call like Eval("sequence") work in the code-beind VB, as
it does in the apsx page where the syntax
<% Eval("sequence")%> seems to work for all the data fields called up by
the SQLDatasource control?

Or is there any way to get at the underlying recordset in the SQLDataSource
directly to get the value of a field by name rather than numeric index?

I suppose there might be a better way to get the value of the field
"sequence" to the target page, but for now I am trying QueryString.

Thanks...
 
M

Mark Fitzpatrick

You can always, in code behind, create an onitemdatabound event and in that
event you can access the link directly like so:

// assuming we have a hyperlink in the repeater called link
HyperLink link = (HyperLink)e.Item.FindControl("link");
if(link != null)
{
// do something with the link
link.Text="We did it";
}
 
J

John Kotuby

Thanks Eliyahu,

Actually that was a typo on my part when I posted the message. The actual
syantax on the page is:
<asp:HyperLink NavigateUrl='Search.aspx?page=base&amp;searchid=<%#
Eval("sequence")%>' ...

I just don't understand why the Eval doesn't relace itself with the value of
the "sequence" field in this context, and why the Request.QueryString is
sending over "<%" as a literal and then just stopping right there rather
than sending the entire unresolved string "<%# Eval("sequence")%>". Maybe
the # character is a delimiter in the QueryString in .NET?

I had noticed something similar happenning when I tried to concatenate a
literal string with an Eval to be the text of a Label control within the
Repeater:

<asp:Label Text='Title: <%# Eval("SearchTitle") %>'
Only the literal part "Title:" was showing up.

and this:
<asp:Label Text='Title: ' & ' <%# Eval("SearchTitle") %>'
Generated a syntax error.

I ended up placing 2 Label controls next to each other, one to hold the
Literal string and the other to hold the Evaluated field expression. That
worked.

Eliyahu Goldin said:
I think you are missing just one character:

searchid=<%# Eval("sequence")%>

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


John Kotuby said:
Hi all,
I am using a Repeater in conjunction with a SQLDatasource and SQL Server.
One of the controls in the repeater is a HyperlLink as follows:

<asp:HyperLink NavigateUrl='Search.aspx?page=base&amp;searchid=<%
Eval("sequence")%>' .....

As you can see I am trying to pass a QueryString evaluated at runtime.
All the other Evals of DataFields in the Repeater are working just fine.
However this NavigateUrl is resolving as "request.querystring("searchid")
= "<%" at the destination page. I thought that to solve the problem I
would then simply set the string in the code-behind on Page_Load.

However the documentaion is unclear giving me 2 problems.

In pseudo-code here is what I'm trying to do. I know the syntax is wrong,
but I m just trying to display my intention.....

Dim strNav as String
strNav = "Search.aspx?page=base&amp;searchid=" & Eval("sequence")
Repeater.HyperLink.NavigateURL = strNav

How do I access the HyperLink control in the Repeater?
Do I need to use the SearchControl method to find the HyperLink in the
Repeater?

Also, will a simple call like Eval("sequence") work in the code-beind VB,
as it does in the apsx page where the syntax
<% Eval("sequence")%> seems to work for all the data fields called up by
the SQLDatasource control?

Or is there any way to get at the underlying recordset in the
SQLDataSource directly to get the value of a field by name rather than
numeric index?

I suppose there might be a better way to get the value of the field
"sequence" to the target page, but for now I am trying QueryString.

Thanks...
 
J

John Kotuby

Thanks Mark for another winning suggestion!

Below is an example of the code I wrote that allowed me to dynamically set
the NavigateUrl property of a HyperLink control withing the Repeater. I have
included it for the benefit of anyone interested in this technique.


In the Declaration of the Repeater control designate the handler for the
OnItemDataBound event.
-------------------------
<asp:Repeater ID="Repeater1" OnItemDataBound="R1_ItemDataBound"
runat="server">
-----------------------

Then in the Code Behind file or within Script tags, assemble a string
variable using the DataItem argument of the RepeaterItemEventArgs that are
passed to the handler. That will pull the value of any Data Field that is
present in the current record being processed.
-------------------------

Sub R1_ItemDataBound(ByVal Sender As Object, ByVal e As
RepeaterItemEventArgs)

' This event is raised for the header, the footer, separators, and items.
Dim hlinkEdit As HyperLink
Dim strURL As String

hlinkEdit = e.Item.FindControl("lnkEdit")

If Not IsDBNull(hlinkEdit) Then
strURL = "~/Search/searchDetail.aspx?page=base&searchId=" &
Trim(e.Item.DataItem("sequence").ToString())
hlinkEdit.NavigateUrl = strURL
End If

End Sub
 

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

Top