views:

68

answers:

4

why can not I do this

I have a data list which retrieves some data out. if the label1 is we say 123 then a second label2 must be invisible

    Label Label1 = (Label)DataList2.FindControl("LabelName1");
    Label Label2 = (Label)DataList2.FindControl("LabelName2");

    if (Label1.Text == "123")
    {
        Label2.Visible = false;
    }

when it coms to
if (Label1.Text == "123")
it says Object reference not set to an instance of an object.

A: 

EDIT : Also mention what you cannot do and what error you get to get a better answer... You dont want to use 'Label'(since Label is a keyword/class) as the name of the first one..name it Label1 or something...and use String.Compare() to compare strings...

Misnomer
year I know label is a keyword just a little mistake in the question i have fixed that nowhow do i use string.Compare ?
saadan
It is probably happening because it is not finding your label..and having null in Label1 my guess...try doin it on a click event or something of some button...it seems on page load you may not have access...and check if it is 'LabelName1' as the name again... String.Compare(C#) -http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
Misnomer
yes i got null it cant find the label
saadan
A: 

You forgot to mention what you can't do? what is happening with the it is now?

Also is it "LabelName" or "LabelName1" like you mentioned in the comment - those can be confuling 'l' and '1' (hard to spot)

VoodooChild
i cant find the labels
saadan
A: 

I would check to ensure that "LabelName2" exists. You may have a typo.

dretzlaff17
it fails with if (Label1.Text == "123")so can not be so and have us checked many times but thanks for the reply
saadan
+1  A: 

It depends on where in the page life-cycle you are. The DataList uses ASP.NET templates to build it's internal control collection, this doesn't happen until you've called DataBind, which normally happens automatically just before PreRender. And you can't find controls in the control collection until they have been created.

Where in the page life-cycle are you doing this? What's the context? There's other things to which can further complicate this (one thing is that FindControl is not recursive) in that it will not necessarily search down through each and every naming container. But I'd like to know more about in what context this doesn't work, becuase it does, but you need to be careful.

EDIT:

Something like this should do the trick:

<asp:DataList runat="server" ID="DataList1" OnPreRender="DataList1_PreRender">...</asp:DataList>

And then code behind:

protected void DataList1_PreRender(object sender, EventArgs e)
{
    // the sender in this case is the DataList1 control
    // i often prefer to rely on the sender argument
    Label label1 = (Label)((Control)sender).FindControl("Label1");
    label1.Text = "Yay, it looks like this works!";
}
John Leidegren
I do it in page load when it should happen when the page loaded
saadan
That could be a problem, try hooking up the PreRender event and see if it changes things. These controls are created by instantiating the templates which is typicaly deferred to a later time i.e. PreRender. Make sure you hook up the right PreRender event, preferably the event of the DataList.
John Leidegren
are only beginner to c # so how do I do it hehe
saadan
thanks for your reply but what I come to is that the findcontrole can not even find the label
saadan
Post the entire source code wee need to investigate this furture. Right about now, I would start dumping the visual control tree and inspect the different controls at different points in the page life-cycle becuase there's no simple way that I know of telling you where to look.
John Leidegren