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.
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.
<div>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. </div>
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.
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:
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!
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.
Using FindControl is kind of a hack and is completely against OO principles. The following link dicusses two ways in which you may access a child control.
disearches
Member
31 Points
40 Posts
Re: Solution to the FindControl problem
Aug 05, 2009 10:03 AM|LINK
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.
the_N_Channe...
Member
8 Points
13 Posts
Re: Solution to the FindControl problem
Aug 06, 2009 12:03 AM|LINK
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.
<div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <asp:FormView ID="FormView1" runat="server" onprerender="FormView1_PreRender"></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <ItemTemplate> </div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </ItemTemplate></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> </asp:FormView></div> <div style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"></asp:Content></div><%@ 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!";
}
}
<div>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. </div>the_N_Channe...
Member
8 Points
13 Posts
Re: Solution to the FindControl problem
Aug 06, 2009 02:09 AM|LINK
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.
disearches
Member
31 Points
40 Posts
Re: Solution to the FindControl problem
Aug 06, 2009 03:22 PM|LINK
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
SugarBomb
Member
32 Points
6 Posts
Re: Solution to the FindControl problem
Sep 16, 2009 03:38 PM|LINK
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.
You may find Truth gift-wrapped within a paradox...Hope this helps!
friendster
Member
749 Points
189 Posts
Re: Solution to the FindControl problem
Sep 17, 2009 09:21 AM|LINK
This one is intresting.
http://highoncoding.com/Articles/606_Creating_a_BetterFindControl_and_MuchBetterFindControl.aspx
Raj
aplon
Member
6 Points
4 Posts
Re: Solution to the FindControl problem
Oct 28, 2009 11:46 AM|LINK
Hey Guys, I am going CRAZY with this FindControl stuff. Any help will be GREATLY appreciated. This is the scenario:
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
FindControl FindControlIterative FindControlRecursive
nkmekal
Member
27 Points
37 Posts
Re: Solution to the FindControl problem
Jan 01, 2010 08:32 PM|LINK
Using FindControl is kind of a hack and is completely against OO principles. The following link dicusses two ways in which you may access a child control.
http://tekstem.com/QuestionDetail/134/Using-Find-Control-in-ASPNET
Hope this helps.
MNK