FindControl not working

K

Keith G Hicks

I'm not sure what I'm doing wrong. Here's my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
Request.QueryString("Filt")), HyperLink)
If Not hyp Is Nothing Then
hyp.Font.Bold = True
EndIf

End If

I have several hyperlink controls on a page. Some are ID = hypFiltA,
hypFiltB, hypFiltC, etc. They are used to filter some data by first letter
of the alphabet. The code runs, nothing crashes but but the code above never
finds them. hyp always = nothing.

"hypFilt" & Request.QueryString("Filt") does evaluate out to the correct
value.

Thanks,

Keith
 
K

Keith G Hicks

I should mention that the hyperlink controls are not embeded inside another
control. They are in a content section but they are recognized by
intellisense when I type "me.hyp..." into the vb code page.
 
K

Keith G Hicks

It seems that the ID's are fine. When I put me.hypFiltA, or me.hypFiltB, etc
in the watch, they give me a value.
Request.QueryString("Filt") in the watch window returns what I expect: "A"
or "B" or "C" etc.

When I put me.Controls in the watch window, I get a count of 1.

When I put Me.FindControl("hypFilt" & Request.QueryString("Filt")) in the
watch window, it comes up as Nothing.

If I hard code me.hypFiltA in the same event handler, it works fine. The
control exists and I'm able to set its properties. But I don't want to code
all 26 letters of the alphabet into the event handler.

I tried this in other page events as well (preload, init, initcomplete,
loadcomplete, prerender, prerendercomplete... all with same results.


Eliyahu Goldin said:
Set a breakpoint and see if the id is formed correctly, run Me.FindControl
in the watch window, see what is inside the Me.Controls collection. This
should help you to figure out what is going on.

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


Keith G Hicks said:
I'm not sure what I'm doing wrong. Here's my code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Handles Me.Load

Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
Request.QueryString("Filt")), HyperLink)
If Not hyp Is Nothing Then
hyp.Font.Bold = True
EndIf

End If

I have several hyperlink controls on a page. Some are ID = hypFiltA,
hypFiltB, hypFiltC, etc. They are used to filter some data by first letter
of the alphabet. The code runs, nothing crashes but but the code above
never
finds them. hyp always = nothing.

"hypFilt" & Request.QueryString("Filt") does evaluate out to the correct
value.

Thanks,

Keith
 
K

Keith G Hicks

If you put Request.QueryString("Filt") in the watch window, what do you
see?

returns "A"
If you put "hypFilt" & Request.QueryString("Filt") in the watch window, what
do you see?

returns "hypFiltA"
Does Me.FindControl("hypFilt" & Request.QueryString("Filt").ToString())
work?

returns Nothing
 
K

Keith G Hicks

No. Like I said in my 2nd post, it's all in a content section. It's a master
page setup (asp.net 2.0 if that helps). This is one of the content pages.
Here's the markup (below the @Page and the @Register Assembly lines):

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2"
runat="Server">
<asp:Image ID="Image1" runat="server"
ImageUrl="~/Images/PageTitles_YearbookPics.jpg" /><br />
<br />
Click an image for a larger view.<br />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="Server">
<br />
<span style="font-size: 11pt">Filters (based on last name at
graduation):</span>
<asp:HyperLink ID="hypFiltAll" runat="server"
NavigateUrl="~/ClassmateYearbookPics.aspx?Filt=ALL" Font-Size="11pt">Show
All</asp:HyperLink>
<asp:HyperLink ID="hypFiltA" runat="server"
NavigateUrl="~/ClassmateYearbookPics.aspx?Filt=A"
Font-Size="11pt">A</asp:HyperLink>
<asp:HyperLink ID="hypFiltB" runat="server"
NavigateUrl="~/ClassmateYearbookPics.aspx?Filt=B"
Font-Size="11pt">B</asp:HyperLink>

etc.....
 
K

Keith G Hicks

Mark Rae said:
[top-posting corrected]
No. Like I said in my 2nd post, it's all in a content section. It's a
master
page setup (asp.net 2.0 if that helps). This is one of the content pages.
Here's the markup (below the @Page and the @Register Assembly lines):

OK, just for the sake of clarity, the hyperlinks are all within a content
page, not the MasterPage.

That's correct.

And presumably the code which is trying to refer to those hyperlinks is
similarly behind the content page, not behind the MasterPage...?

That's correct also.
 
G

George Ter-Saakov

I am sorry to jump in, but do your hyperlinks have runat=server atribute??
Find control will only work with .NET object.

George.

Keith G Hicks said:
Mark Rae said:
[top-posting corrected]
In which case, it sounds very much like the hypFiltA hyperlink isn't
actually in the Page control container. Might it be contained within
another
container within the Page container e.g. an <asp:panel> ...?

No. Like I said in my 2nd post, it's all in a content section. It's a
master
page setup (asp.net 2.0 if that helps). This is one of the content pages.
Here's the markup (below the @Page and the @Register Assembly lines):

OK, just for the sake of clarity, the hyperlinks are all within a content
page, not the MasterPage.

That's correct.

And presumably the code which is trying to refer to those hyperlinks is
similarly behind the content page, not behind the MasterPage...?

That's correct also.
 
G

George Ter-Saakov

Sorry did not see your message...
I've never had a problem with FindControl not working as it suppose to...
So here is my take...

you have a following code which appears to be fine.

Dim hyp As HyperLink = CType(Me.FindControl("hypFilt" &
Request.QueryString("Filt")), HyperLink)
If Not hyp Is Nothing Then
hyp.Font.Bold = True
EndIf

Following options.

0. All you do is set Bold to font. Not sure how Hyperlink control works with
it. The style for <A> tag might take over and overwrite the BOLD thing.
Cause it might product folowing html code <B><A>B</A></B> then style for A
tag will take over the <B> tag.
So do something else.

1. Code never executes.... Easy to test in debugger or just add
Response.Write("AAA") right before you do FindCotrol

My bet would be on #0 then #1.

George.
 
K

Keith G Hicks

There are things I plan to do other than bold. That was my first step. When
I couldn't get that to work I stopped to solve the other problem. Anyway, I
solved it. See my post under Mark's
 
K

Keith G Hicks

There is only one control. Me.Controls.Count = 1. Me.Control(0) =
{ASP.masterpage_master}. Interesting.

So here's the answer. Ready?

Dim hyp As HyperLink =
CType(Me.Master.FindControl("ContentPlaceHolder1").FindControl("hypFilt" &
Request.QueryString("Filt")), HyperLink)

This page helped a lot:

http://www.west-wind.com/WebLog/posts/5127.aspx

Apparently master pages really screw things up in this regard.

Thanks everyone for all your help.

Keith
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top