.NET Forum / ASP.NET / General / May 2008
FindControl not working
|
|
Thread rating:  |
Keith G Hicks - 29 May 2008 01:38 GMT 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
Keith G Hicks - 29 May 2008 03:16 GMT 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.
> I'm not sure what I'm doing wrong. Here's my code: > [quoted text clipped - 20 lines] > > Keith Eliyahu Goldin - 29 May 2008 09:55 GMT 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.
 Signature Eliyahu Goldin, Software Developer Microsoft MVP [ASP.NET] http://msmvps.com/blogs/egoldin http://usableasp.net
> I'm not sure what I'm doing wrong. Here's my code: > [quoted text clipped - 22 lines] > > Keith Keith G Hicks - 29 May 2008 13:25 GMT 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.
> 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 [quoted text clipped - 33 lines] > > > > Keith Mark Rae [MVP] - 29 May 2008 13:40 GMT > When I put Me.FindControl("hypFilt" & Request.QueryString("Filt")) in the > watch window, it comes up as Nothing. If you put Request.QueryString("Filt") in the watch window, what do you see?
If you put "hypFilt" & Request.QueryString("Filt") in the watch window, what do you see?
Does Me.FindControl("hypFilt" & Request.QueryString("Filt").ToString()) work?
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
Keith G Hicks - 29 May 2008 13:53 GMT > > When I put Me.FindControl("hypFilt" & Request.QueryString("Filt")) in the > > watch window, it comes up as Nothing.
> 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
> -- > Mark Rae > ASP.NET MVP > http://www.markrae.net Mark Rae [MVP] - 29 May 2008 14:02 GMT >> If you put Request.QueryString("Filt") in the watch window, what do you >> see? [quoted text clipped - 10 lines] > > returns Nothing 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> ...?
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
Keith G Hicks - 29 May 2008 14:23 GMT 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.....
> >> If you put Request.QueryString("Filt") in the watch window, what do you > >> see? [quoted text clipped - 19 lines] > ASP.NET MVP > http://www.markrae.net Mark Rae [MVP] - 29 May 2008 14:37 GMT [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 [quoted text clipped - 5 lines] > 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.
And presumably the code which is trying to refer to those hyperlinks is similarly behind the content page, not behind the MasterPage...?
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
Keith G Hicks - 29 May 2008 18:12 GMT > [top-posting corrected] > [quoted text clipped - 10 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.
> -- > Mark Rae > ASP.NET MVP > http://www.markrae.net George Ter-Saakov - 29 May 2008 18:35 GMT I am sorry to jump in, but do your hyperlinks have runat=server atribute?? Find control will only work with .NET object.
George.
>> [top-posting corrected] >> [quoted text clipped - 23 lines] >> ASP.NET MVP >> http://www.markrae.net Keith G Hicks - 29 May 2008 19:05 GMT Yes. It's in the code I posted a couple of posts up.
> I am sorry to jump in, but do your hyperlinks have runat=server atribute?? > Find control will only work with .NET object. > > George. George Ter-Saakov - 29 May 2008 19:27 GMT 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.
> Yes. It's in the code I posted a couple of posts up. > [quoted text clipped - 3 lines] >> >> George. Keith G Hicks - 29 May 2008 22:04 GMT 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
> Sorry did not see your message... > I've never had a problem with FindControl not working as it suppose to... [quoted text clipped - 30 lines] > >> > >> George. Mark Rae [MVP] - 29 May 2008 19:36 GMT >> 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. Hmm - OK. I'm starting to run out of options now...
If the code which is trying to reference the hyperlink is definitely behind the same page where the hyperlink is defined, and the hyperlink is not contained within a separate container, then FindControl should definitely find it.
So, can you please try the following:
In the watch window, evaluate Me.Controls.Count and then inspect each of them e.g.
Me.Controls[0] Me.Controls[1]
etc
Is hypFiltA one of them...?
 Signature Mark Rae ASP.NET MVP http://www.markrae.net
Keith G Hicks - 29 May 2008 22:06 GMT 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|