Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with FindControl!! 1

Status
Not open for further replies.

dcampo

Programmer
Jun 13, 2006
71
EC
Hi

In my webform appear this message in the line 3:

Object reference not set to an instance of an object.

1 string id;
2 id = "f1c2";
3 if(id==(((TextBox)FindControl(id)).ID))
4 {
5 ((TextBox)FindControl(id)).Text = "OK";
6 }
7 else
8 {
9 Label1.Text = "Failed";
10 }

Where is the problem?? What's wrong??

Thanks
 
Have a look at this. If you have trouble ask for more explain:

Code:
        string id = "f1c2";
        TextBox t = (TextBox)this.FindControl(id);

        if (t == null)
        {
            Label1.Text = "failed";
        }
        else
        {
            t.Text = "OK";
        }
 
How about:
Code:
string id; 
id = "f1c2"; 
if (id == (((TextBox)(Page.FindControl("Form1").FindControl(id))).ID)) { 
 ((TextBox)(FindControl(id))).Text = "OK"; 
} else { 
 Label1.Text = "Failed"; 
}
 
Alternately:

Code:
TextBox txt = this.FindControl("f1c2") as TextBox;
Label1.Text 
     = ( txt != null ? "Ok" : "Failed" );
 
.. FindControl(id)).ID

I dont really like that way. Find a control by its id and request its id ?!?!
What do you think? I find it a little dump (no misunderstanding with 'dump')
 
Thanks..

It's fantastic, thanks for help me!!!!!
 
hi guys,

i too faced that problem, currently i am trying using .NET 2 framework.

in page_init event the following is failing:

Ctype(Me.FindControl("tdControl"),HtmlTableCell)

i am getting an object not set to an instance of an object.

the tdControl is present. will try to give you a sample code that is causing this error...

Known is handfull, Unknown is worldfull
 
Yes, give some info on what you want to achive. The CTYPE may be correct, but it must be assigned or assign it somewhere.

... = Ctype(Me.FindControl("tdControl"),HtmlTableCell)
Ctype(Me.FindControl("tdControl"),HtmlTableCell) = ...
?
 
hi guys,

this is wht i tried in my page_init:

Response.Write(CType(Me.FindControl("fntFestivalName"), HtmlTableCell).InnerHtml)

Error:
Object reference not set to an instance of an object.


however if i type the name directly like this it works:
Response.Write(fntFestivalName.innerHTML)

why is this happening???

even these failed:
Response.Write(CType(Me.FindControl("Form1").FindControl("fntFestivalName"), HtmlTableCell).InnerHtml)

Response.Write(CType(Me.Form.FindControl("fntFestivalName"), HtmlTableCell).InnerHtml)

Known is handfull, Unknown is worldfull
 
It will most likely be failing as the control you are looking for won't be part of "Me" and therefore it can't be found.

Instead, either find the form using FindControl and then use FindControl again, or place the control in a panel/placeholder and use the FindControl method of the panel/placeholder.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
>>Instead, either find the form using FindControl and then use FindControl again

my previous post says:

Response.Write(CType(Me.FindControl("Form1").FindControl("fntFestivalName"), HtmlTableCell).InnerHtml)

Response.Write(CType(Me.Form.FindControl("fntFestivalName"), HtmlTableCell).InnerHtml)


Will also try Page.FindControl.

But the above code is working perfectly in ASP.NET 1.1...

Known is handfull, Unknown is worldfull
 
Have you examined the control collection of the parent container using the debugger?

Have you tried delaying searching for the controls until Load?
 
>>Have you examined the control collection of the parent container using the debugger?

will try that.

>>Have you tried delaying searching for the controls until Load?

nope, have not tried that, will try that. but again, why is intellisense working for these objects if i call them directly???

Known is handfull, Unknown is worldfull
 
If it's not a dynamically created control then you can reference it directly. If FindControl() isn't working (and really you wouldn't want to use FindControl() if you have a reference to the control already) then the problem is that you're looking in the wrong place.

As stated FindControl searches the child controls of the control you search with, and it only goes one level deep. i.e. It will find children, but not grandchildren. You'll have to search the proper parent's children to get it to work.

I just confirmed that non-dymanic controls are still available in Page.Init via FindControl() so this is the most likely cause.

As to why it worked with 1.1, I'd wonder if the implementation of your parent container changed or something but it's hard to say without digging into your setup.
 
>>If FindControl() isn't working (and really you wouldn't want to use FindControl() if you have a reference to the control already)

i require this so that i can pass the page object as a parameter to the Business Layer (where i will do a lot of manipulations in about 20 controls).


i did however realise one thing just now. the page is inside a Master Template. therefore

"As stated FindControl searches the child controls of the control you search with, and it only goes one level deep. i.e. It will find children, but not grandchildren"

must be correct.

how do i go about searching such controls???

Known is handfull, Unknown is worldfull
 
You can either do a recursive loop or you can put it in a panel/placeholder like I suggested above.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm nailed it, (though you can also expose the control values as public properties of the page to make things easier) but in my opinion what you want to do is to not pass the UI to the business layer.

At the place I used to work we had components in the UI to perform mapping of control values to collections in the business and data layers. That may be what you really want instead (and if you do it right, you can have aspx files that don't require a line of code in the code-beside that still have full functionality).

PS - If you want to do the public property thing, you'll need a strongly-typed page which means you'll want to download this plug-in from Microsoft:

 
>>You can either do a recursive loop or you can put it in a panel/placeholder like I suggested above.


yeah, that would be an option. but its like I need to do this through out all the pages.

therefore i was inclined to use the Page object.

>> At the place I used to work we had components in the UI to perform mapping of control values to collections in the business and data layers. That may be what you really want instead (and if you do it right, you can have aspx files that don't require a line of code in the code-beside that still have full functionality).

samples???

Known is handfull, Unknown is worldfull
 
samples???

I'm actually a little protective of the specifics of how to accomplish codeless form frameworks because people typically pay me $55/hr to consult for such things. :-D

There's a decent article in this month's Code magazine that does something similar, however.


Also, at a simple level, I can also suggest doing something like this:

Code:
public class Item
{
   private string _name;
   private int _value;

   public string Name
   {
        get{ return _name; }
        set{ _name = value; }
   }

   public int Value
   {
        get{ return _value; }
        set{ _value = value; }
   }
}

//in button click event handler
Item myItem = new Item();
myItem.Name = myTextBox.Text;
myItem.Value = Int32.Parse( myDropDown.SelectedValue );

ProcessingService.ProcessItem( myItem );

This is sort of popular way of handling such things. Guys like Martin Fowler would call it an "anemic domain model" and say the processing logic might belong in the "Item" class itself, but just as many people think he's full of crap and that doing so would violate the principle of single responsiblity.

There are pros and cons to every approach.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top