FindControl() problem

Last post 01-19-2008 6:35 AM by vikash2009. 9 replies.

Sort Posts:

  • FindControl() problem

    01-15-2007, 8:31 AM

    Hi There

    I am having problems with the FindControl() method. My aspx page contains 3 panels (but it could be any number).

    In my code-behind, I am using a for loop which loops as many times as there are number of panels, checks if the panel exists on the page and set's it's visible property to false. Here is the C# code, I am also using ASP.NET 2.0:

    Panel

    pnlPanel;
    // hide all panels
    for (i = 0; i < objTabNav.NumTabs; i++)
    {
        intPostfix = i + 1;
        strPageID = "tabPanel" + intPostfix.ToString();
        pnlPanel = (
    Panel)this.FindControl(strPageID);
        if (pnlPanel != null)
        {
            pnlPanel.Visible =
    false;
        }
    }



    My problem is that "pnlPanel" is always null after it looks for and assigns the panel using the FindControl method. The panel ids being passed into the FindControl method are "tabPanel1", "tabPanel2" etc. These panels 100% exist on the page, but the FindControl method cannot find them. Or there is something wrong with the code above (that worked perfectly in ASP.NET 1.1).

    Anyone have any ideas?

    Cheers

    JD
    Web Monkey
  • Re: FindControl() problem

    01-15-2007, 9:02 AM
    • Loading...
    • Marian Kostal
    • Joined on 01-10-2007, 11:33 PM
    • Banovce nad Bebravou, Slovakia
    • Posts 190

    Enable tracing on your page using  

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" Trace="true" %>
     and after you will see tree hierarchy of controls on your page. This can help you to solve your problem.
  • Re: FindControl() problem

    01-15-2007, 9:06 AM
    • Loading...
    • Haissam
    • Joined on 10-05-2006, 6:25 AM
    • Beirut - Lebanon
    • Posts 4,551

    Dear,

    i recommend you to read the below two articles

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

    http://www.odetocode.com/Articles/116.aspx

     

    Happy Coding

    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: FindControl() problem

    01-15-2007, 9:06 AM
    • Loading...
    • V-Bot
    • Joined on 12-20-2006, 4:17 PM
    • Posts 18

    I made the following changes to your code and it works fine for me on .NET 1.1:

     

    private void Button1_Click(object sender, System.EventArgs e)

    {

    Panel pnlPanel;

    // hide all panels

    for (int i = 0; i < 3; i++)

    {

    int intPostfix = i + 1;

    string strPageID = "Panel" + intPostfix.ToString();

    pnlPanel = (Panel)

    this.FindControl(strPageID);

    if (pnlPanel != null)

    {

    pnlPanel.Visible =

    false;

    }

    }

     

    }

     

     

     

    This is the front end code:

       <form id="Form1" method="post" runat="server">
       <asp:Panel id="Panel1" style="Z-INDEX: 101; LEFT: 272px; POSITION: absolute; TOP: 96px" runat="server">Panel 1</asp:Panel>
       <asp:Panel id="Panel2" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 144px" runat="server">Panel 2</asp:Panel>
       <asp:Panel id="Panel3" style="Z-INDEX: 103; LEFT: 288px; POSITION: absolute; TOP: 192px" runat="server">Panel 3</asp:Panel>
       <asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 504px; POSITION: absolute; TOP: 96px" runat="server"
        Text="Button"></asp:Button>
      </form>Panel 1 Panel 2 Panel 3

  • Re: FindControl() problem

    01-15-2007, 9:49 AM
    Answer
    • Loading...
    • e_screw
    • Joined on 10-20-2004, 1:22 PM
    • Women, Guitar, Russia, Billiards, Nature, .NET
    • Posts 3,852

    Seems like you wrote the code in page_load. If you are finding the panels on the page, you need to find them in the form of the page, as panels are placed inside the form, but not on the page directly.

    So its true, you cant find any panel if you use pnlPanel = (Panel) this.FindControl(strPageID); as "this" refers to page, but not the form.

    You need to use

    pnlPanel = (Panel) this.Form.FindControl(strPageID): 

    Thanks 


     

    Mark post(s) as "Answer" that helped you

    Electronic Screw
    Website||Blog||Dub@i.net
  • Re: FindControl() problem

    01-15-2007, 10:59 PM

    hi,

    this code works fine in asp.net2.0

    protected void Button1_Click(object sender, EventArgs e)
        {
            Panel p;
            for (int i = 0; i < 3; i++)
            {
                int num = i + 1;
                string strID = "Panel" + num.ToString();
                p = (Panel)form1.FindControl(strID);

                if (p != null)
                    p.Visible = true;
            }
        }

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
  • Re: FindControl() problem

    01-15-2007, 11:23 PM
    Answer
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 7,360
    • Moderator
      TrustedFriends-MVPs

    If there is a MasterPage involved, you may want to read this thread: http://forums.asp.net/thread/1513625.aspx

     

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: FindControl() problem

    01-15-2007, 11:33 PM
    • Loading...
    • mokeefe
    • Joined on 08-20-2006, 5:15 AM
    • Canberra Australia
    • Posts 2,096

    As for the variable ending up null. That's because you can do this

    Panel

    pnl2 = (Panel)null;

    Therefore the last cycle is not getting a panel. The reasons for which are not evident by your code sample.

    As your panels are direct children of one Control consider removing the ID dependency and just loop through the child controls setting only the Panels like so (likely to be faster than FindControl as it hits each child only once) -

    protected void Page_Load(object sender, EventArgs e)

        {

            HidePanels(objTabNav);

        }

     

        private void HidePanels(Control parent)

        {

     

            foreach (Control obj in parent.Controls)

            {

                if (obj is Panel)

                {

                    Panel pnl = (Panel)obj;

                    pnl.Visible = false;

     

                }

            }

        }

    Rgds,

    Martin.

    Rgds,
    Martin.

    For the benefit of all users please mark any post answers as appropriate.
  • Re: FindControl() problem

    01-16-2007, 7:56 AM

    Thanks.

    Yes, master pages are involved :)

    I wasnt using the findcontrol method in the right place, within the master then the form etc. All sorted now.

    Cheers

    JD
    Web Monkey
  • Re: FindControl() problem

    01-19-2008, 6:35 AM
    • Loading...
    • vikash2009
    • Joined on 01-19-2008, 6:10 AM
    • INDIA
    • Posts 1

    i have dynamic panel within a panel. i have to select them

    but problem in FindControl();

    several panel in the main panel. but it  found only one panel another panel another panel dont find.

    but all are clearly display in page. 

     

    Panel chk1 = (Panel)this.FindControl("mainpanel");
                        Panel pnll = (Panel) chk1.FindControl("pnl1");
                        Response.Write(pnll.ID.ToString());

     

    Please Gimme answer  

Page 1 of 1 (10 items)