.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

findControl ?

Posted By:      Posted Date: September 13, 2010    Points: 0   Category :ASP.Net
Hi to access an aspx drop down control inside a user control?

View Complete Post

More Related Resource Links


I have 2 web pages. First page has a textbox and a button control.

when I enter test and click on the button , system goes to the second page. And in the second page I want to access the Textbox in the first page. When I try to do this I get the following error.

"Object reference not set to an instance of an object. Following is my code :

Dim txt As New TextBox

' Find the server name on the previous page
txt = CType(Page.PreviousPage.FindControl("TextBox2"), TextBox)
If Not IsNothing(txt) Then
Response.Write("[Name Not available]")
End If

I know I could do this with session, but I want to use the Page.PreviousPage.FindControl. Any help is greatly appreciated

Page.FindControl Causing Null Reference Exception


Hi guys,

I developed a page in VWD 2008 and it works perfectly locally, but when I deployed it on the server every Page.FindControl() call is causing the NullReference Exception "Object reference not set to an instance of an object. " Can anyone help me out? Code is below


Mike Marshall

            For i As Integer = 1 To 10
                If (CType(Page.FindControl("last_" & i), TextBox).Text.Length > 0) Or (CType(Page.FindControl("first_" & i), TextBox).Text.Length > 0) Or (CType(Page.FindControl("dob_" & i), TextBox).Text.Length > 0) Then
                    Dim person_entry(7) As String
                    person_entry(0) = info_list.Count - 1
                    person_entry(1) = CType(Page.FindControl("last_" & i), TextBox).Text
                    person_entry(2) = CType(Page.FindControl("first_" & i), TextBox).Text
                    person_entry(3) = CType(Page.FindControl("dob_" & i), TextBox).Text
                    person_entry(4) = CType(Page.FindControl("appear_" & i), TextBox).Text
                    person_entry(5) = CType(Page.FindControl("mental_" & i), TextBox).Text
                    person_entry(6) = CType(Page.FindControl("behave_" & i), TextB

GridView EmptyDataTemplate and FindControl Method


I have 2 textboxes, a dropdownlist and a button in the EmptyDataTemplate of a GridView control and when I click the button I want to get the text from the text boxes and DropDownList put it in the QueryString of a URL i redirect to, but I can't work out why the FindControl method cannot find the controls.  I am using the following code:

Dim ddlNumPpl As DropDownList = GridView1.FindControl("ddlNumPpl")

I get an "Object reference not set to an instance of an object" error though when I try to refer to the SelectedValue property of it though.  I would be very grateful if someone could let me know why the FindControl method can't find the control and what I should do to rectify it.

Many Thanks (in advance),


Nested controls in UserControl return null (FindControl)

Hi everyone, I have a very weird issue. I have a UserControl that has some controls inside. I want to refer those controls after, in another postback. But when I try to get them the ".Controls" property of my controls returns null. I'm working on vs2008 Here is the sample code: public partial class MyUserControl : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { foreach (Control control in this.Controls) { Response.Write(control.ClientID); } } private void MyTable() { Table table = new Table(); TableRow row = new TableRow(); TableCell cell = new TableCell(); CheckBox check = new CheckBox(); check.ID = "theId"; check.Text = "My Check"; check.AutoPostBack = true; cell.Controls.Add(check); row.Cells.Add(cell); check = new CheckBox(); check.ID = "theOther"; check.AutoPostBack = true; check.Text = "My Other Check"; cell = new TableCell(); cell.Controls.Add(check); row.Cells.Add(cell); table.Rows.Add(row); this.Controls.Add(table); } protected override void Render(HtmlTextWriter writer) { MyTable(); base.Render(writer); } } And in the Default.aspx is like:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.cs" Inherits="Tester.Default" %> &

Using FindControl on Nested FormViews

I have a nested FormView and I need to get the Text values from some of its xontrols (TextBox) in code behind. I can easily pull the values from the "parent" FormView using: TextBox txtBox = (TextBox)fvParent.FindControl("txtField");String myStr= txtBox.Text; Although, when I try to get a refence to one of the controls in the "child" FormView I get a null. I am trying to do this: FormView formView = (FormView)fvParent.FindControl("fvChild");TextBox txtBox = (TextBox)fvChild.FindControl("txtChildField");String myStr= txtBox.Text; The variable "formView" is set to null. Can the FindControl method not find nested items? If not, what is the proper way of getting the values from these fields (in the Child/Nested FormView)?   Thanks again for everyone's help!  

LoginView.FindControl not finding control

I'm trying to bind data to a repeater. The repeater is inside a loginView control. Like so:            <asp:loginview id="loginView" runat="server">                <anonymoustemplate>                    <asp:repeater id="rpLessons" runat="server">                                            <itemtemplate>                            <div>                                <a  href="Default.aspx?lid=<%# Eval("PodID") %>"><%# Eval("Name") %></a><%#Eval("Duration")%>                            </div>                        </itemtemplate>    &n

Div in LoginView findcontrol

Hi,This code works:<div id="GridDiv" runat="server">               gridview would go in here....           </div>GridDiv.Visible=false;However, because I have a Div in LoginView2 I have to find with findcontrol.This is not work:        LoginView Div = LoginView2.FindControl("GridDiv") as LoginView;        Div.Visible = false; Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error: Line 785: Line 786: Line 787: Div.Visible = false; Thanks for your help!

FindControl returns null

I am trying to fetch two Labels within a Repeater element to attach data to; which Label is assigned which Data Field depends on the result of a Radio Button List, hence why I need to fetch them in the code behind. This is the ASP.NET code: <asp:View ID="vwSportType" runat="server"> <table> <tr> <td rowspan="2"> <span class="boldtext">Select type of sport:</span><br /><br /> <asp:ListBox ID="lbType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="lbType_OnSelectedIndexChanged" Height="300px" Width="12em"> </asp:ListBox> </td> <td width="70%"> <span class="boldtext">Show by: <asp:RadioButtonList ID="rbSportType" runat="server" RepeatDirection="Horizontal"> <asp:ListItem Text="Date, Sport" Selected="True" Value="0"></asp:ListItem> <asp:ListItem Text="Sport,

function - findcontrol

How to write a function that should always be identified:RadioButton LoginView2.FindControl RadioButton22 = ("RadioButton2") as RadioButton;Currently, I always write in any event: RadioButton LoginView2.FindControl RadioButton22 = ("RadioButton2") as RadioButton;Is it possible how to write a function that would be written once and used repeatedly?And how do you call?thanks.

findcontrol problem in a gridview

Hey guys I've got a gridview with a few textboxes in each row which I want to insert into a database, after a user clicks a submit button (thats outside of the gridview)  This is my code for the gridview<asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" EnableModelValidation="True"> <Columns> <asp:TemplateField > <ItemTemplate> name: <asp:Label ID="Label12" runat="server" Text='<%# Bind("PlayN") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="TxtFirst" runat="server"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="txtLast" runat="server"></asp:TextBox> </ItemTemplate>

FindControl TextBox

Hi all, I have one page text.aspx there is textbox control <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <% Dim mf As New MyClass mf.filltxt() %>I am trying to call filltxt from MyClass via below code. but it seems code is unable to find the control. Public Sub filltxt() Dim txtb As TextBox = CType(Page.FindControl("TextBox1"), TextBox) txtb.Text = "hello world" End SubI am getting Error : Object reference not set to an instance of an object.I will appreciate if you guys correct my code.Thank You

Pager Findcontrol

 Hi, I use a pager to navigate a listview<asp:DataPager ID="Pager" runat="server" PagedControlID="Listview1" PageSize="6"> <Fields> <asp:TemplatePagerField> <PagerTemplate> <asp:Label runat="server" ID="CurrentPageLabel" Text="<%# IIf(Container.TotalRowCount>0, (Container.StartRowIndex / Container.PageSize) + 1 , 0) %>" /> <asp:Label runat="server" ID="TotalPagesLabel" Text="<%# Math.Ceiling (System.Convert.ToDouble(Container.TotalRowCount) / Container.PageSize) %>" /> <asp:Label runat="server" ID="TotalItemsLabel" Text="<%# Container.TotalRowCount%>" /> </td><td class="tdPager"></td><td> </PagerTemplate> </asp:TemplatePagerField> <asp:NextPreviousPagerField ShowFirstPageButton ="true" ShowNextPageButton="true" ShowPreviousPageButton="true" ShowLastPageButton="true" ButtonType="Button" ButtonCssClass="Navigator" FirstPageText="|<" LastPageText=">|" NextPageText=">" PreviousPageText="<" /> </Fields> </asp:DataPager>  <asp:Label ID="lbl1" runat=&q

Findcontrol returns an empty control


Hello all,

The concept of findcontrol seems to elude me somewhat and I would like some advice if anyone can spare it.

My SP page layout contains a richhtmlfield with a highly descriptive ID of "RichHtmlField1". The same page layout contains a custom user control that needs to read te contents of this richhtmlfield and e-mail it.

To get the content I figured I'd use the findcontrol function. And this works fine. I then cast the control to a richhtmlfield and try to access the .Html property. But apparently that's empty. I don't understand that, since if you look at the page the content of the field is clearly filled.

Can anybody shed some light on the subject?

My (highly professional) code:

HtmlGenericControl myDiv = new HtmlGenericControl("div");

Control cntlTest = this.Parent.FindControl("RichHtmlField1");

//This works and gives me the proper results
myDiv.InnerHtml += cntlTest.ID + " || " + cntlTest.GetType();

//This is empty
myDiv.InnerHtml += 

FindControl - basic explanation for a moron like me...



ASP 3.5

Dim control as Control

control = Page.ParseControl("<tr><td><asp:textbox id='textbox1' runat='server' /></td></tr>")

This as simple as I can make it.

So, in QuickWatch, I can see:

control.Controls(0) is a literalcontrol

control.Controls(1) is a textbox with id, clientid and uniqueid all set to 'textbox1'.

So the 64k question:

Why does control.FindControl("textbox1") return Nothing? I really don't get it. I know that FindControl is not recursive, but textbox1 is in the controlscollection of the root control! Please explain - this is driving me nuts...




Loading ImageButtons Dynamically using FindControl


I have a table where each cell contains an ImageButton, with an iterative ID:

	<asp:TableCell ID="zm_1">
		<asp:ImageButton ID="zmi_1"  runat="server" Height="15px" Width="15px" />
	<asp:TableCell ID="zm_2">
		<asp:ImageButton ID="zmi_2"  runat="server" Height="15px" Width="15px" />
	<asp:TableCell ID="zm_3">
		<asp:ImageButton ID="zmi_3"  runat="server" Height="15px" Width="15px" />

I am then trying to assign a URL to each imagebutton dynamically in a separate code file

ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
ImageButton button = new ImageButton();
for (int i=1;i <=3; i++)
        button = MainContent.FindControl("zmi_" + i.ToString()) as ImageButton;
        button.ImageUrl = Server.MapPath("~/img/some_image.jpg");

Everything compiles, but when I run this I throw a Null Reference Exception:  {"Object reference not set to an instance of an object."}


Failed to FindControl on Web Page



I got a content place holder, in this holder, I have a grid view. Inside this grid view, I will have multiple textbox added at runtime.

From the source code, I found that one of the textbox's id is as "ctl00_CPHDetail_gvDetail_ctl02_txt_Text1", CPHDetail is the content place holder, and gvDetail is the grid view.

What I wanted to do is to find the text box control and retrieve its value.

So far, this is what I have done, and it was able to return the correct info up to the grid view. It can never find the text box control.

I wonder will it caused by the ct102 between "ctl00_CPHDetail_gvDetail_ctl02_txt_Text1"

ContentPlaceHolder myContent = (ContentPlaceHolder)Page.Master.FindControl("CPHDetail");
GridView gvView = (GridView)myContent.FindControl("gvDetail");
TextBox tb = (TextBox)gvView.FindControl("txt_Text1");

Pls advise.

with FindControl() receives: Object reference not set to an instance of an object.   


I am trying to "Find" a TableCell in a GridView but receive the error: Object reference not set to an instance of an object.

First in the Page_Load event I set 5 rows to this GridView1. After this I loop through the GridView1 to FindControl("tdCell1") to set a String to each <td> on each of those 5 rows.

But I receive the above error when doing this. I wonder what I can be doing wrong? Thanks!

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            EnableViewState="false" Font-Names="Times New Roman" Font-Size="12pt" 
            ForeColor="Black" Height="116px" style="margin-right: 0px" Width="48%">
                            <table align="left" class="collapse" style="height:100%" 
                                    <td id="tdCell1" align="left" style="height:100%; width:100%">
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend