Getting value from child control of formview

T

timpera2501

Hello,

I'm a bit of a newb, so please forgive me if this is a stupid question.

I have a simple formview that contains only one control (a label control
called "COUNTLabel"). The formview is bound to an ObjectDataSournce that
returns one value named "COUNT" which is assigned to this label control.

In the code behind, I've been trying to programmatically set the "Visible"
property of the formview control based on the value in the "COUNTLabel" label
control that is within the formview.

In the DataBinding event for the formview I have the following code:

if (EmergencyTTs.Row.FindControl("COUNTLabel").Equals(0))
{
EmergencyTTs.Visible = false;
}
else
{
EmergencyTTs.Visible = true;
}

But the value for "EmergencyTTs.Row.FindControl("COUNTLabel")" is always
null. What am I doing wrong?

Is there a better/easier way to go about going this?
 
P

Phillip Williams

You could have done it declaratively like this:

<asp:Label Id="COUNTLabel" runat="server" Text='<%# Bind ("COUNTFieldName")
%>' Visible='<%# Eval("COUNTFieldName").ToString().Equals("0") %>' ></Label>

You could have still done it the way you tried except that the syntax should
be corrected to look like this:
Label lbl=(Label)FormView1.Row.FindControl("COUNTLabel");
//if control is found
if (lbl !=null) lbl.Visible = lbl.Text.Equals("0");
 
P

Phillip Williams

The second alternative below should be used during the FomView.DataBinding or
FromView.DataBound or any later event, but not during handling the page.load,
as I explained, because the Page.Load event fires before the FormView is
populated with the data.
 
T

timpera2501

Phillip,

Thank you very much for taking the time to respond. =)

I made a method for the Formview Databinding and inserted the following code:

******************
protected void EmergencyTTs_DataBinding(object sender, EventArgs e)
{
Label lbl = (Label)EmergencyTTs.FindControl("COUNTLabel");
//if control is found
if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");
}
******************

However, it still does not hide the formview when the value of the label is
0. Ideas?
 
P

Phillip Williams

Have you tried the declarative approach? (I tried it on my desktop and the
syntax works find)

Regarding the FormView.DataBinding approach you can set up break points on
the code and step through it to verify that the method is called, then look
at the values of the lbl.Text (maybe it is padded with spaces and therefore
you need to adjust the criteria for the if condition)
 
T

timpera2501

Ah ha!!

Figured it out. The code works fine in the method, just instead of:

if (lbl != null) EmergencyTTs.Visible = lbl.Text.Equals("0");

Needed this:

if (lbl != null) EmergencyTTs.Visible = !(lbl.Text.Equals("0"));

=) Thank you sooo much for your help Phillip. Now I can get on with my life!

-Amanda
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top