Solution to the FindControl problem

Rate It (6)

Last post 10-28-2009 7:46 AM by aplon. 37 replies.

Sort Posts:

  • Re: Solution to the FindControl problem

    08-05-2009, 6:03 AM
    • Member
      19 point Member
    • disearches
    • Member since 02-25-2009, 6:23 AM
    • Posts 35

     Hi guys please i have read thru all the replies in this topic and i have tried most of em, but i still cant get the controls embeded in Data controls like gridview , datalist, formview etc.

    this is an old post (early replies were based on .NET 2.0), i am on .NET 3.5 SP1, i dont know if they is any bug in this current framework thats resulting to my problem.

  • Re: Solution to the FindControl problem

    08-05-2009, 8:03 PM

    I echo that sentiment. I've gone over dozens of threads that talk about using FindControl in Pre_Render(), Data_Bound(), and a whole bunch of other places, and this still doesn't work. 


    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:FormView ID="FormView1" runat="server" onprerender="FormView1_PreRender">
        <ItemTemplate> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        </ItemTemplate>
        </asp:FormView>
    </asp:Content>

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>


    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

        <asp:FormView ID="FormView1" runat="server" onprerender="FormView1_PreRender">

        <ItemTemplate> 

            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

        </ItemTemplate>

        </asp:FormView>

    </asp:Content>


    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;


    public partial class Default2 : System.Web.UI.Page

    {

        protected void FormView1_ItemCreated(object sender, EventArgs e)

        {

            TextBox aText = (TextBox) Util.FindControl("TextBox1", Page.Controls);

            TextBox myText = (TextBox)FormView1.FindControl("TextBox1");

            myText.Text = "Hello!";

        }

        protected void FormView1_DataBound(object sender, EventArgs e)

        {

            TextBox myText = (TextBox)FormView1.FindControl("TextBox1");

            myText.Text = "Hello!";

        }

        protected void FormView1_PreRender(object sender, EventArgs e)

        {

            TextBox myText = (TextBox)FormView1.FindControl("TextBox1");

            myText.Text = "Hello!";

        }

    }

    No matter where I try, FindControl is returning null. I'm also running 3.5 SP1. I have a hard time believing its the framework itself, but none of the solutions I've seen posted on the forums seem to work. 

  • Re: Solution to the FindControl problem

    08-05-2009, 10:09 PM

    Got a solution. This was because the control was not explicitly being created without the FormView being data bound. I needed to explicitly call Formview1.DataBind(). I also didn't understand that the ItemTemplate is only rendered when the data source actually has data, otherwise the EmptyDataTemplate is used.

    For FindControl() in naming containers with templates involved, it's important to know if your template is being created and your controls exist when you think they do.  


  • Re: Solution to the FindControl problem

    08-06-2009, 11:22 AM
    • Member
      19 point Member
    • disearches
    • Member since 02-25-2009, 6:23 AM
    • Posts 35

     for data presentation contols, you have to use the findcontrol on the individual rows or items (depending on the kind of control)

    for example for a listview control : listview1.items[0].findcontol();

    for a gridview: gridview1.rows[0].findcontrol();

    i have tried it and it worked

  • Re: Solution to the FindControl problem

    09-16-2009, 11:38 AM
    • Member
      32 point Member
    • SugarBomb
    • Member since 10-08-2008, 4:30 AM
    • Posts 6

    david wendelken:

    chalamarc:

    i think using of findcontrol() on your formview or anyother control name, it is the best way as you neednot to actually go down on your performance and also you can get rid of all the code that has been shown on the top and one which make full use of the api's and its functions which microsoft provides.

     

    If that were a valid argument, we would still be coding in COBOL and FORTRAN, or maybe 1s and 0s. :) 

    Computer Science is the science of giving away something you want to get something else you want more.

    Which route to use is simply a matter of priorities.  The recursive (or iterative) versions of FindControl provide the following benefits and drawbacks:

    -  Runtime Performance
    + Codetime Performance (it's faster and more convenient for the programmer!)
    + Robustness when the number of levels in the UI Layer changes
    + Easier code to read.

    Hard-wiring the reference by nesting chaining FindControl methods offers the following benefits and drawbacks:

    + Runtime Performance
    -  Codetime Performance
    -  Robustness
    -  Harder to read.

    Pick the method that best meets your needs.

     

     

    I hope that a lot of people stumble cross this bit of wisdom. If Codetime performance was properly considered in code construction, coding efforts would move closer to projected timelines. 

    I have found through years of experience in managing coding efforts (including my own) that professional developers err more on the side of optimizing RunTime performance vs CodeTime performance.

    You also could express the "Robustness" and "Harder to Read" jointly as "BugTime" and "MaintenanceTime" performance. These are huge time investments.

     

    -SugarBomb

    You may find Truth gift-wrapped within a paradox...Hope this helps!
  • Re: Solution to the FindControl problem

    09-17-2009, 5:21 AM
    • Member
      689 point Member
    • friendster
    • Member since 12-02-2004, 11:45 PM
    • India,Banglore
    • Posts 162
    According to my calculations the problem doesn't exist.
  • I need this script plz helppp

    10-23-2009, 10:31 PM

    I need this script plz helppp  

    Script is like this web : merrjep.com  plz help to find iii need soo much 

  • Re: Solution to the FindControl problem

    10-28-2009, 7:46 AM
    • Member
      4 point Member
    • aplon
    • Member since 10-28-2009, 7:34 AM
    • Posts 2

    Hey Guys, I am going CRAZY with this FindControl stuff. Any help will be GREATLY appreciated. This is the scenario:

    1. I am using the whole membership login stuff described in the tutorial (GREAT STUFF, btw)
    2. I have a master page with a LoginView1 which includes 2 Labels
    3. I have a content page from which I do the Login with the LoginWizard
    4. Once the user is authenticated, I want to place the user's role in one of the Master's Label box described in step 2
    5. I have tried all recomendations: FindControlIterative, using several FindControls in one sentence, etc.
    6. Example: "

    lblUserType = Page.Master.FindControl("LoginContent").FindControl("LoginView1").FindControl("lblLoginUserRole")"

    As you can see, the Label is contained inside the LoginView, which in turn is contained inside a ContentPlaceholder which is located in the Master page.

    I am goin crazy with this as I continue to get an error message that states: "Object reference not set to an instance of an object"

    I also tried setting up the property in the Master page, but I cannot reference the Label control either.

    Please...please, any help will do!!!!

    Thanks  a lot, Ariel

Page 3 of 3 (38 items) < Previous 1 2 3